gh-153014: Fix data race on the GC debug flag in free-threading builds#153015
Open
tonghuaroot wants to merge 2 commits into
Open
gh-153014: Fix data race on the GC debug flag in free-threading builds#153015tonghuaroot wants to merge 2 commits into
tonghuaroot wants to merge 2 commits into
Conversation
… builds In free-threading builds gc.set_debug() stored gcstate->debug with a plain write while gc.get_debug() and the collector read it without synchronisation, which ThreadSanitizer reports as a data race. Access the flag with FT_ATOMIC_STORE/LOAD_INT_RELAXED, consistent with how gc.enable()/disable() already handle gcstate->enabled.
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.
In a free-threading build,
gc.set_debug()storedgcstate->debugwith aplain write while
gc.get_debug()and the collector read it withoutsynchronisation.
gc.set_debug()takes no lock and, unlikegc_set_threshold_impl(), does not run under_PyEval_StopTheWorld(), soThreadSanitizer reports a write/write race on
gcstate->debug.This accesses the flag with
FT_ATOMIC_STORE_INT_RELAXED/FT_ATOMIC_LOAD_INT_RELAXEDingc_set_debug_impl()/gc_get_debug_impl()and in the collector reads in
Python/gc_free_threading.c. Four of thosecollector reads run outside stop-the-world (in
delete_garbage(),handle_legacy_finalizers(), and two stats reads ingc_collect_main()) andrace the write directly; the read in
gc_collect_internal()runs understop-the-world and is made atomic only for consistency. Relaxed ordering is
correct for an independent
intflag, matching howgcstate->enabledandsysdlopenflags (gh-151644) are already handled.Python/gc.c(the default GIL build) is left unchanged: it has no concurrentaccess, and
FT_ATOMIC_*compiles to a plain load/store there anyway.Verified with a
--with-thread-sanitizer --disable-gilbuild stressingconcurrent
gc.set_debug()/gc.get_debug()plus a thread churning cyclicgarbage:
A regression test is added under
Lib/test/test_free_threading/.