Skip to content

Commit

Permalink
properly propogate exit code if process onexit handler throws (#13058)
Browse files Browse the repository at this point in the history
  • Loading branch information
nektro authored Aug 3, 2024
1 parent c552cb4 commit 6fbe3d8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/bun_js.zig
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,8 @@ pub const Run = struct {

vm.onUnhandledRejection = &onUnhandledRejectionBeforeClose;
vm.global.handleRejectedPromises();
vm.onExit();

if (this.any_unhandled and this.vm.exit_handler.exit_code == 0) {
this.vm.exit_handler.exit_code = 1;

Expand All @@ -462,7 +464,6 @@ pub const Run = struct {
);
}

vm.onExit();
if (!JSC.is_bindgen) JSC.napi.fixDeadCodeElimination();
vm.globalExit();
}
Expand Down
24 changes: 24 additions & 0 deletions test/js/node/process/process.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,30 @@ describe("process.onBeforeExit", () => {
expect(exitCode).toBe(0);
expect(stdout.toString().trim()).toBe("beforeExit: 0\nbeforeExit: 1\nexit: 2");
});

it("throwing inside preserves exit code", async () => {
const proc = Bun.spawnSync({
cmd: [bunExe(), "-e", `process.on("beforeExit", () => {throw new Error("boom")});`],
env: bunEnv,
stdio: ["inherit", "pipe", "pipe"],
});
expect(proc.exitCode).toBe(1);
expect(proc.stderr.toString("utf8")).toInclude("error: boom");
expect(proc.stdout.toString("utf8")).toBeEmpty();
});
});

describe("process.onExit", () => {
it("throwing inside preserves exit code", async () => {
const proc = Bun.spawnSync({
cmd: [bunExe(), "-e", `process.on("exit", () => {throw new Error("boom")});`],
env: bunEnv,
stdio: ["inherit", "pipe", "pipe"],
});
expect(proc.exitCode).toBe(1);
expect(proc.stderr.toString("utf8")).toInclude("error: boom");
expect(proc.stdout.toString("utf8")).toBeEmpty();
});
});

it("process.memoryUsage", () => {
Expand Down

0 comments on commit 6fbe3d8

Please sign in to comment.