Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/jsc/JSValue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,13 @@ impl JSValue {
}
JSC__JSValue__isAnyError(self)
}
/// An object whose realm `bun test --isolate` has retired: the file that
/// created it finished and the swap fenced its global (JSC discards its
/// microtasks). Native code does not call into such a function.
Comment thread
robobun marked this conversation as resolved.
Resolved
#[inline]
pub fn is_from_retired_test_isolation_realm(self) -> bool {
self.is_cell() && Bun__JSValue__isFromRetiredTestIsolationRealm(self)
}
/// `JSValue.isError()` — true iff this is an
/// `ErrorInstance` cell (does NOT match `Exception`).
#[inline]
Expand Down Expand Up @@ -2136,6 +2143,7 @@ unsafe extern "C" {
) -> JSValue;
safe fn Bun__JSValue__protect(this: JSValue);
safe fn Bun__JSValue__unprotect(this: JSValue);
safe fn Bun__JSValue__isFromRetiredTestIsolationRealm(this: JSValue) -> bool;
}

// ──────────────────────────────────────────────────────────────────────────
Expand Down
16 changes: 16 additions & 0 deletions src/jsc/bindings/ZigGlobalObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4374,12 +4374,28 @@ extern "C" void Zig__GlobalObject__forbidExecution(Zig::GlobalObject* globalObje

// `bun test --isolate`: the file that just finished is being retired on a live VM. Its context's
// workers, ports, channels and sockets are stopped before anything else of the file is swept.
//
// The realm is also fenced for the rest of the run: JSC drops every microtask queued against it from
// here on (the drain checks `microtaskRunnability()` per task; WebCore sets the same state for a
// document whose event loop group stopped). Native work the file left in flight (a thread-pool job,
// a killed child's exit, a stream read) settles promises of this realm when it lands, and without
// the fence their reactions would run the finished file's script while the next file's global is the
// live one, so whatever they create (timers, servers, subprocesses, a chdir) would be adopted by it.
Comment thread
robobun marked this conversation as resolved.
Resolved
extern "C" void Zig__GlobalObject__stopActiveDOMObjectsForTestIsolation(Zig::GlobalObject* globalObject)
{
globalObject->setMicrotaskRunnability(JSC::QueuedTaskResult::Discard);
Bun::retireWebViewsForTestIsolation(globalObject);
globalObject->scriptExecutionContext()->prepareForDestruction();
}

// Whether `value` is an object from a realm retired by the `--isolate` swap above. Native callers
// that would enter such a function skip it: the event that fired belongs to no live file.
Comment thread
robobun marked this conversation as resolved.
Resolved
extern "C" bool Bun__JSValue__isFromRetiredTestIsolationRealm(JSC::EncodedJSValue encodedValue)
{
JSC::JSObject* object = JSC::JSValue::decode(encodedValue).getObject();
return object && object->globalObject()->microtaskRunnability() == JSC::QueuedTaskResult::Discard;
}

extern "C" void Zig__GlobalObject__destructOnExit(Zig::GlobalObject* globalObject)
{
auto& vm = JSC::getVM(globalObject);
Expand Down
31 changes: 20 additions & 11 deletions src/jsc/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,23 @@ impl EventLoop {
Ok(())
}

/// The gate for native code entering user JS from outside the task queue
/// (all 50+ `run_callback*` callers funnel through here): not once teardown
/// has forbidden script (Node's `can_call_into_js`), not with an exception
/// already pending — a prior callback's microtasks can request termination
/// (worker.terminate()), and entering JS then would trip executeCallImpl's
/// `assertNoException` — and not into a realm `bun test --isolate` has
/// retired: an event a finished file's object outlived it to deliver (a
/// killed child's exit, a socket's close) belongs to no live file, and its
/// handler would otherwise run, and create handles, under the next file's
/// global.
Comment thread
robobun marked this conversation as resolved.
Resolved
#[inline]
fn may_enter_js(callback: JSValue, global_object: &JSGlobalObject) -> bool {
!global_object.has_exception()
&& !(global_object.bun_vm().test_isolation_enabled
&& callback.is_from_retired_test_isolation_realm())
}

/// When you call a JavaScript function from outside the event loop task
/// queue, it has to be wrapped in `runCallback` to ensure that microtasks
/// are drained and errors are handled.
Expand All @@ -470,13 +487,7 @@ impl EventLoop {
this_value: JSValue,
arguments: &[JSValue],
) {
// The gate for native code entering user JS from outside the task
// queue (all 50+ callers funnel through here): not once teardown has
// forbidden script (Node's `can_call_into_js`), and not with an
// exception already pending — a prior callback's microtasks can request
// termination (worker.terminate()), and entering JS then would trip
// executeCallImpl's `assertNoException`.
if global_object.has_exception() {
if !Self::may_enter_js(callback, global_object) {
return;
}
// R-2 noalias mitigation (see PORT_NOTES_PLAN R-2; precedent
Expand Down Expand Up @@ -513,8 +524,7 @@ impl EventLoop {
this_value: JSValue,
arguments: &[JSValue],
) -> JSValue {
// Same gate as `run_callback`.
if global_object.has_exception() {
if !Self::may_enter_js(callback, global_object) {
return JSValue::ZERO;
}
// R-2 noalias mitigation — see `run_callback` above.
Expand Down Expand Up @@ -1215,8 +1225,7 @@ impl EventLoop {
this_value: JSValue,
arguments: &[JSValue],
) -> JsResult<JSValue> {
// Same gate as `run_callback`.
if global_object.has_exception() {
if !Self::may_enter_js(callback, global_object) {
return Ok(JSValue::UNDEFINED);
}
let result = callback.call(global_object, this_value, arguments)?;
Expand Down
14 changes: 13 additions & 1 deletion src/jsc/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,9 @@ pub struct JobHeader {
cancel: unsafe fn(*mut JobHeader),
prev: *mut JobHeader,
next: *mut JobHeader,
/// The `bun test --isolate` file (`VirtualMachine::test_isolation_generation`)
/// that scheduled the job: its completion is for that file's realm only.
Comment thread
robobun marked this conversation as resolved.
Resolved
generation: u32,
}

/// A VM's live [cancellable](JobContext::CANCELLABLE) jobs (JS thread only;
Expand Down Expand Up @@ -316,6 +319,7 @@ impl<C: JobContext> Job<C> {
cancel: |p| unsafe { C::cancel(&raw mut (*p.cast::<Self>()).off) },
prev: core::ptr::null_mut(),
next: core::ptr::null_mut(),
generation: cx.vm().test_isolation_generation,
},
ticket: Some(cx.vm().ticket()),
task: WorkPoolTask {
Expand Down Expand Up @@ -433,7 +437,15 @@ pub unsafe fn complete_erased(ptr: *mut (), cx: &JsThread<'_>) -> JsResult<()> {
// build script-facing values under a pending termination. Release it as
// teardown would — Node's threadpool `after` callbacks bail the same way
// on `!can_call_into_js()`.
if !cx.vm().script_allowed() {
//
// Likewise one that lands after `bun test --isolate` retired the file that
// scheduled it: the swap is that file's exit. Its realm's microtasks are
// already discarded, and a `then` that calls back directly (node:crypto's
// callback forms) must not run the finished file's script under the next
// file's global.
// SAFETY: `ptr` is a live posted `Job<C>`, header first (fn contract).
let stale = unsafe { (*header).generation } != cx.vm().test_isolation_generation;
if !cx.vm().script_allowed() || stale {
// SAFETY: as below; released exactly once, here.
unsafe { ((*header).release_unrun)(header) };
return Ok(());
Expand Down
92 changes: 92 additions & 0 deletions test/cli/test/isolation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1077,6 +1077,98 @@ test.concurrent("--isolate: leaked AbortSignal.timeout does not fire in next fil
expect(exitCode).toBe(0);
});

// The swap sweeps what exists AT the file boundary. Work a finished file left
// in flight lands later, while the next file runs: a thread-pool job settles a
// promise of the retired realm, a child the swap killed reports its exit. The
// finished file's continuation must not run then. Before the fence it did, and
// whatever it created (a setInterval, a Bun.serve, a child process, a chdir)
// was adopted by the running file.
describe.concurrent("--isolate: a finished file's late completions do not run in the next file", () => {
const lateFixtures = {
"a-late.test.ts": `
import { test, expect } from "bun:test";
import { existsSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";

const dir = import.meta.dir;

test("leaks a thread-pool chain and a child with onExit", () => {
// Each turn hops through the thread pool, so the chain is always mid-flight
// at the swap. It only acts once b-late.test.ts says it is running.
(async () => {
while (!existsSync(join(dir, "b-running"))) {
await Bun.password.hash("pw", { algorithm: "bcrypt", cost: 4 });
}
process.chdir(tmpdir());
const server = Bun.serve({ port: 0, fetch: () => new Response("served by dead A") });
writeFileSync(join(dir, "a-acted"), String(server.port));
})();

// Killed by the swap; its exit lands while B runs.
Bun.spawn({
cmd: [process.execPath, "-e", "setInterval(() => {}, 1000)"],
stdio: ["ignore", "ignore", "ignore"],
onExit() {
writeFileSync(join(dir, "a-onexit"), "");
},
});

expect(existsSync(join(dir, "b-running"))).toBe(false);
});
`,
"b-late.test.ts": `
import { test, expect } from "bun:test";
import { existsSync, realpathSync, writeFileSync } from "node:fs";
import { join } from "node:path";

const dir = import.meta.dir;

test("sees nothing from A", async () => {
writeFileSync(join(dir, "b-running"), "");
// Turn the event loop through the thread pool so A's pending job (and
// its killed child's exit) get every chance to land here.
for (let i = 0; i < 40 && !existsSync(join(dir, "a-acted")); i++) {
await Bun.password.hash("pw", { algorithm: "bcrypt", cost: 4 });
}
expect({
acted: existsSync(join(dir, "a-acted")),
onExit: existsSync(join(dir, "a-onexit")),
cwd: realpathSync("."),
}).toEqual({
acted: false,
onExit: false,
cwd: realpathSync(dir),
});
});
`,
};
const files = ["./a-late.test.ts", "./b-late.test.ts"];

test("--isolate", async () => {
using dir = tempDir("isolate-late", lateFixtures);
const { stderr, exitCode } = await runTests(String(dir), ["--isolate"], files);
expect(normalizeBunSnapshot(stderr, dir)).toContain("2 pass");
expect(normalizeBunSnapshot(stderr, dir)).toContain("0 fail");
if (exitCode !== 0) expect(stderr).toBe("");
expect(exitCode).toBe(0);
Comment thread
robobun marked this conversation as resolved.
Resolved
});

// One worker takes both files (scale-up gated), so the same fence applies
// between files inside a --parallel worker.
test("--parallel", async () => {
using dir = tempDir("isolate-late-parallel", lateFixtures);
const { stderr, exitCode } = await runTests(String(dir), ["--parallel=2"], files, {
...bunEnv,
BUN_TEST_PARALLEL_SCALE_MS: "60000",
});
expect(normalizeBunSnapshot(stderr, dir)).toContain("2 pass");
expect(normalizeBunSnapshot(stderr, dir)).toContain("0 fail");
if (exitCode !== 0) expect(stderr).toBe("");
expect(exitCode).toBe(0);
});
});

// Each of these leaked handles used to pin its test file's ENTIRE global
// object (and therefore the file's module graph) for the rest of a
// `bun test --isolate` run, growing memory by one full global per file:
Expand Down
Loading