Fix top-level await of a .NET Task in a module failing with "Pending"#2665
Merged
Conversation
Modules.Import evaluates a top-level-await module by draining the event loop until the module's capability promise settles. The drain used a tight loop (max 10 iterations, no wait) that completes in microseconds: when the top-level await awaits a .NET Task<T> (task interop), the Task resolves on a ThreadPool thread milliseconds later, so the queue is empty on every iteration and the loop gives up and throws: InvalidOperationException: Module evaluation did not return a fulfilled promise: Pending Replace the spin with a poll that waits on the promise's completion event with a short interval (mirroring UnwrapIfPromise), bounded by Constraints.PromiseTimeout, and claims the waiting thread so background Task completions don't race to run JavaScript on the engine. Purely microtask-settled modules (the Test262 case) still settle on the first drain, so there is no added latency for them. Fixes sebastienros#2663 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
lahma
enabled auto-merge (squash)
July 13, 2026 12:41
This was referenced Jul 15, 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.
Problem
Fixes #2663.
When a module uses top-level
await(TLA) to await a .NETTask<T>returned from a registered method (withExperimentalFeature.TaskInterop),engine.Modules.Import()throws:Root cause
EvaluateModuledrains the event loop until the module's top-level capability promise settles, but it did so with a tight loop (max 10 iterations, no wait) that completes in microseconds. ATask<T>awaited at the top level completes on a ThreadPool thread and enqueues its resolve job only milliseconds later, so the queue is empty on every iteration; the loop hits its empty-queue limit and gives up while the promise is stillPending, then throws.UnwrapIfPromise(used by, e.g., theawait usingdisposal path) does not have this problem because it polls on the promise's completion event with a short interval, giving the background Task time to complete.Fix
Replace the spin loop with a poll that mirrors
UnwrapIfPromise: drain continuations, then wait on the promise's completion event with a 10 ms interval, bounded byConstraints.PromiseTimeout. The draining thread is claimed via_waitingThreadIdso background Task completions don't race to run JavaScript on the engine. The shared logic lives in a newEngine.DrainEventLoopUntilSettledhelper.Modules whose top-level await settles purely through microtasks (the entire Test262 corpus) still settle on the first drain call, so there is no added latency for them — confirmed by the timings below.
Tests
ShouldResolveTopLevelAwaitOfDelayedTaskInModule— the exact repro from the issue (assertsanswer === 42).ShouldPropagateRejectionOfTopLevelAwaitedTaskInModule— a faulted top-level-awaited Task surfaces as aJavaScriptExceptionout ofImportrather than a spurious pending state.Both failed against
main(...did not return a fulfilled promise: Pending) and pass with the fix, onnet10.0andnet472.Regression coverage (all green):
Jint.Testssuite: 3539 (net10.0) / 3476 (net472)top-level-await: 273 passed in 186 msdynamic-import: 1906 passed in 619 ms