Fix cached Promise result being reused across for…of loop iterations with await#2304
Merged
Conversation
…for...of loop Co-authored-by: lahma <171892+lahma@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix Jint returning cached Promise result in for…of loop
Fix cached Promise result being reused across for…of loop iterations with await
Mar 3, 2026
lahma
marked this pull request as ready for review
March 3, 2026 10:57
lahma
approved these changes
Mar 3, 2026
This was referenced Mar 7, 2026
This was referenced Jun 8, 2026
This was referenced Jun 29, 2026
This was referenced Jul 7, 2026
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.
When
awaitis used inside afor...ofloop body, all iterations after the first return the same resolved value as the first iteration instead of re-evaluating per-iteration.Root causes (both in
JintForInForOfStatement.BodyEvaluation)Stale
_completedAwaitscache:AsyncFunctionInstance._completedAwaitscachesawaitresults keyed by AST node. Since the sameawaitexpression node is shared across every loop iteration, the first iteration's result was returned for all subsequent ones. The other loop statement types (JintForStatement,JintWhileStatement,JintDoWhileStatement) already clear this cache at iteration boundaries —for...ofdid not.Iterator state not preserved across suspension: When the async function suspended at an
awaitin the loop body, no iterator state was saved (only generators savedForOfSuspendData). On resume,HeadEvaluationcreated a fresh iterator from position 0, restarting the loop from the beginning rather than continuing from the suspended iteration.Changes
JintForInForOfStatement.BodyEvaluation: At the top of thewhileloop, clear_completedAwaitsfor async functions when!_isResuming— identical to the pattern in the other loop statement implementations.JintForInForOfStatement.BodyEvaluation: Before executing the loop body with a sync iterator, saveForOfSuspendDatainto the async function's suspend data dictionary (iterator position, current value, iteration env, accumulated value). On resume,ExecuteInternalfinds this data via the existingsuspendable.Data.TryGetpath and restores the correct iteration state.Original prompt
🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.