fix: for-in snapshots iteration length at loop entry (#491)#516
Merged
Conversation
`for x in xs` re-read the sequence's *live* length on every ITER_NEXT, so a body that appended to xs looped forever (unbounded memory, OOM) and one that removed from the front skipped elements past the shrink. SPEC left mutation-during-iteration undefined. make_iter_state now records the length at loop entry as a third element of the iterator state; ITER_NEXT (interpreter) and jit_helper_iter_next (JIT) bound the iteration by min(snapshot, live length). Appending can no longer extend the loop; removing stops at the live length instead of reading a freed slot. `for` over a mutated sequence is now well-defined: it visits exactly the indices 0..N-1 that existed at entry, read live. Applies to both tiers and to list comprehensions (same ITER_SETUP/ITER_NEXT path). SPEC updated. Regression: suite section [117] (test_for_in_mutation.eigs, 9 checks — append-terminates, remove-is-OOB-safe, JIT-tier snapshot, buffer, empty, listcomp). Release suite green (2655/2655). Closes #491 Co-Authored-By: Claude Opus 4.8 (1M context) <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.
for x in xsre-read the sequence's live length on everyITER_NEXT, so mutatingxsin the loop body was a footgun:SPEC left mutation-during-iteration undefined.
Fix
make_iter_staterecords the length at loop entry as a third element of the iterator state.ITER_NEXT(interpreter) andjit_helper_iter_next(JIT) now bound the iteration bymin(snapshot, live length):forover a mutated sequence is now well-defined: it visits exactly the indices0..N-1that existed at entry, read live. Applies to both the interpreter and JIT tiers and to list comprehensions (sameITER_SETUP/ITER_NEXTpath). SPEC updated.Notes
make_num/decrefper loop setup (not per iteration) — negligible; the hot per-iteration path (NUM_REUSE index bump) is unchanged.jit_helper_iter_nextrather than inlining the count, so the single helper fix covers native code; verified identical output JIT-on vsEIGS_JIT_OFF.Tests
New suite section [117] —
test_for_in_mutation.eigs, 9 checks (append-terminates, remove-is-OOB-safe, JIT-tier 500-iter snapshot, buffer, empty, listcomp, plain iteration).main(with batch-2b): ASan +detect_leaks=12669/2669, leak tally 0.Closes #491
🤖 Generated with Claude Code