Repository navigation
Conversation
A vm.SourceTextModule cycle whose link() fails, then a link() that gives another module for one specifier, then evaluate() crashed: "ASSERTION FAILED: m_moduleEnvironment" on a debug build, and a segmentation fault at address 0x18 on a release build. JSC keeps what a failed Link() made on the modules that did not link. The next Link() used that state with the new modules. instantiate() now calls CyclicModuleRecord::resetGraphAfterFailedLink() when record->link() throws (oven-sh/WebKit#842). Native link() changes nothing on a record that JSC already linked. Native getNamespace() refuses a record that is not linked. WEBKIT_VERSION is the preview build of oven-sh/WebKit#842.
Collaborator
Author
|
Updated 4:12 PM PT - Oct 21st, 2026
✅ @robobun, your commit adf8c54bbcb825da8b9f1d5e14af8fb22a1b13c8 passed in 🧪 To try this PR locally: bunx bun-pr 46129That installs a local version of the PR into your bun-46129 --bun |
Collaborator
Author
|
Status
|
This was referenced Oct 20, 2026
Jarred-Sumner
added a commit
that referenced
this pull request
Oct 21, 2026
…6270) ### What does this PR do? Hardening follow-up to #46021. It fixes regressions from #46021 and #44618 that were never released, includes more of the open pull requests that fix a crash, and fixes a few crashes that had no pull request. #### Regressions fixed (none was released) - `spyOn(Bun, "$")`, and the same for `"sql"`, `"postgres"` and `"SQL"`, threw "does not support accessor properties yet". - Async `fs.read`, `fs.write` and `zlib.brotliCompress` on a view over a `WebAssembly.Memory` could lose data or copy far more than they needed. - `node:vm` with `vm.constants.DONT_CONTEXTIFY`: `this` in host accessors, prototype handling, `util.types.isProxy`, `Bun.deepEquals`, `assert.deepEqual` and `console.log` of a context. - `zlib` `stream.flush()` threw for arguments it used to accept, and streams had a new enumerable property. - `Bun.Transpiler` `exports.replace` with the name `"default"`. - `process.env.X = 3000` could store the number once the function was JIT-compiled. - `node:sqlite` and `bun:sqlite` could lose rows at exit. - Dev server: stylesheets shared between pages after a build error. - Shell `cp` of a file onto itself exited 1. - `error.stack`: the frame of `require()` reads `at anonymous (unknown)` again. - `import * as process from "process"` followed by `process.on(...)` threw at load. The EventEmitter methods that the module exports now act on `process`. - `delete res.statusCode; res.end()` in a `node:http` handler threw. It sends 200, as in Node. (from #46128) - `node:tls`, `node:https` and `node:http2`: connection teardown and write callbacks behave as in 1.4.3 and Node again. #### Crash fixes Bundler, parser and CSS: #42286, #46092, #46097, #46177, #46239, #36334, #43024, #40769. `bun test`, `--hot` and `--watch`: #30776, #38206, #38974. Shell: #43183, #37743, #46168, #46207, #34900. `node:http`, `node:http2`, `Bun.serve`, streams: #43449, #45380, #44457, #42197, #42485, #38082, #38124, #46074, #45999, #46045. `node:vm`, native addons, workers: #46129, #46198, #45615, #33412, #38031. Dev server: #42790, #42850, #39488. Other: #35991, #38030, #38586, #40526, #41830, #46029, #46078, #46114, and the bug of #42215. #### Behavior changes - `crypto.randomFill(view, cb)` reports `ERR_CRYPTO_OPERATION_FAILED` if it could not fill the view. It used to report success. - A function registered twice on `process` runs twice, as in Node. (#41830) - Under `--hot`, a reload after an uncaught exception cancels the timers that the failed run created. The new run's timers fire, and so do timers and `fs.watchFile` watchers that earlier runs kept on `globalThis`. The reload also resets the exit code to 0. (#38206) - The parent of a Worker no longer gets an `'error'` event for a failed `Bun.serve` request in that Worker. The error is printed, as on the main thread. - `hash.write()` after `digest()` drops the chunk, as in Node. (#35991) - `http2` over TLS with `server.emit("connection", socket)`: `session.socket` is a `TLSSocket` and a `'connection'` listener fires, as in Node. (#38124) - A `slice()` with no type of a body `Blob` has `type === ""`, as in Node. (#46114) - `util.types.isProxy(globalThis)` is false, as in Node. - A write on a `node:net` or `node:tls` socket that is still pending when the connection closes calls back with `ECANCELED`, as in Node. - `HTMLRewriter.on()` throws for a selector that is nested too deeply. - `Bun.Image`: `maxPixels` also bounds PNG metadata, `metadata()` of a GIF reports the size that gets decoded, and a path with a NUL byte throws. (#40526) - `bun build --no-bundle` with the `base64` or `dataurl` loader prints an error. (#36334) - Bundled output for a default-exported function or class expression changes to `var file_default = function name() {}`, as in esbuild. `.name` is unchanged. (#46097) - A Proxy in the prototype chain of a `node:vm` global is allowed. jsdom 30 with `runScripts` needs this. - `bun:sqlite` commits a write that `iterate()` left open when the main thread exits. An explicit `BEGIN` is still rolled back. - When a Worker stops while a native addon's async work is still running, the addon's finalizers run after that work is done, as in Node. - Shell: `mv -n` and `cp -n` don't overwrite, and `cp` reads `R`, `v` and `n` anywhere in a flag group. #### Performance - Ordinary `node:vm` contexts run at the same speed. With `DONT_CONTEXTIFY`, stores to globals and reads of inherited names in a hot loop cost 10 to 20 % more than on `main`. - A `process.env` write costs 20 to 35 % less than after #46021. - An async read into a `WebAssembly.Memory` view looks through the live memories: about 1.5 µs more with one memory. #### Not included - Taken in part, so these should stay open for the rest: #36334, #38974, #39488, #40526, #40769, #42485, #45615, #45999, #46029, #46092, #46114, #46168, #46207, #46239. - Needs a WebKit change: #34655 #39204 #40170 #40253 #41587 #41900 #42699 #42897 #42903 #43084 #43813 #44518 #44949 #45770 #45804 #45827. - #44723 (covered by #46097), #41675, #42753, #38762, #41475, #46016. ### How did you verify your code works? - Every item was reproduced first on release builds of `main`, of its parent and of 1.4.3, and compared with Node v26 where Node has the API. - Every fix has a test that fails on `main` and passes on a debug + ASAN build of this branch, except where that isn't practical (huge inputs, code that only runs on Windows or macOS). - The regressions were checked on grids of calls against the build before #46021 and against 1.4.3. - Bun's own test suites and Node's `test-vm-*`, `test-http2-*` and `test-zlib-*` files for the areas touched.
Collaborator
Author
|
Closing: #46270 fixed this on Checked on a release build of Two things stay:
The program for the second pointimport vm from "node:vm";
const context = vm.createContext({});
const mk = (identifier, source) => new vm.SourceTextModule(source, { identifier, context });
const outcome = promise => promise.then(() => "linked", error => error.name);
const root = mk("root", 'import "b"; import { read } from "a"; export { read };');
const a = mk("a", 'import * as ns from "b"; export const read = () => Object.keys(ns) + " v=" + ns.v;');
const b1 = mk("b1", 'import "a"; import { missing } from "c"; export var v = "b1";');
const b2 = mk("b2", 'import "a"; export var v = "b2"; export var only2;');
const c = mk("c", "export var y = 1;");
// x is not in the graph of root. It gets a before the link() of root fails.
const x = mk("x", 'import { read } from "a"; export { read };');
x.linkRequests([a]);
console.log(await outcome(root.link(s => ({ a, b: b1, c })[s])));
console.log(await outcome(root.link(s => ({ a, b: b2, c })[s])));
await root.evaluate();
console.log("root:", root.namespace.read());
try {
x.instantiate();
await x.evaluate();
console.log("x:", x.namespace.read());
} catch (error) {
console.log("x:", error.name, error.message);
}
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
WEBKIT_VERSIONis the preview build of oven-sh/WebKit#842. Move it to the merged sha before the merge.Problem
vm.SourceTextModulecycle fails to link, the nextlink()gives another module for one specifier, andevaluate()crashes:ASSERTION FAILED: m_moduleEnvironmentatAbstractModuleRecord.h(278),JSC::AbstractModuleRecord::moduleEnvironment(). Release:Segmentation fault at address 0x18. Node runs it.Link()on the modules that did not link.link()then replaces one of their modules (NodeVMSourceTextModule.cpp:361).Fix
record->link()throws,instantiate()calls the newCyclicModuleRecord::resetGraphAfterFailedLink(). Modules that did not link return to their state before theLink().link()changes nothing on a linked record, and nativegetNamespace()refuses one that is not linked, as in Node.test/js/node/vm/vm.test.ts(9 fail onmain), Node's 97test-vm-*files.Background
importof a module record to a module. Onlynode:vmchanges an entry:link(linker)asks the linker again, since 1.4.0 (node:vm: pass the Node v26.3.0 vm test suite (+25 tests, ~65% → 97%) #32018) also for a module that a failedlink()linked.Link(). The main loader never replaces a module but would pay for the walk.Downsides
link(), +15 for the firstmodule.namespaceread, +3,064 bytes of JSC text. A failedinstantiate()walks the graph again (21 allocations for 1,000 modules).Notes
The program.
bun crash1.mjs, andnode --experimental-vm-modules --no-warnings crash1.mjs:b1.link threw: SyntaxError/a:unlinked b1:unlinked b2:unlinked c:linked/a.link ok/a:linked b1:unlinked b2:linked c:linked/a reads b2/a.evaluate ok/a:evaluated b1:unlinked b2:evaluated c:linkedmainc:unlinked, thenpanic(main thread): Segmentation fault at address 0x18b1.link threw: SyntaxError/a:unlinked b1:unlinked b2:unlinked c:unlinked/a.link ok/a:linked b1:unlinked b2:linked c:unlinked/a reads undefined/a.evaluate ok/a:evaluated b1:unlinked b2:evaluated c:unlinkedIn the first
link(),afinishesInitializeEnvironmentwithnsbound to the namespace object ofb1. Thenb1fails, so it never gets an environment. The secondlink()givesathe moduleb2, butinitializeEnvironment()returns early for a record that it initialized before. The body ofathen reads through the namespace object ofb1.What stays different from Node. This PR does not change the two differences in the last row. #46004 has both.
a reads undefined: since 1.4.0 the body ofaruns before the body ofb2. The tests here read a binding of a cycle after the evaluation.c:unlinked:clinked inside the failedlink()ofb1, and its status does not say so. Seven calls on such a module differ from Node. They are the same onmainand with this PR:cc.statuslinkedunlinkedc.link(linker)ERR_VM_MODULE_ALREADY_LINKEDc.linkRequests([d])ERR_VM_MODULE_STATUSc.instantiate()ERR_VM_MODULE_STATUSc.evaluate()cERR_VM_MODULE_STATUSc.namespaceERR_VM_MODULE_STATUSc.hasAsyncGraph()falseERR_VM_MODULE_STATUSThe regression of 1.4.0. The early return in native
link()needs no cycle. #32018 madelink()leave a module "unlinked" until its owninstantiate(). After a failedlink()of an importer, a module that JSC linked inside it stays "unlinked", the nextlink()asks the linker for its modules again, and nativelink()gave the linked record those modules. The record still reads the bindings of the modules it linked with, and nothing keeps those modules alive.SyntaxError/linked/dep#1 made=1mainSyntaxError/unlinked/undefined made=2SyntaxError/unlinked/dep#1 made=2With a collection before the read, 1.4.x and
mainread a freed module record (Segmentation faultat a wild address). That is the test program "the linker makes a new module for its request on every call".jest. jest-runtime 29.7.0 links a module whose status is "unlinked" (
linkAndEvaluateModule), and its linker makes a newSyntheticModuleon every call for a CommonJS module (loadCjsAsEsm). A project with"type": "module"and"transform": {}: one test doesimport("./bad.js"), which fails to link (a named import that a CommonJS package does not have). The next test doesimport("./good.js"), which sharesexport { value } from "cjs-dep"with it. Later tests allocate, and one calls a function ofgood.js.bun node_modules/jest/bin/jest.json that project: 4 of 4 runs on 1.4.3 end withSegmentation fault at address 0x0. A debug build with this PR passes 2 of 2 runs, and Node passes.Landing order.
getModuleNamespace()accepts a module that is not linked yet (tc39/ecma262#3114) WebKit#846 (the assertion ingetModuleNamespace()), then [JSC] A host can reset the module graph of a failed Link() WebKit#842 (the reset, stacked on ci: add docker caching to ci workflows #846), then this PR withWEBKIT_VERSIONat the merged sha.webkit.ts([JSC] A job that finishes a module from inside a module body does not strand or start its importers early WebKit#832) and the same lines ofNodeVMSourceTextModule::link(). Itsstatus()reads the module record, so its checkstatus() != Status::Unlinkeddoes what the early return of this PR does. If this PR lands first, node:vm: module.evaluate() runs one Evaluate() of the module record #46004 drops the early return of this PR in its rebase and keeps the reset block ininstantiate(). If node:vm: module.evaluate() runs one Evaluate() of the module record #46004 lands first, this PR drops itslink()hunk. The programs here print no status.$vmfunction that the stress test of [JSC] A host can reset the module graph of a failed Link() WebKit#842 uses does not read the module record from the error. [JSC] A module error does not keep the module record of its Evaluate() alive WebKit#840 stops storing it there.link()alone fixes the jest crash, with no WebKit change: a debug build with only that hunk passes the jest project in 2 of 2 runs. It can ship first if Using Tailwindcss without build process - Nextjs #842 needs more time. It does not fix the crash of a cycle.The second fix in the WebKit bump. oven-sh/WebKit#846 removes an abort of builds with assertions (debug and ASAN builds) for a plain ES module graph: a member of an import cycle imports
ns, and another member hasexport * as ns from "./c.js".ASSERTION FAILED: cyclic->status() != CyclicModuleRecord::Status::New && cyclic->status() != CyclicModuleRecord::Status::UnlinkedatAbstractModuleRecord.cpp(1204). Release builds run the graph. tc39/ecma262#3114 tracks the same assertion of the specification.JSTests/modules/namespace-export-of-module-not-linked-yet.jsin #846 has the graph. Here, the programs "a named import of the namespace that the replaced module exports" and "a module that the failed link() did not reach" go through that assertion.Native methods. The methods of the native module are reachable with
Object.getOwnPropertySymbols(module), andvm.test.tsalready tests that they validate their input.getNamespace()had no status check:vm.tschecks before it calls. On a module that is not linked it made a namespace object from the modules that the module had then. After a reset or alinkRequests()with another module, a read through that object crashed (Segmentation fault at address 0x18on 1.4.3, with no failed link). It now throwsERR_VM_MODULE_STATUSwhen the module record is not linked.module.namespaceis unchanged:vm.tsthrows the same error first.link()on a module with no module record (a direct subclass ofvm.Module, which the public API cannot link). It dereferences null onmaintoo (NodeVMSourceTextModule.cpp:384).Tests. The programs are in
test/js/node/vm/vm-module-relink-after-failed-link-fixture.mjs, with the lines that Node v26.3.0 prints. The file uses onlynode:assertandnode:vm, sonode --experimental-vm-modules vm-module-relink-after-failed-link-fixture.mjsruns the identical file. One test does that when the installednodeis the version that Bun reports.link(linker)andlinkRequests()withinstantiate(), aSyntheticModule,import defer, a stack overflow insideLink()(RangeError), and a collection between the two links.main(1.4.4-canary.1+6bb5dc195)main: the two of "two modules with one identifier and one source text", and "a link() of it that started before the failed instantiate() ends after it". The first two failed on 1.4.3 (b1 p0): since [JSC] Module records share an executable only when their sources have the same SourceOrigin and start position WebKit#639 twonode:vmmodules do not share code, and the programs are here for the day they do again. The third one fails when the early return does not mark the module as linked by the caller.instantiate()of a cycle of 200 modules, the heap has 1 moreJSModuleEnvironmentand 1 moreModuleProgramExecutable(the leaf that linked). Onmain: 201 and 202.main: 9 fail. The two that pass are "two modules with one identifier and one source text" and the Node test. With thesrc/ofmainand the WebKit of this PR, on a debug build: 10 fail, 8 of them on the new assertions of Using Tailwindcss without build process - Nextjs #842.test/js/node/vm/vm.test.ts: 338 pass. One test ofmain, "doesn't produce cachedData for a source with a 2^27-character string", needs more than the default 5 s on a debug build of these runs. WithBUN_JSC_validateExceptionChecks=1, the 11 new tests pass.test-vm-*files: 97 of 97 pass.sourcetextmodule-link-gc,sourcetextmodule-leak,vm-sourceUrlandhappy-dom-vm-16277intest/js/node/vm/: 11 pass.test/js/bun/resolve/: 407 pass.test/js/bun/module-graph/module-graph.test.tsandmodule-graph-matrix.test.ts: 1,185 pass.module-graph-compile.test.ts,test/bundler/bundler_compile_prelinked.test.ts,dynamic-import-tla-cycle.test.tsandwebkit-prebuilt-url.test.ts: 168 pass.test/js/bun/resolve/and 7 of the two module-graph files need more than the default 5 s on a debug build. They pass with a longer timeout. A debug build ofmainhas the same timeouts there.Measurements.
bun-webkit-linux-amd64,autobuild-87ee10daagainstautobuild-preview-pr-842-373d494d:libJavaScriptCore.atext 34,253,449 to 34,256,513 bytes (llvm-size).llvm-objdump -dof the functions of the link path that existed before (CyclicModuleRecord::link,initializeEnvironment,setImportedModule,getModuleNamespace,resolveExportByName,getOrMakeExecutable): no difference.innerModuleLinkingandresolveExportdiffer in one immediate each, a line number.NodeVMSourceTextModule.oandNodeVMModule.o, built with the flags of a release build frommainand from this PR: text 57,234 to 57,236 bytes and 33,211 to 33,459 bytes.link()has 357 and 359 instructions: the early return is a load, a compare and a branch.instantiate()has 339 and 339.namespaceObject()has 97 and 144: 15 of the new ones run on the first read of the namespace of aSourceTextModule, and none after it, because the object is kept.JSModuleEnvironment::createcalls in a secondinstantiate()of the same 1,000 modules: 1 onmain, 1,001 with this PR.$vmtest functions to JSC.Self-review. What is open in four of the 20 concerns:
link()for a module with no module record: not added. It is a different bug, and it is onmain("Native methods").test/js/bun/resolve/for the plain ES module graph of ci: add docker caching to ci workflows #846: not added. The change is only in WebKit, so the test cannot fail in this repository at this pin.JSTests/modulesin ci: add docker caching to ci workflows #846 and the two programs named above cover the assertion.Considered.
CyclicModuleRecord::link(), for every caller: the main loader never replaces a module of a record, and every failedimportwould pay for a walk of the graph.Link()can still take the code of an old record.vm.tsthat refuses another module: Node accepts the call, and native callers do not passvm.ts.link(): it fixes the regression of 1.4.0 and not the crash of a cycle ("Landing order").no test proof · iteration 1 · platform-specific test(s) that do not run on this machine, deferring to CI, which covers all platforms: test/js/node/vm/vm.test.ts