Repository navigation
Fix data race on the thread-done pthread key at process exit - #1446
Merged
Merged
Conversation
_mi_heap_default_key is a plain global. At process exit, _mi_prim_thread_done_auto_done() resets it and deletes the key, while another thread that is still running may be initializing mimalloc and reading it in _mi_prim_thread_associate_default_theap(). ThreadSanitizer reports this as a data race; CPython hits it in an embedding test that leaves a thread blocked in PyGILState_Ensure() while main() returns. Make the key atomic, like _mi_theap_default_key in prim-tls.c: publish it with a release store after pthread_key_create succeeds, swap it out with an exchange before deleting it, and load it once before pthread_setspecific. Apply the same change to the Emscripten copy of this code.
Collaborator
|
Thanks! (and they should indeed be static as well). |
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.
_mi_heap_default_key is a plain global. At process exit, _mi_prim_thread_done_auto_done() resets it and deletes the key, while another thread that is still running may be initializing mimalloc and reading it in _mi_prim_thread_associate_default_theap(). ThreadSanitizer reports this as a data race; CPython hits it in an embedding test that leaves a thread blocked in PyGILState_Ensure() while main() returns.
Make the key atomic, like _mi_theap_default_key in prim-tls.c: publish it with a release store after pthread_key_create succeeds, swap it out with an exchange before deleting it, and load it once before pthread_setspecific. Apply the same change to the Emscripten copy of this code.