Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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
17 changes: 16 additions & 1 deletion src/jsc/VirtualMachine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2481,7 +2481,22 @@
return Ok(promise);
}
self.event_loop_mut().perform_gc();
self.wait_for_promise(jsc::AnyPromise::Internal(promise));
// Bail on `unhandled_error_counter` so a default-fatal uncaught
// exception while the entry module is suspended in top-level await
// stops evaluation, matching the main drain loop in `Run::start`.
while crate::JSPromise::status_ptr(promise) == crate::js_promise::Status::Pending
&& self.unhandled_error_counter == 0
{
if self.jsc_vm().execution_forbidden() {
break;
}
self.event_loop_mut().tick();
if crate::JSPromise::status_ptr(promise) == crate::js_promise::Status::Pending
&& self.unhandled_error_counter == 0
{
self.auto_tick();
}
}

Check warning on line 2499 in src/jsc/VirtualMachine.rs

View check run for this annotation

Claude / Claude Code Review

Two more unpatched TLA-wait sibling sites: --preload and bun test entry point

Beyond the watcher arm already noted, two more sibling TLA-wait sites still call the unguarded generic `wait_for_promise`: the `--preload` non-watcher arm at `src/runtime/jsc_hooks.rs:836` (one call frame deeper on the *same* `Run::start` → `load_entry_point` → `reload_entry_point` → `load_preloads` stack — the guard here would *not* be a no-op), and `load_entry_point_for_test_runner` at `VirtualMachine.rs:4686` (a near byte-identical copy of this function). The PR description's `wait_for_promis
Comment thread
robobun marked this conversation as resolved.
Resolved
Comment thread
robobun marked this conversation as resolved.
Resolved
}

Ok(self.pending_internal_promise.unwrap_or(promise))
Expand Down
10 changes: 10 additions & 0 deletions test/js/bun/spawn/exit-code-uncaught-during-tla-fixture.mjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 41 additions & 1 deletion test/js/bun/spawn/exit-code.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { spawnSync } from "bun";
import { expect, it } from "bun:test";
import { bunExe } from "harness";
import { bunEnv, bunExe } from "harness";

it("process.exit(1) works", () => {
const { exitCode } = spawnSync([bunExe(), import.meta.dir + "/exit-code-1.js"]);
Expand All @@ -26,3 +26,43 @@ it("process.exit(0) works", () => {
const { exitCode } = spawnSync([bunExe(), import.meta.dir + "/exit-code-0.js"]);
expect(exitCode).toBe(0);
});

it("uncaught exception during top-level await is immediately fatal", async () => {
await using proc = Bun.spawn({
cmd: [bunExe(), import.meta.dir + "/exit-code-uncaught-during-tla-fixture.mjs"],
env: bunEnv,
stdout: "pipe",
stderr: "pipe",
});
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
// Module evaluation must not resume after the default-fatal throw.
expect(stdout).toBe("");
expect(stderr).toContain("boom-during-tla");
expect(exitCode).toBe(1);
});

it("unhandled rejection during top-level await is immediately fatal (#22546)", async () => {
await using proc = Bun.spawn({
cmd: [bunExe(), import.meta.dir + "/exit-code-unhandled-rejection-during-tla-fixture.mjs"],
env: bunEnv,
stdout: "pipe",
stderr: "pipe",
});
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
expect(stdout).toBe("");
expect(stderr).toContain("rejected-during-tla");
expect(exitCode).toBe(1);
});

it("uncaught exception during top-level await is survivable with a listener", async () => {
await using proc = Bun.spawn({
cmd: [bunExe(), import.meta.dir + "/exit-code-uncaught-during-tla-handled-fixture.mjs"],
env: bunEnv,
stdout: "pipe",
stderr: "pipe",
});
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
expect(stdout).toBe("caught:boom-during-tla\nmodule-end\n");
expect(stderr).toBe("");
Comment thread
robobun marked this conversation as resolved.
Resolved
expect(exitCode).toBe(0);
});
Loading