Title: segfault in ga_iternext: sharing a types.GenericAlias iterator across threads double-frees under free-threading · Issue #154043 · python/cpython · GitHub
Open Graph Title: segfault in ga_iternext: sharing a types.GenericAlias iterator across threads double-frees under free-threading · Issue #154043 · python/cpython
X Title: segfault in ga_iternext: sharing a types.GenericAlias iterator across threads double-frees under free-threading · Issue #154043 · python/cpython
Description: Crash report What happened? On a free-threaded (--disable-gil) build, iterating a single, shared types.GenericAlias iterator (e.g. iter(list[int])) from multiple threads segfaults. ga_iternext (Objects/genericaliasobject.c) is a one-shot...
Open Graph Description: Crash report What happened? On a free-threaded (--disable-gil) build, iterating a single, shared types.GenericAlias iterator (e.g. iter(list[int])) from multiple threads segfaults. ga_iternext (Obj...
X Description: Crash report What happened? On a free-threaded (--disable-gil) build, iterating a single, shared types.GenericAlias iterator (e.g. iter(list[int])) from multiple threads segfaults. ga_iternext (Obj...
Opengraph URL: https://github.com/python/cpython/issues/154043
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"segfault in ga_iternext: sharing a types.GenericAlias iterator across threads double-frees under free-threading","articleBody":"# Crash report\n\n### What happened?\n\nOn a free-threaded (`--disable-gil`) build, iterating a single, shared `types.GenericAlias` iterator (e.g. `iter(list[int])`) from multiple threads segfaults. `ga_iternext` (`Objects/genericaliasobject.c`) is a one-shot iterator with no synchronization:\n\n```c\nstatic PyObject *\nga_iternext(PyObject *op)\n{\n gaiterobject *gi = (gaiterobject *)op;\n if (gi->obj == NULL) { // read gi->obj\n PyErr_SetNone(PyExc_StopIteration);\n return NULL;\n }\n gaobject *alias = (gaobject *)gi->obj;\n PyObject *starred_alias = Py_GenericAlias(alias->origin, alias->args); // use gi->obj\n if (starred_alias == NULL)\n return NULL;\n ((gaobject *)starred_alias)->starred = true;\n Py_SETREF(gi->obj, NULL); // Py_DECREF(gi->obj); gi->obj = NULL\n return starred_alias;\n}\n```\n\nTwo threads both observe `gi->obj != NULL`, both build the starred alias from it, and both reach `Py_SETREF(gi->obj, NULL)` (which is `tmp = gi->obj; gi->obj = NULL; Py_DECREF(tmp)`). With `gi->obj`'s refcount at 1 (the iterator is its only holder), the two `Py_DECREF`s free it twice — a double-free / refcount underflow — and the reads of `alias->origin`/`alias->args` become a use-after-free once the other thread frees it. The process crashes.\n\n### Reproducer\n\n```python\nimport threading\n\nNT = 16\n\ndef worker(it, barrier):\n barrier.wait()\n try:\n next(it)\n except StopIteration:\n pass\n\nfor _round in range(20000):\n shared = iter(list[int]) # ONE shared GenericAlias iterator; gi->obj refcount == 1\n bar = threading.Barrier(NT)\n threads = [threading.Thread(target=worker, args=(shared, bar)) for _ in range(NT)]\n for t in threads:\n t.start()\n for t in threads:\n t.join()\nprint(\"done\")\n```\n\n`PYTHON_GIL=0 ./python repro.py` segfaults within the first few rounds — 5/5 runs on both a debug and a release (`-O0`, no sanitizer) free-threaded build, so it is neither debug-only nor sanitizer-only:\n\n```\nThread NNNN received signal SIGSEGV, Segmentation fault.\n#0 ga_iternext (op=...) at Objects/genericaliasobject.c:952 (Py_SETREF(gi->obj, NULL))\n#1 builtin_next Python/bltinmodule.c:1776\n#2 _Py_BuiltinCallFast_StackRef Python/ceval.c:817\n#3 _PyEval_EvalFrameDefault Python/generated_cases.c.h:2510 (next(it))\n...\n```\n\nThreadSanitizer reports the same as a data race on `gi->obj` (`WARNING: ThreadSanitizer: data race... in ga_iternext`, exit 66).\n\n### Suggested fix\n\nConsume `gi->obj` atomically so exactly one thread takes it:\n\n```c\nPyObject *obj = _Py_atomic_exchange_ptr(&gi->obj, NULL);\nif (obj == NULL) {\n PyErr_SetNone(PyExc_StopIteration);\n return NULL;\n}\ngaobject *alias = (gaobject *)obj;\nPyObject *starred_alias = Py_GenericAlias(alias->origin, alias->args);\nif (starred_alias == NULL) {\n Py_DECREF(obj);\n return NULL;\n}\n((gaobject *)starred_alias)->starred = true;\nPy_DECREF(obj);\nreturn starred_alias;\n```\n\n(Or wrap the body in `Py_BEGIN_CRITICAL_SECTION(gi)`.) This matches the \"consume once\" semantics and keeps the GIL build unchanged.\n\nPer the iterator free-threading strategy (gh-124397), concurrent iteration may return duplicate or skipped values or raise — but it must not crash; this crashes. Distinct from gh-153298, which is the `GenericAlias.__parameters__` lazy-init race, not the iterator.\n\n*(Found by `fusil --tsan`, a ThreadSanitizer fuzzer; crash confirmed by re-running the reproducer without a sanitizer on a plain free-threaded build. Draft and reproducer by Claude Code, minimized and reviewed by hand.)*\n\n\n### CPython versions tested on:\n\nCPython main branch\n\n### Operating systems tested on:\n\nLinux\n\n### Output from running 'python -VV' on the command line:\n\nPython 3.16.0a0 free-threading build (heads/main:a1d580430c8, Jul 18 2026, 20:36:44) [Clang 21.1.8 (6ubuntu1)]\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-154108\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/devdanzin","@type":"Person","name":"devdanzin"},"datePublished":"2026-07-19T03:07:31Z","url":"https://github.com/python/cpython/issues/154043"}
| 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:2ec853b5-25de-fb93-c838-2de26bfaf75d |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C530:2BE220:36C2582:48FE07D:6B0211F5 |
| html-safe-nonce | 7a4881d995a1cc659ea58d1a9da36fce00ed367000cf5167facf60be862a1953 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNTMwOjJCRTIyMDozNkMyNTgyOjQ4RkUwN0Q6NkIwMjExRjUiLCJ2aXNpdG9yX2lkIjoiNTQzMTU3MjI4MTc3NDE4MjkwMSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | e0cd54c63f18268cd9450cb8a830458f17f1503c11f507f0f0461cd9bb9658f8 |
| hovercard-subject-tag | issue:4921049479 |
| 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/154043/issue_layout |
| twitter:image | https://opengraph.githubassets.com/5075606011f378a77994fc4efa82e70882a0a862baa744302f276ac99003c630/python/cpython/issues/154043 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/5075606011f378a77994fc4efa82e70882a0a862baa744302f276ac99003c630/python/cpython/issues/154043 |
| og:image:alt | Crash report What happened? On a free-threaded (--disable-gil) build, iterating a single, shared types.GenericAlias iterator (e.g. iter(list[int])) from multiple threads segfaults. ga_iternext (Obj... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | devdanzin |
| 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