René's URL Explorer Experiment


Title: Sharing `PySeqIter` across threads use-after-frees the sequence under free-threading · Issue #156310 · python/cpython · GitHub

Open Graph Title: Sharing `PySeqIter` across threads use-after-frees the sequence under free-threading · Issue #156310 · python/cpython

X Title: Sharing `PySeqIter` across threads use-after-frees the sequence under free-threading · Issue #156310 · python/cpython

Description: Crash report What happened? On a free-threaded build, advancing a single, shared iterator over a legacy sequence (a type implementing __getitem__/sq_item but not __iter__/tp_iter) from multiple threads use-after-frees the underlying sequ...

Open Graph Description: Crash report What happened? On a free-threaded build, advancing a single, shared iterator over a legacy sequence (a type implementing __getitem__/sq_item but not __iter__/tp_iter) from multiple thr...

X Description: Crash report What happened? On a free-threaded build, advancing a single, shared iterator over a legacy sequence (a type implementing __getitem__/sq_item but not __iter__/tp_iter) from multiple thr...

Opengraph URL: https://github.com/python/cpython/issues/156310

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Sharing `PySeqIter` across threads use-after-frees the sequence under free-threading","articleBody":"# Crash report\n\n### What happened?\n\nOn a free-threaded build, advancing a single, shared iterator over a legacy sequence (a type implementing `__getitem__`/`sq_item` but not `__iter__`/`tp_iter`) from multiple threads use-after-frees the underlying sequence object. \n\nThe defect is in the fallback iterator itself, so it affects pure-Python classes and C extension types alike. `list` (whose `list_iterator` was hardened for free-threading in https://github.com/python/cpython/pull/115605) is clean.\n\n`iter_iternext()` in `Objects/iterobject.c` has no synchronization:\n \nhttps://github.com/python/cpython/blob/9cbd578e38a5ef59f9462b61d1e03338b208cba6/Objects/iterobject.c#L52-L83\n\n\nThe iterator owns exactly one reference to `it_seq` and releases it exactly once, on exhaustion. With the GIL that invariant holds because only one thread is ever inside `tp_iternext`. Without the GIL:\n\n1. **Double DECREF on exhaustion.** Several threads read the same non-`NULL` `it->it_seq`, all observe out-of-bounds from `PySequence_GetItem`, and each executes the `Py_DECREF(seq)`. The single owned reference is released N times, so the sequence can be freed while other code is still holding refs to it.\n2. **Borrowed pointer outliving the object.** Thread A is inside `PySequence_GetItem(seq, ...)` (an arbitrarily long call) and thread B takes the exhaustion path and drops what may be the last reference. A then operates on freed memory.\n\n(There's also a data race for `index` that #115605 fixed for lists with atomic stores, but per #124397, that's acceptable.)\n\n`calliter_iternext` has the same defect: `it_callable`/`it_sentinel` are cleared with non-atomic `Py_CLEAR` on the exhaustion path and read without protection, so a shared `callable_iterator` can double-release and use-after-free them the same way.\n\nThere's prior art for this issue class in #154043, #154108, #154130 and the same fixes can be applied here.\n\n### Reproducer\n\n```python\nimport sys, threading\n\nTHREADS = 16\nROUNDS = 2000\n\n\nclass PySeq:  # sq_item only -> iter() falls back to PySeqIter\n    def __init__(self, n):\n        self.n = n\n\n    def __len__(self):\n        return self.n\n\n    def __getitem__(self, i):\n        if i >= self.n:\n            raise IndexError(i)\n        return i\n\n\ndef drain(it, barrier):\n    barrier.wait()\n    while True:\n        try:\n            next(it)\n        except StopIteration:\n            return\n        except Exception:\n            return  # torn it_index can walk off the end; benign\n\n\ndef hammer(make):\n    for _ in range(ROUNDS):\n        it = make()\n        barrier = threading.Barrier(THREADS)\n        ws = [\n            threading.Thread(target=drain, args=(it, barrier))\n            for _ in range(THREADS)\n        ]\n        for w in ws:\n            w.start()\n        for w in ws:\n            w.join()\n        del it\n\n\nseq = PySeq(4)\nprint(\"gil enabled:\", sys._is_gil_enabled())\nprint(\"iter type:\", type(iter(seq)).__name__)\nbefore = sys.getrefcount(seq)\nhammer(lambda: iter(seq))\nafter = sys.getrefcount(seq)\nprint(\n    f\"before={before} after={after} -> {'CORRUPTED' if after != before else 'ok'}\"\n)\n```\n\n#### Observed results\n\nAll on macOS / arm64 (Apple Silicon):\n\n| Build | `PySeq` (`PySeqIter`) | `[0, 1, 2, 3]` (`list_iterator`) |\n| --- | --- | --- |\n| main (`3.16.0a0`, free-threading debug build) | SIGSEGV | clean, refcount ok |\n| 3.14.6 free-threaded | SIGSEGV (3/3 runs) | clean (3/3 runs) |\n| 3.13.8 free-threaded | SIGSEGV (3/3 runs) | not run |\n| 3.14.6 GIL-enabled | clean, refcount ok | clean |\n| 3.15.x free-threaded | refcount bad (on some runs; see below) | not run |\n\nDepending on allocator timing the failure may show up as refcount corruption, type confusion in\nunrelated code once the freed memory is reused, or as SIGSEGV, or nothing at all:\n\n```\n~/Desktop $ uv run --python=3.15t --isolated repro.py\ngil enabled: False\niter type: iterator\n~/Desktop $ uv run --python=3.15t --isolated repro.py\ngil enabled: False\niter type: iterator\nbefore=2 after=1152921504606846749 -> CORRUPTED\n```\n\n### CPython versions tested on:\n\nCPython main branch, 3.13, 3.14, 3.15\n\n### Operating systems tested on:\n\nmacOS\n\n### Output from running 'python -VV' on the command line:\n\nPython 3.16.0a0 free-threading build (heads/main:53d2e14a308, Aug 24 2026, 09:57:36) [Clang 17.0.0 (clang-1700.3.19.1)]\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-156311\n* gh-162006\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/akx","@type":"Person","name":"akx"},"datePublished":"2026-08-24T08:20:09Z","url":"https://github.com/python/cpython/issues/156310"}

react-profiling0
react-import-mapreact,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-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:a321e447-efb7-a508-cb68-d5ecbf5b946f
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id804E:2A7C1C:18D575F:20CF09B:6B020F7D
html-safe-nonce73560e04259dc1003e20c46687f315f392eaf1a2f22a08d6dedd865f6b099596
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MDRFOjJBN0MxQzoxOEQ1NzVGOjIwQ0YwOUI6NkIwMjBGN0QiLCJ2aXNpdG9yX2lkIjoiNDU3MDU4NTMzNDk3ODU4MDM0OSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmacb79862208c4e5b60d6f15e00ebcfbf3a3ec80accc72951b8c7ca2573f3bec32e
hovercard-subject-tagissue:5232596127
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/156310/issue_layout
twitter:imagehttps://opengraph.githubassets.com/70d11b9244125d605e5b43ab4093eb571abd12a2433c5f23d66b05641fd73405/python/cpython/issues/156310
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/70d11b9244125d605e5b43ab4093eb571abd12a2433c5f23d66b05641fd73405/python/cpython/issues/156310
og:image:altCrash report What happened? On a free-threaded build, advancing a single, shared iterator over a legacy sequence (a type implementing __getitem__/sq_item but not __iter__/tp_iter) from multiple thr...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameakx
hostnamegithub.com
expected-hostnamegithub.com
None87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456
turbo-cache-controlno-preview
go-importgithub.com/python/cpython git https://github.com/python/cpython.git
octolytics-dimension-user_id1525981
octolytics-dimension-user_loginpython
octolytics-dimension-repository_id81598961
octolytics-dimension-repository_nwopython/cpython
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id81598961
octolytics-dimension-repository_network_root_nwopython/cpython
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release423dbb54c5e7bfecc838eb387c1435eb3d0e760d
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/python/cpython/issues/156310#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F156310
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F156310
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=python%2Fcpython
Reloadhttps://github.com/python/cpython/issues/156310
Reloadhttps://github.com/python/cpython/issues/156310
Reloadhttps://github.com/python/cpython/issues/156310
pythonhttps://github.com/python
cpythonhttps://github.com/python/cpython
Codehttps://github.com/python/cpython
Issues5k+ (5k+)https://github.com/python/cpython/issues
Pull requests2.4k (2.4k)https://github.com/python/cpython/pulls
Actionshttps://github.com/python/cpython/actions
Projectshttps://github.com/python/cpython/projects
Security and qualityhttps://github.com/python/cpython/security
Insightshttps://github.com/python/cpython/insights
https://github.com/python/cpython
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/156310
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/156310
Sharing PySeqIter across threads use-after-frees the sequence under free-threadinghttps://github.com/python/cpython/issues/156310#top
interpreter-core(Objects, Python, Grammar, and Parser dirs)https://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22interpreter-core%22
topic-free-threadinghttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-free-threading%22
type-crashA hard crash of the interpreter, possibly with a core dumphttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-crash%22
https://github.com/akx
akxhttps://github.com/akx
on Aug 24, 2026https://github.com/python/cpython/issues/156310#issue-5232596127
#115605https://github.com/python/cpython/pull/115605
cpython/Objects/iterobject.chttps://github.com/python/cpython/blob/9cbd578e38a5ef59f9462b61d1e03338b208cba6/Objects/iterobject.c#L52-L83
9cbd578https://github.com/python/cpython/commit/9cbd578e38a5ef59f9462b61d1e03338b208cba6
#115605https://github.com/python/cpython/pull/115605
#124397https://github.com/python/cpython/issues/124397
#154043https://github.com/python/cpython/issues/154043
#154108https://github.com/python/cpython/pull/154108
#154130https://github.com/python/cpython/issues/154130
gh-156310: Make the iter() sequence fallback iterator safe in free-threaded build #156311https://github.com/python/cpython/pull/156311
[3.15] gh-156310: Make the iter() sequence fallback iterator safe in free-threaded build (GH-156311) #162006https://github.com/python/cpython/pull/162006
akxhttps://github.com/akx
type-crashA hard crash of the interpreter, possibly with a core dumphttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-crash%22
on Aug 24, 2026https://github.com/python/cpython/issues/156310#event-29900157327
aiskhttps://github.com/aisk
interpreter-core(Objects, Python, Grammar, and Parser dirs)https://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22interpreter-core%22
topic-free-threadinghttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-free-threading%22
on Aug 24, 2026https://github.com/python/cpython/issues/156310#event-29906657474
akxhttps://github.com/akx
on Sep 1, 2026https://github.com/python/cpython/issues/156310#event-30348954066
pythongh-156310: Make the iter() sequence fallback iterator safe in f…https://github.com/akx/cpython/commit/6f4e7b2eaf5dc92edf4d39c36db62b07c6b14f50
pythongh-156310https://github.com/python/cpython/issues/156310
6f4e7b2https://github.com/akx/cpython/commit/6f4e7b2eaf5dc92edf4d39c36db62b07c6b14f50
kumaraditya303https://github.com/kumaraditya303
on Sep 6, 2026https://github.com/python/cpython/issues/156310#event-30637639445
gh-156310: Make the iter() sequence fallback iterator safe in free-th…https://github.com/python/cpython/commit/b707ce61968161a444f18e45910d1a25f1181ab7
gh-156310https://github.com/python/cpython/issues/156310
b707ce6https://github.com/python/cpython/commit/b707ce61968161a444f18e45910d1a25f1181ab7
clin1234https://github.com/clin1234
on Sep 12, 2026https://github.com/python/cpython/issues/156310#event-31023917894
pythongh-156310: Make the iter() sequence fallback iterator safe in f…https://github.com/clin1234/cpython/commit/bff281accf489dcf79f89005614f18d6b880dd2d
pythongh-156310https://github.com/python/cpython/issues/156310
bff281ahttps://github.com/clin1234/cpython/commit/bff281accf489dcf79f89005614f18d6b880dd2d
kumaraditya303https://github.com/kumaraditya303
completedhttps://github.com/python/cpython/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Acompleted
on Sep 12, 2026https://github.com/python/cpython/issues/156310#event-31025185222
rjpowerhttps://github.com/rjpower
on Oct 26, 2026https://github.com/python/cpython/issues/156310#event-34305182064
boil: test_iter drops its refcount and finalizer-timing checkshttps://github.com/rjpower/cpython/commit/51b5b918adbc570a49ff93650ec17aaa81371a52
51b5b91https://github.com/rjpower/cpython/commit/51b5b918adbc570a49ff93650ec17aaa81371a52
boil: test_free_threading.test_iteration drops the sequence refcount …https://github.com/rjpower/cpython/commit/bf2aa84c859665ef5ff9f177d5275c180785ab1a
bf2aa84https://github.com/rjpower/cpython/commit/bf2aa84c859665ef5ff9f177d5275c180785ab1a
kumaraditya303https://github.com/kumaraditya303
on Nov 15, 2026https://github.com/python/cpython/issues/156310#event-36176973512
[3.15] gh-156310: Make the iter() sequence fallback iterator safe in …https://github.com/python/cpython/commit/6a9fa39c55ca104ddefbd74d372ae3428e280810
gh-156310https://github.com/python/cpython/issues/156310
6a9fa39https://github.com/python/cpython/commit/6a9fa39c55ca104ddefbd74d372ae3428e280810
Sign up for freehttps://github.com/signup?return_to=https://github.com/python/cpython/issues/156310
Sign in to commenthttps://github.com/login?return_to=https://github.com/python/cpython/issues/156310
interpreter-core(Objects, Python, Grammar, and Parser dirs)https://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22interpreter-core%22
topic-free-threadinghttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-free-threading%22
type-crashA hard crash of the interpreter, possibly with a core dumphttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-crash%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.