Skip to content

[JSC] A job that finishes a module from inside a module body does not strand or start its importers early - #832

Open
robobun wants to merge 1 commit into
mainfrom
robobun/f48be7fb/module-finishes-inside-walk
Open

robobun wants to merge 1 commit into
mainfrom
robobun/f48be7fb/module-finishes-inside-walk

Conversation

@robobun

@robobun robobun commented Oct 17, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

  • Module code can run a job (a microtask checkpoint, a require() of an ES module). When the job finishes a module with a top-level await, its importers never run or run early: in Bun import() stays pending or segfaults.
  • gatherAvailableAncestors skips an importer that is still EVALUATING on the stack of the walk (ASSERTION FAILED: !m->cycleRoot() => m->evaluationError(), CyclicModuleRecord.cpp:739). It also counts a module without a top-level await as done before its body runs, so a job in that body starts an ancestor early.

Fix

  • A count of pending dependencies goes down only when a dependency has finished. asyncExecutionFulfilled() runs the available importers in [[AsyncEvaluationOrder]] and looks for more after each body.
  • An importer on the stack gets its count decremented. A rejected module on the stack keeps its status: the walk and its later importers throw its error.
  • Verified: JSTests/stress/module-finishes-in-job-inside-evaluation.js (20 graphs) and JSTests/modules (149 of 149, debug shell).

Background

  • InnerModuleEvaluation walks a graph depth first. A module with a top-level await parks, and its importers count it in [[PendingAsyncDependencies]] until AsyncModuleExecutionFulfilled runs those whose count reaches 0.
  • ECMA-262 has no job that runs during either algorithm.
  • Considered a host guard that postpones such jobs: it covers only a node:vm context with its own queue.

Downsides

  • Instructions: innerModuleEvaluation 218 to 221 before a leaf body, asyncExecutionFulfilled without the bodies 373 to 384 (one importer) and 589 to 665 (three). Code: 282 bytes smaller.
  • A module body can now run inside a job that another module body started.
Notes

Plain ES modules in Bun, no node:vm. main.mjs calls import("./r.mjs"). r.mjs imports t.mjs (await 0) and x.mjs, which calls drainMicrotasks() of bun:jsc.

  • Bun 1.3.14, 1.4.3 and main (ed680823cb): the import never settles. The body of r.mjs does not run.
  • With a cycle (r imports a, a imports t, x and r) and a t.mjs that throws after the await: exit code 139 on 1.4.3 and main.
  • With this PR: the import fulfils, or rejects with the error of t.mjs.
  • Other triggers do the same: a require() of an ES module that ends the await, and the checkpoint of an async HTMLRewriter handler.

The graphs of the test. Each is a directory under JSTests/stress/resources/module-finishes-in-job-inside-evaluation/. c.js and s.js call drainMicrotasks(). A module that awaits gate finishes in a second step. "pending" means that the promise of the import() never settles.

graph main, release shell this PR
fulfils pending fulfilled
rejects rejected rejected
parent-in-open-cycle-fulfils pending fulfilled
parent-in-open-cycle-rejects rejected rejected
parent-in-open-cycle-waits-for-two pending fulfilled, after the second dependency
tla-parent-in-open-cycle pending fulfilled, the root after the parent
member-fulfils-on-stack pending fulfilled
member-rejects-on-stack rejected rejected
later-importer-of-finished-member segfault fulfilled
later-importer-of-parent-run-in-job pending fulfilled
later-importer-of-rejected-member rejected rejected
later-importer-of-rejected-importer rejected rejected
import-in-job-of-importer-of-module-on-stack fulfilled fulfilled
import-in-job-of-importer-of-rejected-importer rejected, also the inner import() rejected, also the inner import()
rejects-into-open-cycle segfault rejected
two-dependencies-reject rejected rejected, with the first error
one-dependency-rejects-another-fulfils rejected rejected
dependency-rejects-then-body-throws rejected with the error of the job rejected with what the body threw
sibling-finishes-in-body-run-by-job fulfilled, the root ran before the body ended fulfilled, the root ran after it
sibling-finishes-in-body-run-by-job-that-throws fulfilled rejected

A debug shell of main fails an assertion on 18 of the 20. The main column is the release shell of 274ebb3.

For the three importer-of graphs that main gets right, the first version of this PR did not: it ran a module that imports a module on the stack that the job had rejected (y.js, and n.js from a require() in Bun). Step 3 of innerModuleEvaluation now throws the error of such a module.

Which test fails without which line. WebKit CI runs JSTests on the lto lanes, without assertions. With the assertions of the two files turned off, I took out each added branch in turn. The test file fails for every one, by a wrong value or a crash, with two exceptions. The first is isFinished at the pop of a cycle: that line only changes the status that a host reads, and the Bun PR has a node:vm test for it. The second is the last section of these Notes. Without the check at step 3 of innerModuleEvaluation the test fails at later-importer-of-rejected-importer. The two conditions that keep a job from settling the promise of a module on the stack have no test: that promise exists only after an import() of a module on the stack (see "Not covered").

Random graphs. A generator made graphs of 2 to 6 modules with imports, cycles, top-level awaits, throws and drainMicrotasks() calls. A checker looked at the log of each: no body starts before the bodies of the modules that it imports from outside its cycle have ended, a fulfilled import ran every module, and no import stays pending.

  • 44,000 graphs with drains: no violation and no failed assertion with this PR. The first version of this PR had 1 violation in 4,000: the early start that the second Problem bullet names. main crashes or leaves imports pending.
  • 12,000 graphs without drains: the output is the same as that of a debug shell of main, line for line.

Not covered.

  • An import() that a job starts inside the walk, for a module that is on the stack of that walk. It still fails the assertion of step 2 of CyclicModuleRecord::evaluate() in a debug build (CyclicModuleRecord.cpp(586)). In a release build its promise stays pending, as on main: a job does not settle it, also when the job runs or rejects that module. On main a job that rejects the module rejects that promise, through the state that crashes in rejects-into-open-cycle.
  • A member of a cycle that is not closed can run in a job after the job rejected another member of that cycle. GatherAvailableAncestors reads m.[[CycleRoot]].[[EvaluationError]] for this, and a module on the stack has no cycle root yet: a later import can still merge its cycle into the one that failed, or its cycle can close and succeed above a rejected module. The walk throws afterwards and Evaluate() gives every module on its stack the error, so the import() rejects as it must. The body of that member ran.
  • [JSC] An evaluation error does not end a module body that is suspended at a top-level await #845 (a module that waits at a top-level await when a sibling throws, before JSModuleRecord::evaluate: resume suspended TLA body when a cycle sibling throws #281) changes JSModuleRecord::evaluate and what setEvaluationError calls. It merges with this branch without a conflict.

Cost. The numbers are from release builds of Bun on linux-x64 with and without this change. The instruction counts are gdb single-step counts with the JIT off: from the entry of innerModuleEvaluation for a module without imports to the call that runs its body, and from the entry of asyncExecutionFulfilled to its return without the instructions of execute() and executeAsync(). The sizes are from nm -S: innerModuleEvaluation 3,504 to 3,651 bytes, asyncExecutionFulfilled 1,797 to 1,183, asyncExecutionRejected 548 to 811, the new gatherAvailableParents 568, and a Vector slow path 261. The std::sort helper of main (911 bytes) is gone. These are the numbers of 62f9361, the version before the review. The change after it adds one branch for a module that is EVALUATING and two conditions.

An embedder that rejects an importer itself. gatherAvailableParents() skips an importer on the stack that has an error. JSC alone never gets there with a count that reaches 0: the dependency that failed never decrements it. Bun's node:vm rejects such an importer for a run that a timeout stopped, and then finishes the dependency. oven-sh/bun#46004 has the test.

@github-actions

github-actions Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

Preview build of ab1eb44: autobuild-preview-pr-832-ab1eb441

robobun added a commit to oven-sh/bun that referenced this pull request Oct 18, 2026
module.evaluate() called Evaluate() once for each dependency and then for
the module. A legal import cycle failed with a TDZ ReferenceError, and a
module that threw after a top-level await ended the process with an
unhandled rejection that the program could not catch.

evaluate() now calls JSC's Evaluate() once, and JSC walks the graph. A
vm.SyntheticModule has a JSModuleRecord without code, and the
moduleLoaderEvaluate hook runs its evaluateCallback where the walk reaches
the record. A module reads its status and its error from its record.

A vm run that a timeout or a SIGINT cuts short gives its error to the
modules that it stopped.

The WebKit version adds the rule for a job that finishes a module while a
module that imports it is on the stack of the walk (oven-sh/WebKit#832).
robobun added a commit to oven-sh/bun that referenced this pull request Oct 18, 2026
module.evaluate() called Evaluate() once for each dependency and then for
the module. A legal import cycle failed with a TDZ ReferenceError, and a
module that threw after a top-level await ended the process with an
unhandled rejection that the program could not catch.

evaluate() now calls JSC's Evaluate() once, and JSC walks the graph. A
vm.SyntheticModule has a JSModuleRecord without code, and the
moduleLoaderEvaluate hook runs its evaluateCallback where the walk reaches
the record. A module reads its status and its error from its record.

A vm run that a timeout or a SIGINT cuts short gives its error to the
modules that it stopped.

The WebKit version adds the rule for a job that finishes a module while a
module that imports it is on the stack of the walk (oven-sh/WebKit#832).
@robobun
robobun force-pushed the robobun/f48be7fb/module-finishes-inside-walk branch 2 times, most recently from 4006f19 to 62f9361 Compare October 18, 2026 18:27
robobun added a commit to oven-sh/bun that referenced this pull request Oct 19, 2026
module.evaluate() called Evaluate() once for each dependency and then for
the module. A legal import cycle failed with a TDZ ReferenceError, and a
module that threw after a top-level await ended the process with an
unhandled rejection that the program could not catch.

evaluate() now calls JSC's Evaluate() once, and JSC walks the graph. A
vm.SyntheticModule has a JSModuleRecord without code, and the
moduleLoaderEvaluate hook runs its evaluateCallback where the walk reaches
the record. A module reads its status and its error from its record.

A vm run that a timeout or a SIGINT cuts short gives its error to the
node:vm modules that it stopped.

The checkpoint of an afterEvaluate context runs once, after the walk, and
runs only the jobs of that context.

The WebKit version is the preview build of oven-sh/WebKit#832: a job that
finishes a module from inside a module body does not strand its importers
and does not start them early.
@robobun robobun changed the title [JSC] A job that finishes a module inside InnerModuleEvaluation does not strand its importers [JSC] A job that finishes a module from inside a module body does not strand or start its importers early Oct 19, 2026
@robobun
robobun marked this pull request as ready for review October 19, 2026 03:04
@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: 3b8cdc38-91da-4f5f-8c01-c4054fd455e1
📥 Commits

Reviewing files that changed from the base of the PR and between 274ebb3 and e441007.

📒 Files selected for processing: 79
  • JSTests/stress/module-finishes-in-job-inside-evaluation.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/dependency-rejects-then-body-throws/c.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/dependency-rejects-then-body-throws/root.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/dependency-rejects-then-body-throws/t.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/fulfils/c.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/fulfils/root.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/fulfils/t.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/import-in-job-of-importer-of-module-on-stack/c.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/import-in-job-of-importer-of-module-on-stack/n.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/import-in-job-of-importer-of-module-on-stack/root.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/import-in-job-of-importer-of-rejected-importer/c.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/import-in-job-of-importer-of-rejected-importer/n.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/import-in-job-of-importer-of-rejected-importer/root.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/import-in-job-of-importer-of-rejected-importer/t.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/later-importer-of-finished-member/c.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/later-importer-of-finished-member/m.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/later-importer-of-finished-member/root.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/later-importer-of-finished-member/y.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/later-importer-of-parent-run-in-job/c.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/later-importer-of-parent-run-in-job/p.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/later-importer-of-parent-run-in-job/root.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/later-importer-of-parent-run-in-job/t.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/later-importer-of-parent-run-in-job/y.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/later-importer-of-rejected-importer/c.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/later-importer-of-rejected-importer/root.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/later-importer-of-rejected-importer/t.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/later-importer-of-rejected-importer/y.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/later-importer-of-rejected-member/c.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/later-importer-of-rejected-member/m.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/later-importer-of-rejected-member/root.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/later-importer-of-rejected-member/y.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/member-fulfils-on-stack/c.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/member-fulfils-on-stack/m.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/member-fulfils-on-stack/root.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/member-rejects-on-stack/c.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/member-rejects-on-stack/m.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/member-rejects-on-stack/root.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/one-dependency-rejects-another-fulfils/c.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/one-dependency-rejects-another-fulfils/root.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/one-dependency-rejects-another-fulfils/t.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/one-dependency-rejects-another-fulfils/u.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/parent-in-open-cycle-fulfils/c.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/parent-in-open-cycle-fulfils/p.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/parent-in-open-cycle-fulfils/root.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/parent-in-open-cycle-fulfils/t.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/parent-in-open-cycle-rejects/c.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/parent-in-open-cycle-rejects/p.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/parent-in-open-cycle-rejects/root.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/parent-in-open-cycle-rejects/t.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/parent-in-open-cycle-waits-for-two/c.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/parent-in-open-cycle-waits-for-two/p.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/parent-in-open-cycle-waits-for-two/root.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/parent-in-open-cycle-waits-for-two/t.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/parent-in-open-cycle-waits-for-two/u.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/rejects-into-open-cycle/a.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/rejects-into-open-cycle/c.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/rejects-into-open-cycle/root.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/rejects-into-open-cycle/t.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/rejects/c.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/rejects/root.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/rejects/t.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/sibling-finishes-in-body-run-by-job-that-throws/root.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/sibling-finishes-in-body-run-by-job-that-throws/s.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/sibling-finishes-in-body-run-by-job-that-throws/t.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/sibling-finishes-in-body-run-by-job-that-throws/x.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/sibling-finishes-in-body-run-by-job/root.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/sibling-finishes-in-body-run-by-job/s.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/sibling-finishes-in-body-run-by-job/t.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/sibling-finishes-in-body-run-by-job/x.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/tla-parent-in-open-cycle/c.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/tla-parent-in-open-cycle/p.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/tla-parent-in-open-cycle/root.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/tla-parent-in-open-cycle/t.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/two-dependencies-reject/c.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/two-dependencies-reject/root.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/two-dependencies-reject/t.js
  • JSTests/stress/resources/module-finishes-in-job-inside-evaluation/two-dependencies-reject/u.js
  • Source/JavaScriptCore/runtime/AbstractModuleRecord.cpp
  • Source/JavaScriptCore/runtime/CyclicModuleRecord.cpp

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

The change updates Bun-specific module evaluation when jobs run during an active evaluation walk. It also adds stress tests that check fulfillment, rejection, and execution ordering across module dependencies, cycles, and imports.

Changes

Bun module evaluation

Layer / File(s) Summary
Evaluation error and completion state
Source/JavaScriptCore/runtime/AbstractModuleRecord.cpp
Bun-specific evaluation checks stored errors when a module is re-entered, propagates errors found during dependency evaluation, and recognizes modules without an async evaluation order as finished.
Incremental module completion
Source/JavaScriptCore/runtime/CyclicModuleRecord.cpp
Bun-specific evaluation queues newly available parents in async-evaluation order, executes synchronous and top-level-await modules incrementally, and handles rejection and completion for modules on the evaluation stack.
Harness and basic outcomes
JSTests/stress/module-finishes-in-job-inside-evaluation.js, JSTests/stress/resources/module-finishes-in-job-inside-evaluation/fulfils/*, JSTests/stress/resources/module-finishes-in-job-inside-evaluation/rejects/*
The test harness records evaluation outcomes and execution logs. Fulfillment and rejection cases check ordering around microtask draining.
Cycles and modules on the evaluation stack
JSTests/stress/resources/module-finishes-in-job-inside-evaluation/{member-*,parent-in-open-cycle-*,rejects-into-open-cycle,tla-parent-in-open-cycle}/*
The stress cases check completion and rejection ordering for modules on the evaluation stack, open cycles, and delayed dependencies.
Imports and later importers
JSTests/stress/resources/module-finishes-in-job-inside-evaluation/{import-in-job-*,later-importer-*}/*
The stress cases cover dynamic imports during evaluation and later importers of fulfilled or rejected modules.
Competing failures and sibling completion
JSTests/stress/resources/module-finishes-in-job-inside-evaluation/{dependency-rejects-then-body-throws,one-dependency-rejects-another-fulfils,two-dependencies-reject,sibling-finishes-in-body-run-by-job*}/*
The cases combine dependency outcomes and check sibling module execution when a body runs from an async execution job.

Priority: ➖ Normal

Merge Risk: ⚪ Minimal · up to e4410

No actionable issue was established for this change. The direct-import edge case remains unverified and warrants normal validation before merging.

Pre-merge checks: Passed 4
Check name Status Explanation
Title check Passed The title clearly and specifically describes the main fix: preventing jobs that finish modules during evaluation from stranding or prematurely starting importers.
Description check Passed The description thoroughly explains the problem, fix, testing, behavior changes, limitations, and performance impact. It does not include the template's explicit Bugzilla link, reviewer line, or chang…
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 thoroughly explains the problem, fix, testing, behavior changes, limitations, and performance impact. It does not include the template's explicit Bugzilla link, reviewer line, or changed-file/function list, but the substantive description is mostly complete and on topic.

  • 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.

@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.

Beyond the inline findings, I also checked that the non-Bun path is untouched (the spec gatherAvailableAncestors and asyncExecutionFulfilled steps 8-12 are kept verbatim under #else, and the step-16 assertion only relaxes under USE(BUN_JSC_ADDITIONS)), and that the one-level min-heap in gatherAvailableParents runs modules in the same order as the spec's sorted execList in the no-job case, since a parent's [[AsyncEvaluationOrder]] is always assigned after that of every dependency it counts as pending.

Extended reasoning...

The change rewrites the Bun-gated async-ancestor gathering and fulfil/reject handling in CyclicModuleRecord.cpp and adds an error check plus a relaxed status assertion in AbstractModuleRecord::innerModuleEvaluation, with 17 new module-graph stress tests. It touches no auth, crypto, or data-exposure surface. Three confirmed findings are posted inline, so a human look is already signalled; this note only records the two additional checks that were ruled out.

Findings marked 🟡 are optional suggestions and need no follow-up push.

Additional findings (outside the current diff — GitHub can't attach inline comments there):

  • 🔴 Source/JavaScriptCore/runtime/AbstractModuleRecord.cpp — A synchronous require() of an ES module that imports a module already rejected inside the walk now succeeds instead of throwing that module's error. At AbstractModuleRecord.cpp:1829 a required module that is Evaluating on an outer stack is skipped as satisfied, with no look at the evaluationError() the new asyncExecutionRejected branch stores on it. Fix: before an Evaluating required module is skipped or merged, throw cyclic->evaluationError() when it is set, at the depInOuterSCC site and at 12.b.iii alike (a different site from the same-stack case). On the base the rejected importer became Evaluated with its error and step 2 threw it.

    Why this was flagged

    Graph: root imports t and c; t has a top-level await and throws after it; c's body calls drainMicrotasks() and then a synchronous require() of n.mjs; n imports root. The drain rejects t, and the new branch at CyclicModuleRecord.cpp:858-864 stores the error on root and leaves root Evaluating. The require() runs a nested evaluate() with its own stack; n's innerModuleEvaluation reaches root, and at AbstractModuleRecord.cpp:1829 depInOuterSCC is true, so 12.b is skipped entirely and no error check runs. n runs its body at step 13, n becomes Evaluated and the require returns its namespace, although root is about to be rejected with t's error. On the base, asyncExecutionRejected set root to Evaluated with the error (CyclicModuleRecord.cpp:874-876), and n's walk threw that error at step 2 (AbstractModuleRecord.cpp:1756-1763), so the require() failed. The new check at AbstractModuleRecord.cpp:1876 only reads the current module's own error, not the required module's, so it does not catch this.

    Verification: Triggered when a job inside a module body rejects a top-level-await dependency of a module that is still Evaluating on the outer walk, and the same body then synchronously require()s an ES module that imports that on-stack module. The new branch at CyclicModuleRecord.cpp:858-864 records the error on an Evaluating module and leaves its status as Evaluating.

Comment on lines +744 to +758
if (m->status() == CyclicModuleRecord::Status::Evaluating) {
// The walk throws the error of m (asyncExecutionRejected). When a dependency of m failed, that dependency
// never gets here. An embedder can reject m itself and then finish the dependency: Bun's node:vm does,
// for a module that the timeout of a run stopped.
if (m->evaluationError())
continue;
int remaining = m->pendingAsyncDependencies().value() - 1;
m->setPendingAsyncDependencies(remaining);
// m has not reached step 12 of its InnerModuleEvaluation, which reads the count.
if (m->asyncEvaluationOrder().isUnset())
continue;
// m is past that step and waits on the stack for a cycle that is not closed. Nothing in the walk reads
// its count again, so it runs like any other module whose last dependency finished.
if (!remaining)
append(m);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 (optional) Users get side effects from a module body in a cycle a job already failed, then the import rejects; on the base that body never runs. A job's rejection marks only the rejected module and its async-parent chain; a sibling member of the same open cycle keeps no error. gatherAvailableParents then appends it (CyclicModuleRecord.cpp:758) and runs its body at line 963; the same decrement lets step 13 run it (AbstractModuleRecord.cpp:1895). Fix: before an on-stack module's body runs at either site, check that no module on the walk's stack was rejected (record the rejection per walk or on each stack entry) and skip it; Evaluate() step 9 then fails it.

Why this was flagged

Graph: root.js imports x.js, m.js, c.js; x.js imports root.js, has await 0 then throws; m.js imports root.js and t.js and logs; t.js is await 0; c.js calls drainMicrotasks(). Walk: x parks with order 1, root.pending=1 and x.asyncParentModules=[root] (AbstractModuleRecord.cpp:1862-1864); m merges into root's cycle (1839), waits on t (pending=1), gets order 3 at 1886 and returns still on the stack; root.pending=2. c drains: x's body throws, asyncExecutionRejected(x) takes the Evaluating branch (CyclicModuleRecord.cpp:858-864), sets the error on x and on root only. t then fulfils; gatherAvailableParents(t) sees m Evaluating with no error (748), decrements to 0 (750-751), m has an order, so append (758); the loop runs m->execute() at 963 and m's body runs. c returns, root throws x's error at AbstractModuleRecord.cpp:1876, Evaluate() step 9 (CyclicModuleRecord.cpp:621-628) marks root, x and m Evaluated with that error and the import rejects. On the base branch gatherAvailableAncestors skips m because it has no cycleRoot (802-803), so its body never runs.

Verification: A rejection inside the walk only marks the rejected module and its [[AsyncParentModules]] chain: Source/JavaScriptCore/runtime/CyclicModuleRecord.cpp:858-864. m has no error, so it is appended at line 758 and m->execute(globalObject) runs at line 963 while the cycle root already carries x's error. On the base m's body never runs (CyclicModuleRecord.cpp:801-803).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Not changed. It is now in the Notes under "Not covered".

The check that the specification makes for this is m.[[CycleRoot]].[[EvaluationError]], and a module on the stack has no cycle root yet. Which cycle m ends in is not known while the walk runs: a later import can still merge its cycle into the one that failed, or its cycle can close and succeed above a rejected module that is lower on the stack. A skip that guesses wrong leaves a module of a healthy cycle with a count of 0 that nothing runs. So the job runs m, and Evaluate() step 9 gives it the error when the walk throws. The import() rejects either way. The difference from main is that the body of m ran.

What e441007 does change is the part that can be decided: a module that imports the rejected module after the job, in the same walk or in a walk that a job started, fails with that error and does not run (step 3 of innerModuleEvaluation, graphs later-importer-of-rejected-importer and import-in-job-of-importer-of-rejected-importer).

@robobun

robobun commented Oct 19, 2026

Copy link
Copy Markdown
Collaborator Author

The engine change that #281 was for is now #845. #281 is closed. #845 changes JSModuleRecord::evaluate and what CyclicModuleRecord::setEvaluationError calls. It does not change asyncExecutionRejected. This branch and #845 merge without a conflict (git merge-file on CyclicModuleRecord.cpp and AbstractModuleRecord.cpp). With both, the setEvaluationError call that this pull request adds goes through JSModuleRecord::didReceiveEvaluationError.

@robobun

robobun commented Oct 20, 2026

Copy link
Copy Markdown
Collaborator Author

Pushed e441007, rebased on 274ebb3. What changed after the review above:

  • The finding about require(): step 3 of innerModuleEvaluation now throws the error of a module that is EVALUATING and that a job rejected. A module that imports it, in the same walk or in a walk that a job started (depInOuterSCC), fails with that error and does not run. main gives the same result for these graphs. New graphs: later-importer-of-rejected-importer, import-in-job-of-importer-of-rejected-importer, and import-in-job-of-importer-of-module-on-stack as the control. Without the check the test fails at the first of them.
  • A job no longer settles the promise of an Evaluate() that a job called on a module on the stack (second thread).
  • The first thread is not changed. The reply there has the reason.

JSTests/stress/module-finishes-in-job-inside-evaluation.js (20 graphs) and JSTests/modules (149 of 149) pass on a debug shell with this change.

@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.

Code review found no issues

No high-confidence issues detected in this change.

Still open from earlier reviews (1):

  • Unresolved: 1 minor or pre-existing.

robobun added a commit to oven-sh/bun that referenced this pull request Oct 20, 2026
module.evaluate() called Evaluate() once for each dependency and then for
the module. A legal import cycle failed with a TDZ ReferenceError, and a
module that threw after a top-level await ended the process with an
unhandled rejection that the program could not catch.

evaluate() now calls JSC's Evaluate() once, and JSC walks the graph. A
vm.SyntheticModule has a JSModuleRecord without code, and the
moduleLoaderEvaluate hook runs its evaluateCallback where the walk reaches
the record. A module reads its status and its error from its record.

A vm run that a timeout or a SIGINT cuts short gives its error to the
node:vm modules that it stopped.

The checkpoint of an afterEvaluate context runs once, after the walk, and
runs only the jobs of that context.

The WebKit version is the preview build of oven-sh/WebKit#832: a job that
finishes a module from inside a module body does not strand its importers
and does not start them early.
robobun added a commit to oven-sh/bun that referenced this pull request Oct 20, 2026
module.evaluate() called Evaluate() once for each dependency and then for
the module. A legal import cycle failed with a TDZ ReferenceError, and a
module that threw after a top-level await ended the process with an
unhandled rejection that the program could not catch.

evaluate() now calls JSC's Evaluate() once, and JSC walks the graph. A
vm.SyntheticModule has a JSModuleRecord without code, and the
moduleLoaderEvaluate hook runs its evaluateCallback where the walk reaches
the record. A module reads its status and its error from its record.

A vm run that a timeout or a SIGINT cuts short gives its error to the
node:vm modules that it stopped.

The checkpoint of an afterEvaluate context runs once, after the walk, and
runs only the jobs of that context.

The WebKit version is the preview build of oven-sh/WebKit#832: a job that
finishes a module from inside a module body does not strand its importers
and does not start them early.
… strand or start its importers early

ECMA-262 has no job that runs while a module graph is evaluated. A host can
run one from a module body: a microtask checkpoint, or a synchronous
require() of an ES module. That job can finish a module with a top-level
await while the modules that wait for it are in the middle of
InnerModuleEvaluation or of AsyncModuleExecutionFulfilled.

Inside InnerModuleEvaluation, GatherAvailableAncestors skipped an importer
that was still EVALUATING and left its count of pending dependencies as it
was. The importer never ran, and the promise of its Evaluate() never
settled. A rejection gave a module on the stack the status EVALUATED with
no cycle root, and the walk then read that root.

Inside AsyncModuleExecutionFulfilled, GatherAvailableAncestors counts a
module without a top-level await as done when it appends the module to
execList, before the body of that module runs. A job that the body ran
could finish the last other dependency of an ancestor. The ancestor then
ran before that body ended, also when the body threw afterwards.

A count of pending dependencies now goes down when a dependency has
finished. asyncExecutionFulfilled() gathers the modules that the finished
module was the last pending dependency of, runs them in the order of their
[[AsyncEvaluationOrder]], and gathers again for a module without a
top-level await when its body has run. A module gets its order after each
module that it waits for, so this is the order of the specification's
sorted execList.

An importer on the stack of the walk gets its count decremented. The walk
reads the count at step 12 of InnerModuleEvaluation. An importer that is
past that step, a member of a cycle that is not closed, runs in the job. A
rejected module on the stack keeps its status and remembers the error, and
the walk throws it when it returns to that module. A module that imports
the rejected module before that, in that walk or in a walk that a job
started, fails with the same error and does not run.

A job does not settle the promise of an Evaluate() that a job called on a
module on the stack: the cycle of that module has not run.
@robobun
robobun force-pushed the robobun/f48be7fb/module-finishes-inside-walk branch from e441007 to ab1eb44 Compare October 22, 2026 01:09
robobun added a commit to oven-sh/bun that referenced this pull request Oct 22, 2026
module.evaluate() called Evaluate() once for each dependency and then for
the module. A legal import cycle failed with a TDZ ReferenceError, and a
module that threw after a top-level await ended the process with an
unhandled rejection that the program could not catch.

evaluate() now calls JSC's Evaluate() once, and JSC walks the graph. A
vm.SyntheticModule has a JSModuleRecord without code, and the
moduleLoaderEvaluate hook runs its evaluateCallback where the walk reaches
the record. A module reads its status and its error from its record.

A vm run that a timeout or a SIGINT cuts short gives its error to the
node:vm modules that it stopped.

The checkpoint of an afterEvaluate context runs once, after the walk, and
runs only the jobs of that context.

The WebKit version is the preview build of oven-sh/WebKit#832: a job that
finishes a module from inside a module body does not strand its importers
and does not start them early.
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