{{ message }}
gh-151646: Fix data race between gc.get_stats() and GC in free-threading builds - #151766
Merged
Conversation
LindaSummer
reviewed
Jul 2, 2026
Contributor
There was a problem hiding this comment.
It seems the guard macro Py_GIL_DISABLED is missing here and below the unlock.
Member
There was a problem hiding this comment.
The whole gc_free_threading.c file is wrapped in #ifdef Py_GIL_DISABLED so I don't think that's needed.
Member
|
This looks okay to me. Some comments:
Suggested unit test change: |
Contributor
Author
|
I will take a look after work and address comments, thanks for reviewing :) |
Member
|
I revised the unit test to use |
Address review feedback (nascheme) on pythonGH-151766: - The stress test looped 8 reader threads x 50,000 iterations each. Run one writer for a fixed number of gc.collect() calls and have the readers loop until the writer signals done, so the duration is driven by the collection count. Use threading_helper.run_concurrently() for start synchronisation. - Trim the stats_mutex comments: keep one comment on the struct member saying what it protects, and drop the now-redundant comments at the lock sites. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Contributor
Author
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 free-threading builds,
gc_get_stats_impl()reads per-generation statswith plain struct copies while
gc_collect_main()writes the same fieldsafter
_Py_StartTheWorld()— no synchronisation on either side.Fix: add
PyMutex stats_mutexto_gc_runtime_state(underPy_GIL_DISABLED) and hold it around both the seven field writes in thewriter and the three struct copies in the reader. The lock is acquired after
_Py_StartTheWorld(), so no deadlock with stop-the-world. GIL build isunchanged.
Also adds
Lib/test/test_free_threading/test_gc_get_stats_race.py.Verified with TSAN on Linux: race confirmed on
main, clean on the patch.🤖 Generated with Claude Code
gc.get_stats()and a concurrent collection under free-threading #151646