gh-156310: Make the iter() sequence fallback iterator safe in free-threaded build - #156311
Open
akx wants to merge 1 commit into
Open
gh-156310: Make the iter() sequence fallback iterator safe in free-threaded build#156311akx wants to merge 1 commit into
akx wants to merge 1 commit into
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>
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