Skip to content

node:vm: reset the modules that did not link when instantiate() fails - #46129

Closed
robobun wants to merge 3 commits into
mainfrom
robobun/0655b91f/vm-reset-graph-after-failed-link
Closed

robobun wants to merge 3 commits into
mainfrom
robobun/0655b91f/vm-reset-graph-after-failed-link

Conversation

@robobun

@robobun robobun commented Oct 20, 2026 •

Copy link
Copy Markdown
Collaborator

WEBKIT_VERSION is the preview build of oven-sh/WebKit#842. Move it to the merged sha before the merge.

Problem

  • A vm.SourceTextModule cycle fails to link, the next link() gives another module for one specifier, and evaluate() crashes: ASSERTION FAILED: m_moduleEnvironment at AbstractModuleRecord.h(278), JSC::AbstractModuleRecord::moduleEnvironment(). Release: Segmentation fault at address 0x18. Node runs it.
  • JSC keeps the state of a failed Link() on the modules that did not link. link() then replaces one of their modules (NodeVMSourceTextModule.cpp:361).
  • No issue reports it. jest reaches it (Notes).

Fix

  • When record->link() throws, instantiate() calls the new CyclicModuleRecord::resetGraphAfterFailedLink(). Modules that did not link return to their state before the Link().
  • Native link() changes nothing on a linked record, and native getNamespace() refuses one that is not linked, as in Node.
  • Verified: 11 new tests in test/js/node/vm/vm.test.ts (9 fail on main), Node's 97 test-vm-* files.
  • Self-reviewed: 20 concerns raised, 16 addressed. Notes have the other four.

Background

Downsides

Notes

The program. bun crash1.mjs, and node --experimental-vm-modules --no-warnings crash1.mjs:

import vm from "node:vm";
const context = vm.createContext({ log: console.log });
const mk = (identifier, s) => new vm.SourceTextModule(s, { identifier, context });
const a = mk("a", `import * as ns from "b"; log("a reads " + ns.v);`);
const b1 = mk("b1", `import "a"; import { nope } from "c"; export var v = "b1";`);
const b2 = mk("b2", `import "a"; export var v = "b2";`);
const c = mk("c", `export var y = 1;`);
const st = () => `a:${a.status} b1:${b1.status} b2:${b2.status} c:${c.status}`;
try { await b1.link(s => ({ a, b: b1, c })[s]); console.log("linked?!"); } catch (e) { console.log("b1.link threw:", e.name); }
console.log(st());
try { await a.link(s => ({ a, b: b2, c })[s]); console.log("a.link ok"); } catch (e) { console.log("a.link threw:", e.name, e.message); }
console.log(st());
try { await a.evaluate(); console.log("a.evaluate ok"); } catch (e) { console.log("a.evaluate threw:", e.name, e.message); }
console.log(st());
output
Node v26.3.0 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:linked
Bun 1.4.0 to 1.4.3 and main the first four lines with c:unlinked, then panic(main thread): Segmentation fault at address 0x18
this PR b1.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:unlinked

In the first link(), a finishes InitializeEnvironment with ns bound to the namespace object of b1. Then b1 fails, so it never gets an environment. The second link() gives a the module b2, but initializeEnvironment() returns early for a record that it initialized before. The body of a then reads through the namespace object of b1.

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 of a runs before the body of b2. The tests here read a binding of a cycle after the evaluation.
  • c:unlinked: c linked inside the failed link() of b1, and its status does not say so. Seven calls on such a module differ from Node. They are the same on main and with this PR:
call on c Node v26.3.0 Bun
c.status linked unlinked
c.link(linker) rejects with ERR_VM_MODULE_ALREADY_LINKED resolves, and calls the linker
c.linkRequests([d]) throws ERR_VM_MODULE_STATUS returns
c.instantiate() throws ERR_VM_MODULE_STATUS returns
c.evaluate() runs c rejects with ERR_VM_MODULE_STATUS
c.namespace the namespace object throws ERR_VM_MODULE_STATUS
c.hasAsyncGraph() false throws ERR_VM_MODULE_STATUS

The regression of 1.4.0. The early return in native link() needs no cycle. #32018 made link() leave a module "unlinked" until its own instantiate(). After a failed link() of an importer, a module that JSC linked inside it stays "unlinked", the next link() asks the linker for its modules again, and native link() gave the linked record those modules. The record still reads the bindings of the modules it linked with, and nothing keeps those modules alive.

import vm from "node:vm";
const context = vm.createContext({});
const mk = (identifier, source) => new vm.SourceTextModule(source, { identifier, context });
const e = mk("e", 'export { value } from "dep";');
const bad = mk("bad", 'import { value, missing } from "e";');
const good = mk("good", 'import { value } from "e"; export const read = () => value;');
let made = 0;
const dep = n => new vm.SyntheticModule(["value"], function () { this.setExport("value", "dep#" + n); }, { context });
const linker = specifier => (specifier === "e" ? e : dep(++made));
await bad.link(linker).catch(error => console.log(error.name));
console.log(e.status);
await good.link(linker);
await good.evaluate();
console.log(good.namespace.read(), "made=" + made);
output
Node v26.3.0, Bun 1.3.14 SyntaxError / linked / dep#1 made=1
Bun 1.4.0 to 1.4.3 and main SyntaxError / unlinked / undefined made=2
this PR SyntaxError / unlinked / dep#1 made=2

With a collection before the read, 1.4.x and main read a freed module record (Segmentation fault at 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 new SyntheticModule on every call for a CommonJS module (loadCjsAsEsm). A project with "type": "module" and "transform": {}: one test does import("./bad.js"), which fails to link (a named import that a CommonJS package does not have). The next test does import("./good.js"), which shares export { value } from "cjs-dep" with it. Later tests allocate, and one calls a function of good.js. bun node_modules/jest/bin/jest.js on that project: 4 of 4 runs on 1.4.3 end with Segmentation fault at address 0x0. A debug build with this PR passes 2 of 2 runs, and Node passes.

Landing order.

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 has export * as ns from "./c.js". ASSERTION FAILED: cyclic->status() != CyclicModuleRecord::Status::New && cyclic->status() != CyclicModuleRecord::Status::Unlinked at AbstractModuleRecord.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.js in #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), and vm.test.ts already tests that they validate their input.

  • getNamespace() had no status check: vm.ts checks 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 a linkRequests() with another module, a read through that object crashed (Segmentation fault at address 0x18 on 1.4.3, with no failed link). It now throws ERR_VM_MODULE_STATUS when the module record is not linked. module.namespace is unchanged: vm.ts throws the same error first.
  • Not changed: native link() on a module with no module record (a direct subclass of vm.Module, which the public API cannot link). It dereferences null on main too (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 only node:assert and node:vm, so node --experimental-vm-modules vm-module-relink-after-failed-link-fixture.mjs runs the identical file. One test does that when the installed node is the version that Bun reports.

  • 24 programs in 7 groups. Each program runs in a new context and in the main context. They use link(linker) and linkRequests() with instantiate(), a SyntheticModule, import defer, a stack overflow inside Link() (RangeError), and a collection between the two links.
  • Each program alone in a process, for each context:
as Node other lines segmentation fault
Node v26.3.0 24 0 0
release build of main (1.4.4-canary.1+6bb5dc195) 3 12 9
debug build of this PR 24 0 0
  • The three programs that pass on 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 two node:vm modules 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.
  • "what a failed instantiate() leaves in the heap": after a failed instantiate() of a cycle of 200 modules, the heap has 1 more JSModuleEnvironment and 1 more ModuleProgramExecutable (the leaf that linked). On main: 201 and 202.
  • "native getNamespace() refuses a module that is not linked": after a failed link, and with no failed link.
  • The 11 new tests on a release build of main: 9 fail. The two that pass are "two modules with one identifier and one source text" and the Node test. With the src/ of main and 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.
  • Also run on a debug build:
    • test/js/node/vm/vm.test.ts: 338 pass. One test of main, "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. With BUN_JSC_validateExceptionChecks=1, the 11 new tests pass.
    • Node's test-vm-* files: 97 of 97 pass.
    • sourcetextmodule-link-gc, sourcetextmodule-leak, vm-sourceUrl and happy-dom-vm-16277 in test/js/node/vm/: 11 pass.
    • test/js/bun/resolve/: 407 pass. test/js/bun/module-graph/module-graph.test.ts and module-graph-matrix.test.ts: 1,185 pass. module-graph-compile.test.ts, test/bundler/bundler_compile_prelinked.test.ts, dynamic-import-tla-cycle.test.ts and webkit-prebuilt-url.test.ts: 168 pass.
    • On the machine of these runs, 5 tests of 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 of main has the same timeouts there.

Measurements.

  • Release lane bun-webkit-linux-amd64, autobuild-87ee10da against autobuild-preview-pr-842-373d494d: libJavaScriptCore.a text 34,253,449 to 34,256,513 bytes (llvm-size). llvm-objdump -d of the functions of the link path that existed before (CyclicModuleRecord::link, initializeEnvironment, setImportedModule, getModuleNamespace, resolveExportByName, getOrMakeExecutable): no difference. innerModuleLinking and resolveExport differ in one immediate each, a line number.
  • NodeVMSourceTextModule.o and NodeVMModule.o, built with the flags of a release build from main and 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 a SourceTextModule, and none after it, because the object is kept.
  • Allocations inside the reset, counted with gdb on the thread that runs it, for a cycle of N modules whose last member fails: 21 (N = 1,000), 23 (N = 2,000), 26 (N = 4,000).
  • JSModuleEnvironment::create calls in a second instantiate() of the same 1,000 modules: 1 on main, 1,001 with this PR.
  • No new host function, error code or option in Bun. [JSC] A host can reset the module graph of a failed Link() WebKit#842 adds two $vm test functions to JSC.

Self-review. What is open in four of the 20 concerns:

Considered.

  • The reset inside CyclicModuleRecord::link(), for every caller: the main loader never replaces a module of a record, and every failed import would pay for a walk of the graph.
  • A new module record for each module that did not link: the state stays on the old records, and the next Link() can still take the code of an old record.
  • A check in vm.ts that refuses another module: Node accepts the call, and native callers do not pass vm.ts.
  • Only the early return in native 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

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

robobun commented Oct 20, 2026 •

Copy link
Copy Markdown
Collaborator Author
Updated 4:12 PM PT - Oct 21st, 2026

✅ @robobun, your commit adf8c54bbcb825da8b9f1d5e14af8fb22a1b13c8 passed in Build #130219! 🎉


🧪   To try this PR locally:

bunx bun-pr 46129

That installs a local version of the PR into your bun-46129 executable, so you can run:

bun-46129 --bun

@robobun

robobun commented Oct 20, 2026 •

Copy link
Copy Markdown
Collaborator Author

Status

Comment thread src/jsc/bindings/NodeVMModule.cpp Resolved
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.
@robobun

robobun commented Oct 24, 2026

Copy link
Copy Markdown
Collaborator Author

Closing: #46270 fixed this on main. It has the early return in native link() and the status check in native getNamespace() from this PR. In the place of the engine reset, it gives each module that did not link a new module record, so it needs no WebKit change.

Checked on a release build of main (1.4.4-canary.1+a8fcd319b): the 24 programs of this PR's fixture print what Node v26.3.0 prints, in a new context and in the main context. The program of the report no longer crashes.

Two things stay:

The program for the second point
import 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);
}
last line
Node v26.3.0 x: only2,v v=b2
main x: SyntaxError Export named 'missing' not found in module 'c'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant