Title: Free-threading race betwen two Py_SETREF · Issue #161714 · python/cpython · GitHub
Open Graph Title: Free-threading race betwen two Py_SETREF · Issue #161714 · python/cpython
X Title: Free-threading race betwen two Py_SETREF · Issue #161714 · python/cpython
Description: Bug report This can be triggered with existing Python objects that use Py_SETREF or Py_XSETREF such as Exceptions (although slightly contrived): import threading e = Exception() def worker(): for _ in range(1_000_000): e.__init__(1) thre...
Open Graph Description: Bug report This can be triggered with existing Python objects that use Py_SETREF or Py_XSETREF such as Exceptions (although slightly contrived): import threading e = Exception() def worker(): for _...
X Description: Bug report This can be triggered with existing Python objects that use Py_SETREF or Py_XSETREF such as Exceptions (although slightly contrived): import threading e = Exception() def worker(): for _...
Opengraph URL: https://github.com/python/cpython/issues/161714
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Free-threading race betwen two Py_SETREF","articleBody":"### Bug report\n\nThis can be triggered with existing Python objects that use `Py_SETREF` or `Py_XSETREF` such as Exceptions (although slightly contrived):\n```python\nimport threading\n\ne = Exception()\n\ndef worker():\n for _ in range(1_000_000):\n e.__init__(1)\n\nthreads = [threading.Thread(target=worker) for _ in range(2)]\nfor t in threads:\n t.start()\nfor t in threads:\n t.join()\n```\n\nGives on a 3.16 free-threaded debug build:\n```\n../cpython/Include/refcount.h:617: _Py_NegativeRefcount: Assertion failed: object has negative ref count\nEnable tracemalloc to get the memory block allocation traceback\n\nobject address : 0x2000204c3b0\nobject refcount : 0\nobject type : 0x5623737ad5c8\nobject type name: tuple\nobject repr : \u003crefcnt 0 at 0x2000204c3b0>\n\nFatal Python error: _PyObject_AssertFailed: _PyObject_AssertFailed\nPython runtime state: initialized\n\nStack (most recent call first):\n File \"/\u003chome>/projects/python/setref_bug.py\", line 8 in worker\n File \"/\u003chome>/projects/python/cpython/Lib/threading.py\", line 1161 in run\n File \"/\u003chome>/projects/python/cpython/Lib/threading.py\", line 1219 in _bootstrap_inner\n File \"/\u003chome>/projects/python/cpython/Lib/threading.py\", line 1181 in _bootstrap\n[1] 397225 abort (core dumped) ./build/python setref_bug.py\n```\n\nThis arrives because `Py_SETREF` copies the ref and stores a new new one as two separate non-atomic steps. So two threads can do:\n\n```\nT1: old = *slot\nT2: old = *slot // same object as T1\nT1: *slot = a\nT2: *slot = b // a's reference is lost, so a leaks\nT1: Py_XDECREF(old)\nT2: Py_XDECREF(old) // the slot's one reference to old is dropped twice\n```\n\nI ran into this trying to build a cache which swapped an old value for a new value with `Py_SETREF` and finding it sometimes resulted in a double free when two threads swapped the value simultaneously (And ended up thinking they had the last reference).\n\n### CPython versions tested on:\n\n3.16, CPython main branch\n\n### Operating systems tested on:\n\nLinux","author":{"url":"https://github.com/cmaloney","@type":"Person","name":"cmaloney"},"datePublished":"2026-11-09T23:34:22Z","url":"https://github.com/python/cpython/issues/161714"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| 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:78a7bae0-934a-8e00-7f48-6b233ab01a82 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AFFE:16AB79:3C9FA06:4D08295:6B020AE4 |
| html-safe-nonce | a04e29ebdaffbad191e27f0c83c67833aac464a9982ec4034d0cdb2b3312388d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBRkZFOjE2QUI3OTozQzlGQTA2OjREMDgyOTU6NkIwMjBBRTQiLCJ2aXNpdG9yX2lkIjoiODI3OTU0MjMwMjM2MDM0MTIyMCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | bcbd5dbd69495a94aed2f79fd8b3f2e3c71ad1b9a3d1033559f2ed50039c4d2c |
| hovercard-subject-tag | issue:6296878675 |
| 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/161714/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d896d687fcd17ddd3035e5ae00abb44f97006658fc87af8a86057ddb572e8ee5/python/cpython/issues/161714 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d896d687fcd17ddd3035e5ae00abb44f97006658fc87af8a86057ddb572e8ee5/python/cpython/issues/161714 |
| og:image:alt | Bug report This can be triggered with existing Python objects that use Py_SETREF or Py_XSETREF such as Exceptions (although slightly contrived): import threading e = Exception() def worker(): for _... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | cmaloney |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| 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 | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width