Title: Data race on a shared `types.GenericAlias` iterator in `ga_iter_reduce` under free-threading (follow-up to gh-154043) · Issue #154916 · python/cpython · GitHub
Open Graph Title: Data race on a shared `types.GenericAlias` iterator in `ga_iter_reduce` under free-threading (follow-up to gh-154043) · Issue #154916 · python/cpython
X Title: Data race on a shared `types.GenericAlias` iterator in `ga_iter_reduce` under free-threading (follow-up to gh-154043) · Issue #154916 · python/cpython
Description: Bug report Bug description: This is a follow-up to #154043 (segfault in ga_iternext when a types.GenericAlias iterator is shared across threads), which was fixed by gh-154108 by consuming gi->obj atomically in ga_iternext: cpython/Object...
Open Graph Description: Bug report Bug description: This is a follow-up to #154043 (segfault in ga_iternext when a types.GenericAlias iterator is shared across threads), which was fixed by gh-154108 by consuming gi->obj a...
X Description: Bug report Bug description: This is a follow-up to #154043 (segfault in ga_iternext when a types.GenericAlias iterator is shared across threads), which was fixed by gh-154108 by consuming gi->ob...
Opengraph URL: https://github.com/python/cpython/issues/154916
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Data race on a shared `types.GenericAlias` iterator in `ga_iter_reduce` under free-threading (follow-up to gh-154043)","articleBody":"# Bug report\n\n### Bug description:\n\nThis is a follow-up to #154043 (segfault in `ga_iternext` when a `types.GenericAlias` iterator is shared across threads), which was fixed by gh-154108 by consuming `gi->obj` atomically in `ga_iternext`:\n\nhttps://github.com/python/cpython/blob/8b048eb35eb7f83dbff86dd11c7fdb37caab9e7a/Objects/genericaliasobject.c#L942-L963\n\nThat fix only covers `ga_iternext`. The iterator's `__reduce__` implementation, `ga_iter_reduce`, still reads the same `gi->obj` field with plain (non-atomic) loads and no critical section:\n\nhttps://github.com/python/cpython/blob/8b048eb35eb7f83dbff86dd11c7fdb37caab9e7a/Objects/genericaliasobject.c#L990-L1004\n\nSo sharing one `iter(list[int])` across threads and calling `__reduce__` concurrently with `next()` is still a data race on `gi->obj`, the same field the fix made atomic in `ga_iternext`. `ga_iternext` does `_Py_atomic_exchange_ptr(&gi->obj, NULL)` (atomic write) while `ga_iter_reduce` does `if (gi->obj) return Py_BuildValue(\"N(O)\", iter, gi->obj);` (plain read). \n\n```python\nfrom threading import Thread, Barrier\n_shared_iter = iter(list[int])\n\ndef chain1_thread():\n for _ in range(20000):\n try:\n next(_shared_iter)\n except StopIteration:\n pass\n\ndef chain2_thread():\n for _ in range(20000):\n try:\n _shared_iter.__reduce__()\n except Exception:\n pass\n\nN_C1 = 4\nN_C2 = 4\nbarrier = Barrier(N_C1 + N_C2)\n\ndef _c1():\n barrier.wait()\n chain1_thread()\n\ndef _c2():\n barrier.wait()\n chain2_thread()\n\nthreads = [Thread(target=_c1) for _ in range(N_C1)]\nthreads += [Thread(target=_c2) 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=650295)\n Write of size 8 at 0x7fffb6161af0 by thread T1:\n #0 _Py_atomic_exchange_ptr /home/fuzz_cpython/cpython-latest/cpython/./Include/cpython/pyatomic_gcc.h:195:10 \n #1 ga_iternext /home/fuzz_cpython/cpython-latest/cpython/Objects/genericaliasobject.c:946:21 \n #2 builtin_next /home/fuzz_cpython/cpython-latest/cpython/Python/bltinmodule.c:1776:11 \n #3 cfunction_vectorcall_FASTCALL /home/fuzz_cpython/cpython-latest/cpython/Objects/methodobject.c:449:24 \n #4 _PyObject_VectorcallTstate /home/fuzz_cpython/cpython-latest/cpython/./Include/internal/pycore_call.h:144:11 \n #5 PyObject_Vectorcall /home/fuzz_cpython/cpython-latest/cpython/Objects/call.c:327:12 \n #6 _Py_VectorCallInstrumentation_StackRefSteal /home/fuzz_cpython/cpython-latest/cpython/Python/ceval.c:768:11 \n #7 _PyEval_EvalFrameDefault /home/fuzz_cpython/cpython-latest/cpython/Python/generated_cases.c.h:1906:35\n...\n\n Previous read of size 8 at 0x7fffb6161af0 by thread T8:\n #0 ga_iter_reduce /cpython/Objects/genericaliasobject.c:1000:13 \n #1 _PyEval_EvalFrameDefault /cpython/Python/generated_cases.c.h:4330:35 \n...\n\nSUMMARY: ThreadSanitizer: data race /cpython/./Include/cpython/pyatomic_gcc.h:195:10 in _Py_atomic_exchange_ptr\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-154944\n* gh-155476\n* gh-155634\n* gh-155705\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/Naserume","@type":"Person","name":"Naserume"},"datePublished":"2026-07-30T10:15:14Z","url":"https://github.com/python/cpython/issues/154916"}
| 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:bb907cf3-2b98-c3f3-fece-549c9d02b388 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E0A6:16E4ED:A2B8FB:CC41FE:6B021BCC |
| html-safe-nonce | e987a6ac16192ce8d6703c659b56481666cdd84509ea6b32ab10c6f6f4cddd09 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFMEE2OjE2RTRFRDpBMkI4RkI6Q0M0MUZFOjZCMDIxQkNDIiwidmlzaXRvcl9pZCI6IjQwNjg2NjUxOTE3OTYxODYwNjAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 5e92972e2167acd050cd3de481ae67dab17db4abaa750d4ba59f7ed053537e4c |
| hovercard-subject-tag | issue:5018662118 |
| 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/154916/issue_layout |
| twitter:image | https://opengraph.githubassets.com/64a01f3d737fb7e14ed8ca1fcc07d6904ad78f32278723f376c36620229626c9/python/cpython/issues/154916 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/64a01f3d737fb7e14ed8ca1fcc07d6904ad78f32278723f376c36620229626c9/python/cpython/issues/154916 |
| og:image:alt | Bug report Bug description: This is a follow-up to #154043 (segfault in ga_iternext when a types.GenericAlias iterator is shared across threads), which was fixed by gh-154108 by consuming gi->obj a... |
| 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