Skip to content

gh-129139: Fix __setstate__ on exhausted list iterators in free-threaded build#146269

Open
ytang928 wants to merge 2 commits intopython:mainfrom
ytang928:gh-129139-fix-listiter-setstate
Open

gh-129139: Fix __setstate__ on exhausted list iterators in free-threaded build#146269
ytang928 wants to merge 2 commits intopython:mainfrom
ytang928:gh-129139-fix-listiter-setstate

Conversation

@ytang928
Copy link

In free-threaded builds, exhausted list iterators signal exhaustion by
setting it_index to -1 without clearing it_seq to NULL (to avoid
expensive atomic refcount operations on the hot path). The existing
it_seq != NULL guard in listiter_setstate and listreviter_setstate
allowed __setstate__ to revive an exhausted iterator by resetting
it_index to a valid value.

This adds an it_index >= 0 check so that __setstate__ is a no-op on
exhausted iterators, consistent with GIL-enabled build behavior.

The same fix is applied to both listiter_setstate and listreviter_setstate,
which share the same pattern.

…-threaded build

In free-threaded builds, exhausted list iterators signal exhaustion by
setting it_index to -1 without clearing it_seq to NULL. The existing
it_seq != NULL guard in listiter_setstate and listreviter_setstate
allowed __setstate__ to revive an exhausted iterator by resetting
it_index to a valid value.

Add an it_index >= 0 check so that __setstate__ is a no-op on
exhausted iterators, consistent with GIL-enabled build behavior.
@python-cla-bot
Copy link

python-cla-bot bot commented Mar 22, 2026

All commit authors signed the Contributor License Agreement.

CLA signed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant