Repository navigation
Conversation
|
Preview build of ab1eb44: |
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).
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).
4006f19 to
62f9361
Compare
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.
There was a problem hiding this comment.
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.
| 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); |
There was a problem hiding this comment.
🟡 (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).
There was a problem hiding this comment.
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).
|
The engine change that #281 was for is now #845. #281 is closed. #845 changes |
62f9361 to
e441007
Compare
|
Pushed e441007, rebased on 274ebb3. What changed after the review above:
|
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.
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.
e441007 to
ab1eb44
Compare
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.
Problem
require()of an ES module). When the job finishes a module with a top-levelawait, its importers never run or run early: in Bunimport()stays pending or segfaults.gatherAvailableAncestorsskips 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-levelawaitas done before its body runs, so a job in that body starts an ancestor early.Fix
asyncExecutionFulfilled()runs the available importers in[[AsyncEvaluationOrder]]and looks for more after each body.JSTests/stress/module-finishes-in-job-inside-evaluation.js(20 graphs) andJSTests/modules(149 of 149, debug shell).Background
InnerModuleEvaluationwalks a graph depth first. A module with a top-levelawaitparks, and its importers count it in[[PendingAsyncDependencies]]untilAsyncModuleExecutionFulfilledruns those whose count reaches 0.node:vmcontext with its own queue.Downsides
innerModuleEvaluation218 to 221 before a leaf body,asyncExecutionFulfilledwithout the bodies 373 to 384 (one importer) and 589 to 665 (three). Code: 282 bytes smaller.Notes
Plain ES modules in Bun, no
node:vm.main.mjscallsimport("./r.mjs").r.mjsimportst.mjs(await 0) andx.mjs, which callsdrainMicrotasks()ofbun:jsc.main(ed680823cb): the import never settles. The body ofr.mjsdoes not run.rimportsa,aimportst,xandr) and at.mjsthat throws after theawait: exit code 139 on 1.4.3 andmain.t.mjs.require()of an ES module that ends theawait, and the checkpoint of an asyncHTMLRewriterhandler.The graphs of the test. Each is a directory under
JSTests/stress/resources/module-finishes-in-job-inside-evaluation/.c.jsands.jscalldrainMicrotasks(). A module that awaitsgatefinishes in a second step. "pending" means that the promise of theimport()never settles.main, release shellimport()import()A debug shell of
mainfails an assertion on 18 of the 20. Themaincolumn is the release shell of 274ebb3.For the three
importer-ofgraphs thatmaingets 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, andn.jsfrom arequire()in Bun). Step 3 ofinnerModuleEvaluationnow throws the error of such a module.Which test fails without which line. WebKit CI runs
JSTestson theltolanes, 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 isisFinishedat the pop of a cycle: that line only changes the status that a host reads, and the Bun PR has anode:vmtest for it. The second is the last section of these Notes. Without the check at step 3 ofinnerModuleEvaluationthe test fails atlater-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 animport()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.maincrashes or leaves imports pending.main, line for line.Not covered.
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 ofCyclicModuleRecord::evaluate()in a debug build (CyclicModuleRecord.cpp(586)). In a release build its promise stays pending, as onmain: a job does not settle it, also when the job runs or rejects that module. Onmaina job that rejects the module rejects that promise, through the state that crashes inrejects-into-open-cycle.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 theimport()rejects as it must. The body of that member ran.awaitwhen a sibling throws, before JSModuleRecord::evaluate: resume suspended TLA body when a cycle sibling throws #281) changesJSModuleRecord::evaluateand whatsetEvaluationErrorcalls. 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
innerModuleEvaluationfor a module without imports to the call that runs its body, and from the entry ofasyncExecutionFulfilledto its return without the instructions ofexecute()andexecuteAsync(). The sizes are fromnm -S:innerModuleEvaluation3,504 to 3,651 bytes,asyncExecutionFulfilled1,797 to 1,183,asyncExecutionRejected548 to 811, the newgatherAvailableParents568, and aVectorslow path 261. Thestd::sorthelper ofmain(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'snode:vmrejects such an importer for a run that a timeout stopped, and then finishes the dependency. oven-sh/bun#46004 has the test.