Title: Crash when a callable iterator is shared between threads · Issue #161792 · python/cpython · GitHub
Open Graph Title: Crash when a callable iterator is shared between threads · Issue #161792 · python/cpython
X Title: Crash when a callable iterator is shared between threads · Issue #161792 · python/cpython
Description: Crash report What happened? In the free-threaded build, an iter(callable, sentinel) iterator that is used from several threads can crash the interpreter. calliter_iternext() calls it_callable and compares the result against it_sentinel t...
Open Graph Description: Crash report What happened? In the free-threaded build, an iter(callable, sentinel) iterator that is used from several threads can crash the interpreter. calliter_iternext() calls it_callable and c...
X Description: Crash report What happened? In the free-threaded build, an iter(callable, sentinel) iterator that is used from several threads can crash the interpreter. calliter_iternext() calls it_callable and c...
Opengraph URL: https://github.com/python/cpython/issues/161792
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Crash when a callable iterator is shared between threads","articleBody":"### Crash report\n\n### What happened?\n\nIn the free-threaded build, an `iter(callable, sentinel)` iterator that is used from several threads can crash the interpreter.\n\n`calliter_iternext()` calls `it_callable` and compares the result against `it_sentinel` through borrowed pointers. Another thread can concurrently clear those fields when the iterator is exhausted, or replace `it_sentinel` / `it_stop_exc` in `__setstate__()`, freeing an object that is still in use. Two threads that exhaust the iterator at the same time can also both `Py_DECREF` the same callable and sentinel.\n\nReproducer (segfaults within a few seconds on a free-threaded release build of main):\n\n```python\nimport threading\n\nN = 2000\n\ndef worker(its, barrier):\n barrier.wait()\n for it in its:\n for _ in it:\n pass\n\nfor _ in range(200):\n # Each iterator holds the only reference to its callable.\n its = [iter(lambda: None, None) for _ in range(N)]\n barrier = threading.Barrier(4)\n threads = [threading.Thread(target=worker, args=(its, barrier))\n for _ in range(4)]\n for t in threads:\n t.start()\n for t in threads:\n t.join()\n```\n\nThe default (GIL) build is not affected.\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\n_No response_\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-161793\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/nascheme","@type":"Person","name":"nascheme"},"datePublished":"2026-11-11T04:56:52Z","url":"https://github.com/python/cpython/issues/161792"}
| 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:47e8a733-33bd-393f-8372-2401de63622f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D3A4:1F2F:1D88405:2731082:6B021B7C |
| html-safe-nonce | bf9f6c265ea8b83506749a276bef0105802a8b136521e3ec0e88542f4653cb8b |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEM0E0OjFGMkY6MUQ4ODQwNToyNzMxMDgyOjZCMDIxQjdDIiwidmlzaXRvcl9pZCI6Ijg5MDE3OTc0MDA0MjgwMjY3NDgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | e60bdd328a4e9b6a222dccf46b34fb42427d1e2193128140a2ab918bf1456b46 |
| hovercard-subject-tag | issue:6321988985 |
| 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/161792/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f76bb52f8725dbf0fc0b59b768af8bf29079a1fb0d3688b797f51ae9057497cb/python/cpython/issues/161792 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f76bb52f8725dbf0fc0b59b768af8bf29079a1fb0d3688b797f51ae9057497cb/python/cpython/issues/161792 |
| og:image:alt | Crash report What happened? In the free-threaded build, an iter(callable, sentinel) iterator that is used from several threads can crash the interpreter. calliter_iternext() calls it_callable and c... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | nascheme |
| 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