Title: gh-149816: Fix race in subtype_getweakref in free-threading build by Abhi210 · Pull Request #150247 · python/cpython · GitHub
Open Graph Title: gh-149816: Fix race in subtype_getweakref in free-threading build by Abhi210 · Pull Request #150247 · python/cpython
X Title: gh-149816: Fix race in subtype_getweakref in free-threading build by Abhi210 · Pull Request #150247 · python/cpython
Description: Fixes gh-149816 This PR resolves finding 61 from the weakref race tracking issue: "Racy weakref head load before incref in Objects/typeobject.c". Description In free-threaded builds, subtype_getweakref() had a race condition where the weakref list head (*weaklistptr) was loaded and immediately passed to Py_NewRef(). If another thread concurrently triggered weakref_dealloc(), the weakref could be freed before the incref, leading to a use-after-free (UAF). This PR fixes the issue by: Wrapping the pointer load and incref sequence inside the existing LOCK_WEAKREFS(obj) / UNLOCK_WEAKREFS(obj) critical section. Replacing Py_NewRef() with _Py_TryIncref() to safely handle weakrefs whose refcount has reached zero during the race window. Returning Py_NewRef(Py_None) when the list is empty or the weakref is no longer live, preserving the existing API behavior. Testing Added regression tests in Lib/test/test_free_threading/test_weakref.py that: Exercise obj.__weakref__ concurrently from reader and mutator threads synchronized with threading.Barrier. Verify correct return values (None versus weakref.ref). Check refcount stability across repeated accesses to detect leaks or incorrect incref/decref behavior. AI Tool Disclosure: Claude Code was used to help understand the relevant code paths and to improve the clarity and documentation of the proposed tests. It was not used to modify Objects/typeobject.c; all code changes and final decisions were made by me.
Open Graph Description: Fixes gh-149816 This PR resolves finding 61 from the weakref race tracking issue: "Racy weakref head load before incref in Objects/typeobject.c". Description In free-threaded builds, sub...
X Description: Fixes gh-149816 This PR resolves finding 61 from the weakref race tracking issue: "Racy weakref head load before incref in Objects/typeobject.c". Description In free-threaded bui...
Opengraph URL: https://github.com/python/cpython/pull/150247
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:eff22287-27d3-6453-f223-f0b407c6ab50 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | 82DE:103B4D:E8B3F:13DF51:6A5259EE |
| html-safe-nonce | f36c674cad2543c579c0277b7808232e88e1129a0dc00735e7bdb8f4812329c4 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MkRFOjEwM0I0RDpFOEIzRjoxM0RGNTE6NkE1MjU5RUUiLCJ2aXNpdG9yX2lkIjoiMzAzMDI5Njk0NjA2Nzc5MDMxOCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 045f5e7d7fa2e29fab5d3d6bac0ab56d5f92515334a6c362d285697d29169848 |
| hovercard-subject-tag | pull_request:3730318350 |
| 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/150247/files |
| twitter:image | https://avatars.githubusercontent.com/u/27881020?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/27881020?s=400&v=4 |
| og:image:alt | Fixes gh-149816 This PR resolves finding 61 from the weakref race tracking issue: "Racy weakref head load before incref in Objects/typeobject.c". Description In free-threaded builds, sub... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | b9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb |
| 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 | 7aed05249554b889eb33d002851a973eebcc7e91 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width