Title: Data race between `gc.get_stats()` and a concurrent collection under free-threading · Issue #151646 · python/cpython · GitHub
Open Graph Title: Data race between `gc.get_stats()` and a concurrent collection under free-threading · Issue #151646 · python/cpython
X Title: Data race between `gc.get_stats()` and a concurrent collection under free-threading · Issue #151646 · python/cpython
Description: Bug report Bug description: gc.get_stats() (gc_get_stats_impl) plain-copies the per-generation stats structs gcstate->generation_stats->{young,old}.items[index] without any lock or atomics, cpython/Modules/gcmodule.c Lines 377 to 379 in ...
Open Graph Description: Bug report Bug description: gc.get_stats() (gc_get_stats_impl) plain-copies the per-generation stats structs gcstate->generation_stats->{young,old}.items[index] without any lock or atomics, cpython...
X Description: Bug report Bug description: gc.get_stats() (gc_get_stats_impl) plain-copies the per-generation stats structs gcstate->generation_stats->{young,old}.items[index] without any lock or atomics, c...
Opengraph URL: https://github.com/python/cpython/issues/151646
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Data race between `gc.get_stats()` and a concurrent collection under free-threading","articleBody":"# Bug report\n\n### Bug description:\n\n`gc.get_stats()` (`gc_get_stats_impl`) plain-copies the per-generation stats structs \n`gcstate-\u003egeneration_stats-\u003e{young,old}.items[index]` without any lock or atomics,\n\nhttps://github.com/python/cpython/blob/9e863fab283eddca9c2a8f9d1ee30f4dc243e314/Modules/gcmodule.c#L377-L379\n\nwhile a collection on another thread writes those same `items[index]` fields (`collections++`, `collected += m`, ...) at the end of `gc_collect_main`,\n\nhttps://github.com/python/cpython/blob/9e863fab283eddca9c2a8f9d1ee30f4dc243e314/Python/gc_free_threading.c#L2284-L2292\n\nso `gc.get_stats()` races the collector.\n\nReproducer:\n```python\nimport gc\nfrom threading import Thread\n\ndef collector():\n for _ in range(50000):\n a = {}; a['self'] = a\n gc.collect()\n\ndef reader():\n for _ in range(50000):\n gc.get_stats()\n\nthreads = [Thread(target=collector) for _ in range(1)]\nthreads += [Thread(target=reader) for _ in range(8)]\nfor t in threads: t.start()\nfor t in threads: t.join()\n```\n\nTSAN Report:\n```\n==================\nWARNING: ThreadSanitizer: data race (pid=2301505)\n Read of size 8 at 0x7fffb41a0180 by thread T2:\n #0 gc_get_stats_impl /cpython/Modules/gcmodule.c:379:16 \n #1 gc_get_stats /cpython/Modules/clinic/gcmodule.c.h:456:12 \n #2 cfunction_vectorcall_NOARGS /cpython/Objects/methodobject.c:508:24 \n #3 _PyObject_VectorcallTstate /cpython/./Include/internal/pycore_call.h:144:11 \n #4 PyObject_Vectorcall /cpython/Objects/call.c:327:12 \n #5 _Py_VectorCallInstrumentation_StackRefSteal /cpython/Python/ceval.c:768:11 \n #6 _PyEval_EvalFrameDefault /cpython/Python/generated_cases.c.h:1846:35 \n ...\n\n Previous write of size 8 at 0x7fffb41a0180 by thread T1:\n #0 gc_collect_main /cpython/Python/gc_free_threading.c:2288:23 \n #1 _PyGC_Collect /cpython/Python/gc_free_threading.c:2568:12 \n #2 gc_collect_impl /cpython/Modules/gcmodule.c:93:12 \n #3 gc_collect /cpython/Modules/clinic/gcmodule.c.h:143:21 \n #4 _Py_BuiltinCallFastWithKeywords_StackRef /cpython/Python/ceval.c:841:11 \n #5 _PyEval_EvalFrameDefault /cpython/Python/generated_cases.c.h:2508:35 \n ...\n\nSUMMARY: ThreadSanitizer: data race /cpython/Modules/gcmodule.c:379:16 in gc_get_stats_impl\n==================\n```\n\n### CPython versions tested on:\n\nCPython main branch\n\n### Operating systems tested on:\n\nLinux\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-151766\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/Naserume","@type":"Person","name":"Naserume"},"datePublished":"2026-06-18T12:06:31.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/151646/cpython/issues/151646"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:581a702d-17fd-10c6-442b-b0905d85356b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C76A:3AD55C:836078:BFB22C:6A57878D |
| html-safe-nonce | efe334b67dbdb14c7d7817d6e9b5354564ee71ab16eeb2379c4ad04633d8a46a |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNzZBOjNBRDU1Qzo4MzYwNzg6QkZCMjJDOjZBNTc4NzhEIiwidmlzaXRvcl9pZCI6IjI0OTI4NTkyNjE1OTM4MTQ5MjUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 4839fde22b1954bacd0fb075a6a12b3ef711f83e34982150c9932c189d69c519 |
| hovercard-subject-tag | issue:4692182984 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/151646/issue_layout |
| twitter:image | https://opengraph.githubassets.com/07e3a90b4d947bfeb0deb9112003cb30781f3133051d6b00daac456838ff1ca4/python/cpython/issues/151646 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/07e3a90b4d947bfeb0deb9112003cb30781f3133051d6b00daac456838ff1ca4/python/cpython/issues/151646 |
| og:image:alt | Bug report Bug description: gc.get_stats() (gc_get_stats_impl) plain-copies the per-generation stats structs gcstate->generation_stats->{young,old}.items[index] without any lock or atomics, cpython... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Naserume |
| hostname | github.com |
| expected-hostname | github.com |
| None | c6353e56e94423c9cd5abde7708a859afa36a0c1203c73749055bf6ee6704cd0 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | f59ab1c46b638204e2aea09126d089be5dc773b7 |
| ui-target | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width