{{ message }}
gh-156310: Make the iter() sequence fallback iterator safe in free-threaded build - #156311
Merged
Conversation
…ree-threaded build Sharing a single PySeqIter between threads could double-DECREF the underlying sequence and use it after free. Apply the same approach as listiter/tupleiter/reversed (pythongh-120608): use relaxed atomics for it_index with -1 as the exhaustion sentinel, and in the free-threaded build keep the reference to the sequence until the iterator is deallocated. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
akx
force-pushed
the
gh-156310-seqiter-ft
branch
from
September 1, 2026 13:33
f3ac8e1 to
6f4e7b2
Compare
* The default build has a bug if next() can re-enter, handle that. * Use Py_CLEAR to avoid decrementing a stale pointer. * Check index value using relaxed load before advancing the index. This prevents revival of an exhaused iterator. * Add unit test for next() re-entrancy case. * Update NEWS file.
Member
Contributor
Author
|
Thanks @nascheme, looks great to me! 🎉 As a procedural aside, does this need a backport if it now also fixes a double decref in the default build? |
Member
kumaraditya303
approved these changes
Sep 4, 2026
clin1234
pushed a commit
to clin1234/cpython
that referenced
this pull request
Sep 12, 2026
…ree-threaded build (python#156311) Co-authored-by: Neil Schemenauer <nas@arctrix.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Sharing a single
PySeqIterbetween threads could double-DECREF the underlying sequence on the exhaustion path and use the sequence after free while another thread was insidePySequence_GetItem. (See #156310.)This applies the same approach used for
listiter,tupleiterandreversed(gh-120608, #120971).it_indexis accessed withFT_ATOMIC_LOAD/STORE_SSIZE_RELAXEDand-1becomes the exhaustion sentinel.next()call can free the sequence while another thread is using it.__length_hint__,__reduce__and__setstate__now key off the index sentinel so that an exhausted iterator behaves the same in both builds, matching whatreverseddoes since gh-120608: Make reversed iterator work with free-threading #120971).listiter/tupleiter/reversed.New test in
Lib/test/test_free_threading/test_iteration.py. It uses a small sequence and many rounds so that many threads reach the racy exhaustion path simultaneously, and asserts the sequence's refcount is intact afterwards — the double-DECREF does not always crash, but it reliably shows up as a sagging refcount. On 53d2e14 (main) the test segfaults a free-threaded build.Note
callable_iterator(backingiter(callable, sentinel)) has the same defect but no index field to reuse as a sentinel. Should also be fixed, but didn't feel like it's in-scope here.PySeqIteracross threads use-after-frees the sequence under free-threading #156310