Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TSAN: don't race through pthread_cond_destroy #477

Merged
merged 1 commit into from
Apr 10, 2024
Merged

Conversation

jktjkt
Copy link
Contributor

@jktjkt jktjkt commented Apr 10, 2024

In the test_ch test, the test_nc_ch_tls case would result in the following report:

 WARNING: ThreadSanitizer: data race (pid=7309)
   Write of size 8 at 0x7b24000148e0 by thread T6:
     #0 pthread_cond_destroy <null> (test_ch+0x459fd1) (BuildId: 0dd508005b002d2a874ae63746b8b5ea13e50df5)
     #1 nc_ch_client_thread /home/ci/src/cesnet-gerrit-public/CzechLight/dependencies/libnetconf2/src/session_server.c:2909:5 (test_ch+0x4f9f85) (BuildId: 0dd508005b002d2a874ae63746b8b5ea13e50df5)

   Previous read of size 8 at 0x7b24000148e0 by thread T5 (mutexes: write M0, write M1):
     #0 pthread_cond_signal <null> (test_ch+0x459c6c) (BuildId: 0dd508005b002d2a874ae63746b8b5ea13e50df5)
     #1 nc_server_config_destroy_ch_client /home/ci/src/cesnet-gerrit-public/CzechLight/dependencies/libnetconf2/src/server_config.c:920:9 (test_ch+0x4fb12a) (BuildId: 0dd508005b002d2a874ae63746b8b5ea13e50df5)
     #2 nc_server_config_ch /home/ci/src/cesnet-gerrit-public/CzechLight/dependencies/libnetconf2/src/server_config.c:1039:9 (test_ch+0x4fdd27) (BuildId: 0dd508005b002d2a874ae63746b8b5ea13e50df5)
     #3 nc_server_config_setup_data /home/ci/src/cesnet-gerrit-public/CzechLight/dependencies/libnetconf2/src/server_config.c:4211:5 (test_ch+0x4fdd27)
     #4 server_thread_tls /home/ci/src/cesnet-gerrit-public/CzechLight/dependencies/libnetconf2/tests/test_ch.c:307:11 (test_ch+0x522849) (BuildId: 0dd508005b002d2a874ae63746b8b5ea13e50df5)

The pthread_cond_destroy(3p) documentation says that:

It shall be safe to destroy an initialized condition variable upon which no threads are currently blocked. Attempting to destroy a condition variable upon which other threads are currently blocked results in undefined behavior.

Let's make sure that this is the case by locking the associated mutex.

In the `test_ch` test, the `test_nc_ch_tls` case would result in the
following report:

 WARNING: ThreadSanitizer: data race (pid=7309)
   Write of size 8 at 0x7b24000148e0 by thread T6:
     #0 pthread_cond_destroy <null> (test_ch+0x459fd1) (BuildId: 0dd508005b002d2a874ae63746b8b5ea13e50df5)
     CESNET#1 nc_ch_client_thread /home/ci/src/cesnet-gerrit-public/CzechLight/dependencies/libnetconf2/src/session_server.c:2909:5 (test_ch+0x4f9f85) (BuildId: 0dd508005b002d2a874ae63746b8b5ea13e50df5)

   Previous read of size 8 at 0x7b24000148e0 by thread T5 (mutexes: write M0, write M1):
     #0 pthread_cond_signal <null> (test_ch+0x459c6c) (BuildId: 0dd508005b002d2a874ae63746b8b5ea13e50df5)
     CESNET#1 nc_server_config_destroy_ch_client /home/ci/src/cesnet-gerrit-public/CzechLight/dependencies/libnetconf2/src/server_config.c:920:9 (test_ch+0x4fb12a) (BuildId: 0dd508005b002d2a874ae63746b8b5ea13e50df5)
     CESNET#2 nc_server_config_ch /home/ci/src/cesnet-gerrit-public/CzechLight/dependencies/libnetconf2/src/server_config.c:1039:9 (test_ch+0x4fdd27) (BuildId: 0dd508005b002d2a874ae63746b8b5ea13e50df5)
     CESNET#3 nc_server_config_setup_data /home/ci/src/cesnet-gerrit-public/CzechLight/dependencies/libnetconf2/src/server_config.c:4211:5 (test_ch+0x4fdd27)
     CESNET#4 server_thread_tls /home/ci/src/cesnet-gerrit-public/CzechLight/dependencies/libnetconf2/tests/test_ch.c:307:11 (test_ch+0x522849) (BuildId: 0dd508005b002d2a874ae63746b8b5ea13e50df5)

The pthread_cond_destroy(3p) documentation says that:

> It shall be safe to destroy an initialized condition variable upon
> which no threads are currently blocked. Attempting to destroy a
> condition variable upon which other threads are
> currently blocked results in undefined behavior.

Let's make sure that this is the case by locking the associated mutex.
jktjkt added a commit to CESNET/CzechLight-dependencies that referenced this pull request Apr 10, 2024
jktjkt added a commit to CESNET/CzechLight-dependencies that referenced this pull request Apr 10, 2024
jktjkt added a commit to CESNET/libnetconf2-cpp that referenced this pull request Apr 10, 2024
This is really just a change of logging, nothing else. Unfortunately,
the new logging still affects a global state, it was really just a
change of the function signature, and the logger callback might well be
invoked with a NULL session. This means that mapping from the
nc_session* to a libnetconf::client::Session is not possible, so I
though that simply providing an opaque pointer is not a bad solution.

Change-Id: Iaa10855f4173fdd5e3046efa1827e4931cd05acb
Depends-on: https://gerrit.cesnet.cz/c/CzechLight/libyang-cpp/+/7049
Depends-on: CESNET/libnetconf2#477
Depends-on: CESNET/libnetconf2#478
jktjkt added a commit to CESNET/CzechLight-dependencies that referenced this pull request Apr 10, 2024
@michalvasko
Copy link
Member

I do not think this change actually prevents any run-time errors and just makes TSAN happy, which is something I consider a waste of time. But since I have no problem with the change, it can be merged, thanks.

@michalvasko michalvasko merged commit fd3a01d into CESNET:devel Apr 10, 2024
11 checks passed
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