Title: Data race on `ThreadHandle.ident` between `thread_shutdown` and `ThreadHandle_start` under free-threading · Issue #154937 · python/cpython · GitHub
Open Graph Title: Data race on `ThreadHandle.ident` between `thread_shutdown` and `ThreadHandle_start` under free-threading · Issue #154937 · python/cpython
X Title: Data race on `ThreadHandle.ident` between `thread_shutdown` and `ThreadHandle_start` under free-threading · Issue #154937 · python/cpython
Description: Bug report Bug description: On a free-threaded build, ThreadHandle.ident is written and read under two different locks. ThreadHandle_start sets self->ident while holding the handle's own mutex (self->mutex): cpython/Modules/_threadmodule...
Open Graph Description: Bug report Bug description: On a free-threaded build, ThreadHandle.ident is written and read under two different locks. ThreadHandle_start sets self->ident while holding the handle's own mutex (sel...
X Description: Bug report Bug description: On a free-threaded build, ThreadHandle.ident is written and read under two different locks. ThreadHandle_start sets self->ident while holding the handle's own mut...
Opengraph URL: https://github.com/python/cpython/issues/154937
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Data race on `ThreadHandle.ident` between `thread_shutdown` and `ThreadHandle_start` under free-threading","articleBody":"# Bug report\n\n### Bug description:\n\nOn a free-threaded build, `ThreadHandle.ident` is written and read under two different locks.\n\n`ThreadHandle_start` sets `self->ident` while holding the handle's own mutex (`self->mutex`):\nhttps://github.com/python/cpython/blob/22a6c51c94a4fde986b8964f1d36d5ec3ac20dcc/Modules/_threadmodule.c#L483-L490\n\n`thread_shutdown` reads `cur->ident` while holding the runtime head lock (`HEAD_LOCK(&_PyRuntime)`):\nhttps://github.com/python/cpython/blob/22a6c51c94a4fde986b8964f1d36d5ec3ac20dcc/Modules/_threadmodule.c#L2408-L2422\n\nBecause the write is guarded by `self->mutex` and the read by `HEAD_LOCK`, the two do not exclude each other. \n\nhttps://github.com/python/cpython/blob/22a6c51c94a4fde986b8964f1d36d5ec3ac20dcc/Modules/_threadmodule.c#L1912-L1924\n\nThe comment explains the handle is added early to avoid missing a thread that finishes quickly, but that is exactly what exposes `ident` to the `HEAD_LOCK`-side reader in `thread_shutdown` before it is written under `self->mutex`.\n\nReproducer:\n```python\nimport _thread\nimport time\nfrom threading import Thread\n\ndef chain1_thread():\n for _ in range(12000):\n try:\n _thread._shutdown()\n except Exception:\n pass\n\ndef chain2_thread():\n for _ in range(12000):\n try:\n _thread.start_new_thread(lambda: None, ())\n except Exception:\n pass\n time.sleep(0.0001)\n\nN_C1 = 1\nN_C2 = 8\nthreads = [Thread(target=chain1_thread) for _ in range(N_C1)]\nthreads += [Thread(target=chain2_thread) for _ in range(N_C2)]\nfor t in threads: t.start()\nfor t in threads: t.join()\n```\n\nTSAN Report:\n```\nWARNING: ThreadSanitizer: data race (pid=657186)\n Read of size 8 at 0x7fffb40a0570 by thread T1:\n #0 thread_shutdown /cpython/./Modules/_threadmodule.c:2416:22 \n #1 cfunction_vectorcall_NOARGS /cpython/Objects/methodobject.c:508:24 \n #2 _PyObject_VectorcallTstate /cpython/./Include/internal/pycore_call.h:144:11 \n #3 PyObject_Vectorcall /cpython/Objects/call.c:327:12 \n #4 _Py_VectorCall_StackRefSteal /cpython/Python/ceval.c:726:11 \n #5 _PyEval_EvalFrameDefault /cpython/Python/generated_cases.c.h:4559:35 \n\n Previous write of size 8 at 0x7fffb40a0570 by main thread:\n #0 ThreadHandle_start /cpython/./Modules/_threadmodule.c:486:17 \n #1 do_start_new_thread /cpython/./Modules/_threadmodule.c:1919:9 \n #2 thread_PyThread_start_joinable_thread /cpython/./Modules/_threadmodule.c:2042:14 \n #3 cfunction_call /cpython/Objects/methodobject.c:564:18 \n #4 _PyObject_MakeTpCall /cpython/Objects/call.c:242:18 \n #5 _PyObject_VectorcallTstate /cpython/./Include/internal/pycore_call.h:142:16 \n #6 PyObject_Vectorcall /cpython/Objects/call.c:327:12 \n #7 _Py_VectorCall_StackRefSteal /cpython/Python/ceval.c:726:11 \n #8 _PyEval_EvalFrameDefault /cpython/Python/generated_cases.c.h:3686:35 \n\nSUMMARY: ThreadSanitizer: data race /cpython/./Modules/_threadmodule.c:2416:22 in thread_shutdown\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 -->\n### Linked PRs\n* gh-155085\n* gh-155123\n* gh-155124\n* gh-155125\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/Naserume","@type":"Person","name":"Naserume"},"datePublished":"2026-07-30T14:34:32Z","url":"https://github.com/python/cpython/issues/154937"}
| 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:1db9f52a-cabf-e227-d8f7-5f308bc79ad7 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B76A:399E4B:35DFCBA:477DA02:6B01F949 |
| html-safe-nonce | 2d57ad71340e961c804a1933183206c4a717bc360b870218967712b4675320ab |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCNzZBOjM5OUU0QjozNURGQ0JBOjQ3N0RBMDI6NkIwMUY5NDkiLCJ2aXNpdG9yX2lkIjoiODcwMDkwMDEzNzY1Mzg5MzQ0OSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | dacf03901b6d41a75142a917539074764858978fddc8701b873f39740fd74020 |
| hovercard-subject-tag | issue:5020840193 |
| 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/154937/issue_layout |
| twitter:image | https://opengraph.githubassets.com/4e791077310a99d15ed6a1a037bfd173022af5ef3e4a3bac6d4be715bdf80d05/python/cpython/issues/154937 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/4e791077310a99d15ed6a1a037bfd173022af5ef3e4a3bac6d4be715bdf80d05/python/cpython/issues/154937 |
| og:image:alt | Bug report Bug description: On a free-threaded build, ThreadHandle.ident is written and read under two different locks. ThreadHandle_start sets self->ident while holding the handle's own mutex (sel... |
| 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 | 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