Skip to content

[JSC] An evaluation error does not end a module body that is suspended at a top-level await - #845

Open
robobun wants to merge 1 commit into
mainfrom
robobun/097ea4cd/tla-cycle-resume
Open

robobun wants to merge 1 commit into
mainfrom
robobun/097ea4cd/tla-cycle-resume

Conversation

@robobun

@robobun robobun commented Oct 19, 2026

Copy link
Copy Markdown
Collaborator

Supersedes #281.

Problem

  • A module suspended at a top-level await in an unfinished import cycle never continues when a later module of that Evaluate() throws. Its remaining statements and finally blocks never run, unlike ECMA-262 and Node.js v26.3.0.
  • Step 9 gives each record on the stack the error. CyclicModuleRecord::setEvaluationError then released the executable, and JSModuleRecord::evaluate threw the error into the resume job.
  • No user reported it. Bun 1.3.13 ran the body, 1.3.14 does not.

Fix

  • JSModuleRecord::didReceiveEvaluationError releases the executable only when the body has not started.
  • evaluate() enters the body when the record has an executable. Without one it throws the stored error.
  • Correct: the body is an async body (AsyncBlockStart), and AsyncModuleExecutionFulfilled step 1 returns for an evaluated module.
  • Verified: new JSTests/modules/tla-cycle-suspended-member.js (fails on main), JSTests/modules, the module stress tests.

Background

Downsides

  • A record suspended with an error keeps its code until its body ends: 1 more module executable in $vm.codeBlockCensus(). Never resumed, a shared executable keeps it.
  • A record that gets an error runs 8 to 12 more instructions. Release .text grows 64 bytes.
  • Code after such an await now runs, also after import() rejected.
Notes

Repro (jsc -m main.js):

// a.js:    import "./t.js"; import "./x.js";
// t.js:    import "./a.js"; print("t1"); try { await 0; print("t2"); } finally { print("t finally"); }
// x.js:    print("x"); throw new Error("from x");
// main.js: await import("./a.js").catch(e => print("rejected:", e.message));
  • main prints t1, x, rejected: from x.
  • This branch and Node.js v26.3.0 print t1, x, t2, t finally, rejected: from x.

Reach. An audit found this. No issue reports it. All of these must hold:

  1. A static import cycle.
  2. A member of the cycle that has a top-level await and is not the first module of the cycle that the walk visits. Only then is it on the stack, with its body started, when step 9 runs.
  3. A module that the same Evaluate() visits later and that throws synchronously.
  4. A program that continues after the failed load (a caught import(), a Worker, a test runner), or an await that needs only jobs to settle.

These shapes print the same on main, on this branch and on Node.js v26.3.0:

  • no cycle,
  • the module that throws is imported first,
  • the later module fails after an await of its own,
  • a static entry point whose suspended member waits for a timer (the process exits first).

What an error does to a record now

  • Body not started (State is 0): the record gives up its executable, as before. Records that wait for a dependency are in this state, so AsyncModuleExecutionRejected behaves as before.
  • Body suspended: the record keeps the executable. The resume job that the await queued resumes the body. evaluate() releases the executable when the body returns or throws.
  • Body ended: nothing to do, evaluate() released the executable.

The check that was at the top of evaluate() also covered a record with an error whose body had not started. Such a record has no executable now, so the same error is thrown from the no-executable arm. One half of this change without the other therefore rejects the resume once. It does not queue the resume job again.

Callers. Nothing starts a body on a record that has an error: InnerModuleEvaluation step 2 and AsyncModuleExecutionFulfilled step 12.a come first. Two debug assertions state that (JSModuleRecord::execute, JSModuleRecord::evaluate). setEvaluationError has three callers: Evaluate() step 9, AsyncModuleExecutionRejected, and JSModuleLoader::linkAndEvaluateModule after a failed link. Only step 9 can meet a suspended body. The last one is the API path that neither Bun nor the jsc shell calls. It is the only one that gives an error to a record that is not evaluated.

Measurements. Local builds with the flags of the CI lanes bun-webkit-linux-amd64 (release, no LTO) and bun-webkit-linux-amd64-debug-asan. Base is 87ee10d.

  • JSModuleRecord::evaluate, from its entry to the entry of Interpreter::executeModuleProgram (release, gdb stepi, one module await 0): 28 to 25 instructions for a start, 28 to 25 for a resume.
  • Release jsc .text: 28,365,758 to 28,365,822 bytes. JSModuleRecord::evaluate 772 to 800 bytes. New didReceiveEvaluationError 88 bytes.
  • Calls (debug, breakpoint hit counts), a chain of 2,001 top-level-await modules: evaluate 4,002, didFinishEvaluation 2,001, resume jobs 2,001 on base and head. runInternalMicrotask 16,026 or 16,027 on both (it varies by 1 between runs). The repro: 5, 4, 2 and 47 on both.
  • Error path (release, gdb stepi, from the call in setEvaluationError to its return): body not started 146 to 158, body ended 8 to 16, body suspended 164 to 16 (the release moves to the end of the body).
  • Code held: while a record is suspended with an error, $vm.codeBlockCensus() has 1 more module executable with linked code and 1 more with unlinked code (was 0). After the body ended, 0 more. After $vm.shrinkFootprintWhenIdle(), 0.
  • A record that is suspended with an error, never resumed and then collected, while another loader shares its executable: 1 module executable keeps its unlinked code after a shrink (was 0). main has the same 1 for a record that is suspended with no error and for a record that is linked and never reached.
  • sizeof(JSModuleRecord) 432 to 432, sizeof(ModuleProgramExecutable) 176 to 176.
  • Not installed here: valgrind, perf, pahole, bloaty.

Tests run on the debug ASAN build (assertions on):

  • JSTests/modules.yaml: 1,650 of 1,650 runs pass. The new test passes in all 11 configurations and fails on main at its first assertion.
  • JSTests/stress, tests whose names match module|import|tla|top-level: 657 of 658 runs pass. module-graph-loading-stack-overflow.js fails the same way on main (debug ASAN stack frames).
  • module-loaders-share-released-code.js passes its 5 configurations and fails on main.
  • Mutations: with the release done for every record, the new test fails at its first assertion in about 0.5 s. With the old check back at the top of evaluate(), it fails in about 0.3 s. With no release at all, module-loaders-share-released-code.js fails its census at line 150.

Other open pull requests

test262 and upstream

  • test262 has no test in which a module is suspended when a later module of its cycle throws. The nearest are top-level-await/pending-async-dep-from-cycle.js (nothing throws) and dynamic-import/import-fulfilled-member-of-errored-cycle.js (the error comes after an await).
  • WebKit/WebKit has the same check at the top of JSModuleRecord::evaluate, from the module loader rewrite. JSTests/modules/tla-cycle.js has this graph and checks only that the import rejects.

Not changed

  • A resume whose entry fails before the body is entered (for example a stack overflow in Interpreter::executeModuleProgram) still ends the body. That is as before.
  • node:vm in Bun evaluates the dependencies of a module one by one, so some vm.SourceTextModule graphs still differ from Node.js. That is a separate change (node:vm: module.evaluate() runs one Evaluate() of the module record bun#46004).

@robobun

robobun commented Oct 19, 2026 •

Copy link
Copy Markdown
Collaborator Author

Status: reproduced on main with the four files in the description. The stock debug jsc prints t1, x, rejected: from x, and this branch prints t2 and t finally too. The Bun tests and the WEBKIT_VERSION change are in oven-sh/bun#46306 (draft, pinned to the preview build of this branch).

@github-actions

github-actions Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Preview build of a23d1a3: autobuild-preview-pr-845-a23d1a36

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reviewed this PR and found no bugs. Because it changes observable top-level-await semantics in the module evaluation state machine (code after an await in an errored cycle now runs, and Bun test expectations pin the old result), a human look from someone who owns the module loader is still worthwhile.

What was reviewed:

  • The executable accounting across every record state: not-started releases in didReceiveEvaluationError, a synchronous throw or a finished body releases in evaluate(), a suspended body releases on resume whether it returns or throws — each path reaches didFinishWithExecutable exactly once.
  • The resume path after the error: asyncExecutionFulfilled/asyncExecutionRejected return at their step 1 for an Evaluated record, so the resumed body's completion neither double-settles nor re-queues.
  • The three setEvaluationError callers (Evaluate step 9, AsyncModuleExecutionRejected, failed link): only step 9 can meet a started body; the other two see State == Init.
  • The new tests only add fixtures and extend a fork-specific stress test, so BUN-TEST-DIFFERENCES.md does not need an entry; I could not run them in this environment.
Extended reasoning...

The change touches four JSC runtime files (CyclicModuleRecord.cpp, JSModuleRecord.cpp/.h, ModuleProgramExecutable.h) with roughly 30 lines of logic: setEvaluationError now releases a record's ModuleProgramExecutable only when the body has not started, and evaluate() throws the stored error only when the executable is already gone. It touches no security-sensitive surface. The logic is small and I traced the executable release to be balanced on every path, and the bug hunt ran dry with no findings. What keeps this from an approve is that it alters user-visible module evaluation semantics in a JS engine core path, the PR itself notes downstream Bun test expectations change, and the new tests could not be executed here.

@coderabbitai

coderabbitai Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Important

Review skipped

We couldn't safely recover the incremental review. No full review was started, and the last reviewed checkpoint was preserved. Retry later, or explicitly request a full review by commenting @coderabbitai full review.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: acc660dc-6d38-4802-9f27-7985ad0db3b0
📥 Commits

Reviewing files that changed from the base of the PR and between 87ee10d and efeabf0.

📒 Files selected for processing: 33
  • JSTests/modules/tla-cycle-suspended-member.js
  • JSTests/modules/tla-cycle-suspended-member/awaits-a.js
  • JSTests/modules/tla-cycle-suspended-member/awaits-suspended.js
  • JSTests/modules/tla-cycle-suspended-member/awaits-throws.js
  • JSTests/modules/tla-cycle-suspended-member/finally-a.js
  • JSTests/modules/tla-cycle-suspended-member/finally-suspended.js
  • JSTests/modules/tla-cycle-suspended-member/finally-throws.js
  • JSTests/modules/tla-cycle-suspended-member/for-await-a.js
  • JSTests/modules/tla-cycle-suspended-member/for-await-suspended.js
  • JSTests/modules/tla-cycle-suspended-member/for-await-throws.js
  • JSTests/modules/tla-cycle-suspended-member/gate-a.js
  • JSTests/modules/tla-cycle-suspended-member/gate-suspended.js
  • JSTests/modules/tla-cycle-suspended-member/gate-throws.js
  • JSTests/modules/tla-cycle-suspended-member/late-throw-a.js
  • JSTests/modules/tla-cycle-suspended-member/late-throw-suspended.js
  • JSTests/modules/tla-cycle-suspended-member/late-throw-throws.js
  • JSTests/modules/tla-cycle-suspended-member/not-started-a.js
  • JSTests/modules/tla-cycle-suspended-member/not-started-suspended.js
  • JSTests/modules/tla-cycle-suspended-member/not-started-throws.js
  • JSTests/modules/tla-cycle-suspended-member/not-started-waits.js
  • JSTests/modules/tla-cycle-suspended-member/rejected-await-a.js
  • JSTests/modules/tla-cycle-suspended-member/rejected-await-suspended.js
  • JSTests/modules/tla-cycle-suspended-member/rejected-await-throws.js
  • JSTests/stress/module-loaders-share-released-code.js
  • JSTests/stress/resources/module-loaders-released-code/adopted-awaits.js
  • JSTests/stress/resources/module-loaders-released-code/adopted-dependency.js
  • JSTests/stress/resources/module-loaders-released-code/adopted-root.js
  • JSTests/stress/resources/module-loaders-released-code/adopted-sibling-throws.js
  • JSTests/stress/resources/module-loaders-released-code/cycle-awaits.js
  • Source/JavaScriptCore/runtime/CyclicModuleRecord.cpp
  • Source/JavaScriptCore/runtime/JSModuleRecord.cpp
  • Source/JavaScriptCore/runtime/JSModuleRecord.h
  • Source/JavaScriptCore/runtime/ModuleProgramExecutable.h

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.

Walkthrough

JavaScriptCore now handles module evaluation errors according to whether top-level execution has started. New module and stress tests cover suspended cycle members resuming, completing or throwing, and releasing executable code.

Changes

Top-Level Await Cycle Evaluation

Layer / File(s) Summary
Module evaluation-error lifecycle
Source/JavaScriptCore/runtime/CyclicModuleRecord.cpp, Source/JavaScriptCore/runtime/JSModuleRecord.cpp, Source/JavaScriptCore/runtime/JSModuleRecord.h, Source/JavaScriptCore/runtime/ModuleProgramExecutable.h
Module records release executable use when an evaluation error arrives before execution starts. If execution has started, they retain the executable and can resume evaluation.
Cycle-member resumption cases
JSTests/modules/tla-cycle-suspended-member.js, JSTests/modules/tla-cycle-suspended-member/*
Module graph tests cover resumed finally blocks, sequential and rejected awaits, and for await iteration when a sibling module throws.
Gated and late-error cases
JSTests/modules/tla-cycle-suspended-member.js, JSTests/modules/tla-cycle-suspended-member/*
Tests cover gated resumption after garbage collection, post-await throws, repeated imports, and a dependent module that has not started when the cycle throws.
Shared executable retention and release
JSTests/stress/module-loaders-share-released-code.js, JSTests/stress/resources/module-loaders-released-code/*
Stress tests check executable retention while records remain suspended and code release after their bodies complete or throw, including a shared-executable scenario.

Priority: ➖ Normal

Merge Risk: ⚪ Minimal · up to efeab

This change makes module bodies suspended at a top-level await resume and finish after a sibling module throws, as ECMA-262 specifies. The tests cover resumption, release of the executable, and not-yet-started modules. No merge-blocking risk was found.

Pre-merge checks: Passed 3 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Description check Warning The description gives a detailed, relevant explanation of the problem, fix, testing, impact, and affected behavior. It does not follow the repository template because it omits a Bugzilla URL, the requ… Add the associated Bugzilla URL, include a "Reviewed by NOBODY (OOPS!)." line or the actual reviewer, and add the required changed-file list with relevant functions or classes. Retain the existing technical explanation and test results.
Passed Passed checks: 3 passed
Check name Status Explanation
Title check Passed The title clearly and concisely describes the primary change: preserving execution of a module body suspended at top-level await after an evaluation error.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Description check

Explanation

The description gives a detailed, relevant explanation of the problem, fix, testing, impact, and affected behavior. It does not follow the repository template because it omits a Bugzilla URL, the required review line, and the template-style changed-file list.

Resolution

Add the associated Bugzilla URL, include a "Reviewed by NOBODY (OOPS!)." line or the actual reviewer, and add the required changed-file list with relevant functions or classes. Retain the existing technical explanation and test results.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Git: CodeRabbit could not clone the repository, so clone-backed analysis was skipped and this review may be incomplete. Verify repository clone access, such as SSH credentials, before requesting another full review. If clone access is intentionally unavailable, use path_filters to narrow the review scope.

Comment @coderabbitai help to get the list of available commands.

@robobun
robobun force-pushed the robobun/097ea4cd/tla-cycle-resume branch from efeabf0 to 3cce2c4 Compare October 21, 2026 05:12
robobun added a commit to oven-sh/bun that referenced this pull request Oct 21, 2026
…fails (WebKit bump)

A module that waits at a top-level await while its import cycle is still
being evaluated never continued when a later module of the cycle threw.
The statements after the await did not run, finally blocks included.
Node.js and ECMA-262 run the body to its end, and Bun 1.3.13 did too.

oven-sh/WebKit#845 keeps the code of a module body that has started and
lets the job that its await queued resume it. WEBKIT_VERSION points at
the preview build of that pull request.

The tests cover import(), a static entry point, several awaits, a
rejected await, for await, a body that throws after its await, a member
whose body has not started, and Bun.ModuleGraph with dispose().
…d at a top-level await

A module of an import cycle can be suspended at a top-level await while it is
still on the stack of Evaluate(): its cycle is not complete. When a later
module of the same walk throws, Evaluate() step 9 gives every record on the
stack that error. The suspended body never continued after that:

    // a.js: import "./t.js"; import "./x.js";
    // t.js: import "./a.js"; try { await 0; } finally { release(); }
    // x.js: throw new Error("x");
    await import("./a.js").catch(() => {}); // release() never runs

CyclicModuleRecord::setEvaluationError() released the record's executable,
and the check at the top of JSModuleRecord::evaluate() threw the stored error
into the job that resumes the body.

ExecuteModule starts such a body with AsyncBlockStart, so it runs to its end
like the body of an async function. AsyncModuleExecutionFulfilled and
AsyncModuleExecutionRejected return at step 1 when they find the module
evaluated. V8 runs the body to its end (Node.js v26.3.0).

JSModuleRecord now decides what an error does to its body
(didReceiveEvaluationError). A body that has not started never runs, and the
record is done with its executable, as before. A body that has started keeps
the executable until evaluate() sees it end. evaluate() enters the body when
the record has an executable, and throws the stored error only when it has
none, so it no longer reads [[EvaluationError]] for a module that runs.

A record that is suspended when it gets an error now keeps its claim on the
code until its body ends, like a record that is suspended with no error. If
the body is never resumed and other loaders share the executable, that
executable keeps its unlinked code. module-loaders-share-released-code.js
asserted the opposite ("it is never resumed"). It now asserts that the code
stays while the body is suspended, also when other loaders take the
executable meanwhile, and goes when the last such body has ended or thrown.

test262 has no test in which a module is suspended when a later module of
its cycle throws. Upstream has the same check at the top of
JSModuleRecord::evaluate().

* JSTests/modules/tla-cycle-suspended-member.js: Added.
* JSTests/modules/tla-cycle-suspended-member/: Added.
* JSTests/stress/module-loaders-share-released-code.js:
* JSTests/stress/resources/module-loaders-released-code/adopted-awaits.js: Added.
* JSTests/stress/resources/module-loaders-released-code/adopted-dependency.js: Added.
* JSTests/stress/resources/module-loaders-released-code/adopted-root.js: Added.
* JSTests/stress/resources/module-loaders-released-code/adopted-sibling-throws.js: Added.
* JSTests/stress/resources/module-loaders-released-code/cycle-awaits.js:
* Source/JavaScriptCore/runtime/CyclicModuleRecord.cpp:
(JSC::CyclicModuleRecord::setEvaluationError):
* Source/JavaScriptCore/runtime/JSModuleRecord.cpp:
(JSC::JSModuleRecord::hasTopLevelExecutionStarted const):
(JSC::JSModuleRecord::didReceiveEvaluationError):
(JSC::JSModuleRecord::evaluate):
(JSC::JSModuleRecord::execute):
* Source/JavaScriptCore/runtime/JSModuleRecord.h:
* Source/JavaScriptCore/runtime/ModuleProgramExecutable.h:
@robobun
robobun force-pushed the robobun/097ea4cd/tla-cycle-resume branch 2 times, most recently from 3cce2c4 to a23d1a3 Compare October 21, 2026 08:32
robobun added a commit to oven-sh/bun that referenced this pull request Oct 21, 2026
…fails (WebKit bump)

A module that waits at a top-level await while its import cycle is still
being evaluated never continued when a later module of the cycle threw.
The statements after the await did not run, finally blocks included.
Node.js and ECMA-262 run the body to its end, and Bun 1.3.13 did too.

oven-sh/WebKit#845 keeps the code of a module body that has started and
lets the job that its await queued resume it. WEBKIT_VERSION points at
the preview build of that pull request.

The tests cover import(), a static entry point, several awaits, a
rejected await, for await, a body that throws after its await, a member
whose body has not started, and Bun.ModuleGraph with dispose().
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant