Title: gh-151646: Fix data race between gc.get_stats() and GC in free-threading builds by zangjiucheng · Pull Request #151766 · python/cpython · GitHub
Open Graph Title: gh-151646: Fix data race between gc.get_stats() and GC in free-threading builds by zangjiucheng · Pull Request #151766 · python/cpython
X Title: gh-151646: Fix data race between gc.get_stats() and GC in free-threading builds by zangjiucheng · Pull Request #151766 · python/cpython
Description: In free-threading builds, gc_get_stats_impl() reads per-generation stats with plain struct copies while gc_collect_main() writes the same fields after _Py_StartTheWorld() — no synchronisation on either side. Fix: add PyMutex stats_mutex to _gc_runtime_state (under Py_GIL_DISABLED) and hold it around both the seven field writes in the writer and the three struct copies in the reader. The lock is acquired after _Py_StartTheWorld(), so no deadlock with stop-the-world. GIL build is unchanged. 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 Issue: gh-151646
Open Graph Description: In free-threading builds, gc_get_stats_impl() reads per-generation stats with plain struct copies while gc_collect_main() writes the same fields after _Py_StartTheWorld() — no synchronisation on ei...
X Description: In free-threading builds, gc_get_stats_impl() reads per-generation stats with plain struct copies while gc_collect_main() writes the same fields after _Py_StartTheWorld() — no synchronisation on ei...
Opengraph URL: https://github.com/python/cpython/pull/151766
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:33cf1d71-38a3-67bf-0b16-d976cc0eab6c |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | 9A12:5F24D:127319:19DF1E:6A57B114 |
| html-safe-nonce | 450e59ea129a0b19c2f4d243b908f12af56f176c58897615c7452a65c313cbc4 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QTEyOjVGMjREOjEyNzMxOToxOURGMUU6NkE1N0IxMTQiLCJ2aXNpdG9yX2lkIjoiNTc5NjI5NjI5MTc0NDQ2MTA3NiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | fdf0b107749d6fc9079a85f9e89c2e9567d765f68e6d9775551ad9b2818d619c |
| hovercard-subject-tag | pull_request:3902242653 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,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/python/cpython/pull/151766/files |
| twitter:image | https://avatars.githubusercontent.com/u/56462380?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/56462380?s=400&v=4 |
| og:image:alt | In free-threading builds, gc_get_stats_impl() reads per-generation stats with plain struct copies while gc_collect_main() writes the same fields after _Py_StartTheWorld() — no synchronisation on ei... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 899c314dfde9abcf8fc1fe8c04f09866b347ea06f43166d653afc482795c9fca |
| turbo-cache-control | no-preview |
| diff-view | unified |
| 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 full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 67aa73f3bba9e68b2de0ebe01c65fcd120461cba |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width