Fix concurrent nc_client_init() and nc_server_init() - #641
Merged
Conversation
michalvasko
requested changes
Sep 10, 2026
Roytak
force-pushed
the
global-init-race
branch
from
September 10, 2026 12:31
6c69c10 to
5748040
Compare
The client and the server side both initialized the same process-global libraries (libcurl, the TLS backend, libssh) without any lock, so an application (or test_thread_messages) that starts a server thread and initializes the client at the same time raced inside them. With MbedTLS this corrupts the shared PSA entropy context, which was observed as a leaked 216 byte hash context, a double free, a segfault or a spurious psa_crypto_init() failure. Both sides now init and destroy the libraries through nc_global_init() and nc_global_destroy() guarded by a lock. The libraries are also initialized only once and destroyed only once the other side does not use them any longer, so nc_client_destroy() no longer tears down the crypto state of a running server and vice versa.
test_thread_messages never destroyed either side, leaking the global state of the used libraries on every run.
Roytak
force-pushed
the
global-init-race
branch
from
September 11, 2026 09:59
5748040 to
6a0bbd5
Compare
michalvasko
approved these changes
Sep 11, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
nc_client_init()andnc_server_init()initialized the same process-global libraries without any lock, so doing both at once from two threads raced inside them.Both sides now go through
nc_global_init()/nc_global_destroy(), which take a lock and track which side initialized the libraries. They are therefore also initialized only once and destroyed only when neither side uses them any longer, sonc_client_destroy()no longer tears down the crypto state of a running server and vice versa.test_thread_messagesalso never destroyed either side.Seen in: https://github.com/CESNET/libnetconf2/actions/runs/34359810512