Skip to content

Fix test_ch data race reported by TSAN - #648

Closed
jktjkt wants to merge 2 commits into
CESNET:develfrom
jktjkt:fix-tsan-test-ch
Closed

jktjkt wants to merge 2 commits into
CESNET:develfrom
jktjkt:fix-tsan-test-ch

Conversation

@jktjkt

@jktjkt jktjkt commented Oct 16, 2026

Copy link
Copy Markdown
Contributor

We hit this error in the CI:

WARNING: ThreadSanitizer: data race (pid=7016)
 Read of size 1 at 0x0000019048f0 by thread T2:
   #0 server_thread_ssh libnetconf2/tests/test_ch.c:126:15

 Previous write of size 8 at 0x0000019048f0 by thread T3:
   #0 strncpy
   #1 test_msg_callback libnetconf2/tests/test_ch.c:54:9
   #2 nc_log_vprintf libnetconf2/src/log.c:134:9
   #3 nc_log_printf libnetconf2/src/log.c:152:5
   #4 nc_ch_client_thread libnetconf2/src/session_server.c:4478:17

 As if synchronized via sleep:
   #0 usleep
   #1 nc_session_free libnetconf2/src/session.c:1206:13
   #2 server_thread_ssh libnetconf2/tests/test_ch.c:122:13

 Location is global 'buffer' of size 512 at 0x0000019048f0 (test_ch+0x19048f0)

 Thread T2 (tid=7126, running) created by main thread at:
   #0 pthread_create
   #1 test_nc_ch_ssh libnetconf2/tests/test_ch.c:249:11
   #2 <null> <null> (libcmocka.so.0+0x618f)
   #3 __libc_start_call_main /usr/src/debug/glibc-2.37-18.fc38.x86_64/csu/../sysdeps/nptl/libc_start_call_main.h:58:16

 Thread T3 (tid=7127, running) created by thread T2 at:
   #0 pthread_create
   #1 _nc_connect_ch_client_dispatch libnetconf2/src/session_server.c:4770:14
   #2 nc_connect_ch_client_dispatch libnetconf2/src/session_server.c:4813:10
   #3 server_thread_ssh libnetconf2/tests/test_ch.c:113:11

SUMMARY: ThreadSanitizer: data race libnetconf2/tests/test_ch.c:126:15 in server_thread_ssh

jktjkt and others added 2 commits October 16, 2026 13:27
The log callback runs in the call-home client thread and copied the
matching message into a global buffer, while the test thread polled that
buffer with strlen(), which TSan reported as a data race:

 WARNING: ThreadSanitizer: data race (pid=7016)
  Read of size 1 at 0x0000019048f0 by thread T2:
    #0 server_thread_ssh libnetconf2/tests/test_ch.c:126:15

  Previous write of size 8 at 0x0000019048f0 by thread T3:
    #0 strncpy
    CESNET#1 test_msg_callback libnetconf2/tests/test_ch.c:54:9
    CESNET#2 nc_log_vprintf libnetconf2/src/log.c:134:9
    CESNET#3 nc_log_printf libnetconf2/src/log.c:152:5
    CESNET#4 nc_ch_client_thread libnetconf2/src/session_server.c:4478:17

  As if synchronized via sleep:
    #0 usleep
    CESNET#1 nc_session_free libnetconf2/src/session.c:1206:13
    CESNET#2 server_thread_ssh libnetconf2/tests/test_ch.c:122:13

  Location is global 'buffer' of size 512 at 0x0000019048f0 (test_ch+0x19048f0)

  Thread T2 (tid=7126, running) created by main thread at:
    #0 pthread_create
    CESNET#1 test_nc_ch_ssh libnetconf2/tests/test_ch.c:249:11
    CESNET#2 <null> <null> (libcmocka.so.0+0x618f)
    CESNET#3 __libc_start_call_main /usr/src/debug/glibc-2.37-18.fc38.x86_64/csu/../sysdeps/nptl/libc_start_call_main.h:58:16

  Thread T3 (tid=7127, running) created by thread T2 at:
    #0 pthread_create
    CESNET#1 _nc_connect_ch_client_dispatch libnetconf2/src/session_server.c:4770:14
    CESNET#2 nc_connect_ch_client_dispatch libnetconf2/src/session_server.c:4813:10
    CESNET#3 server_thread_ssh libnetconf2/tests/test_ch.c:113:11

 SUMMARY: ThreadSanitizer: data race libnetconf2/tests/test_ch.c:126:15 in server_thread_ssh

The message text itself was never used, so replace the buffer with an
atomic flag.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The log callback reads the expected message from whichever thread is
logging, while the test thread overwrote it with strcpy(). Point to a
string literal through an atomic pointer instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
michalvasko added a commit that referenced this pull request Oct 16, 2026
@michalvasko

Copy link
Copy Markdown
Member

Oh, I thought it is just an issue, not a pull request. In any case, I have added a lock for the messages so the error should be gone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants