Repository navigation
Conversation
|
Status: reproduced on main with the four files in the description. The stock debug |
|
Preview build of a23d1a3: |
There was a problem hiding this comment.
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.
efeabf0 to
3cce2c4
Compare
…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:
3cce2c4 to
a23d1a3
Compare
…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().
Supersedes #281.
Problem
Evaluate()throws. Its remaining statements andfinallyblocks never run, unlike ECMA-262 and Node.js v26.3.0.CyclicModuleRecord::setEvaluationErrorthen released the executable, andJSModuleRecord::evaluatethrew the error into the resume job.Fix
JSModuleRecord::didReceiveEvaluationErrorreleases 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.AsyncBlockStart), andAsyncModuleExecutionFulfilledstep 1 returns for an evaluated module.JSTests/modules/tla-cycle-suspended-member.js(fails on main),JSTests/modules, the module stress tests.Background
Stateis 0 before it starts and a resume label while suspended.ModuleProgramExecutablewhen it is done, so the code can be released ([JSC] Less memory per function that never runs, and code that can be dropped and decoded again #628).Statetest at both old sites: everyevaluate()call keeps the error check, and one half alone queues the resume job forever.Downsides
$vm.codeBlockCensus(). Never resumed, a shared executable keeps it..textgrows 64 bytes.awaitnow runs, also afterimport()rejected.Notes
Repro (
jsc -m main.js):t1,x,rejected: from x.t1,x,t2,t finally,rejected: from x.Reach. An audit found this. No issue reports it. All of these must hold:
Evaluate()visits later and that throws synchronously.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:
What an error does to a record now
Stateis 0): the record gives up its executable, as before. Records that wait for a dependency are in this state, soAsyncModuleExecutionRejectedbehaves as before.evaluate()releases the executable when the body returns or throws.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:
InnerModuleEvaluationstep 2 andAsyncModuleExecutionFulfilledstep 12.a come first. Two debug assertions state that (JSModuleRecord::execute,JSModuleRecord::evaluate).setEvaluationErrorhas three callers:Evaluate()step 9,AsyncModuleExecutionRejected, andJSModuleLoader::linkAndEvaluateModuleafter a failed link. Only step 9 can meet a suspended body. The last one is the API path that neither Bun nor thejscshell 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) andbun-webkit-linux-amd64-debug-asan. Base is 87ee10d.JSModuleRecord::evaluate, from its entry to the entry ofInterpreter::executeModuleProgram(release,gdb stepi, one moduleawait 0): 28 to 25 instructions for a start, 28 to 25 for a resume.jsc.text: 28,365,758 to 28,365,822 bytes.JSModuleRecord::evaluate772 to 800 bytes. NewdidReceiveEvaluationError88 bytes.evaluate4,002,didFinishEvaluation2,001, resume jobs 2,001 on base and head.runInternalMicrotask16,026 or 16,027 on both (it varies by 1 between runs). The repro: 5, 4, 2 and 47 on both.gdb stepi, from the call insetEvaluationErrorto 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).$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.sizeof(JSModuleRecord)432 to 432,sizeof(ModuleProgramExecutable)176 to 176.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 matchmodule|import|tla|top-level: 657 of 658 runs pass.module-graph-loading-stack-overflow.jsfails the same way on main (debug ASAN stack frames).module-loaders-share-released-code.jspasses its 5 configurations and fails on main.evaluate(), it fails in about 0.3 s. With no release at all,module-loaders-share-released-code.jsfails its census at line 150.Other open pull requests
setEvaluationError. It goes throughdidReceiveEvaluationErrorlike the others, and the two branches merge without a conflict.JSModuleRecord::resetAfterFailedLinknext todidFinishWithExecutable. The two branches conflict in one hunk ofJSModuleRecord.hand one ofJSModuleRecord.cpp. Keep both functions.didFinishWithExecutablestays private.test262 and upstream
top-level-await/pending-async-dep-from-cycle.js(nothing throws) anddynamic-import/import-fulfilled-member-of-errored-cycle.js(the error comes after an await).JSModuleRecord::evaluate, from the module loader rewrite.JSTests/modules/tla-cycle.jshas this graph and checks only that the import rejects.Not changed
Interpreter::executeModuleProgram) still ends the body. That is as before.node:vmin Bun evaluates the dependencies of a module one by one, so somevm.SourceTextModulegraphs still differ from Node.js. That is a separate change (node:vm: module.evaluate() runs one Evaluate() of the module record bun#46004).