Skip to content

An exit listener that throws replaces the exit code with 1 #44018

Closed as not planned

Description

@robobun

What version of Bun is running?

1.4.3-canary.1+367d939d9. Also main at 9730163.

What steps can reproduce the bug?

// a.cjs
process.exitCode = 4;
process.on("exit", code => {
  console.log("exit", code, process.exitCode);
  throw new Error("cleanup failed");
});
bun a.cjs; echo $?

What is the expected behavior?

Node v26.3.0 prints exit 4 4, prints the error, and exits with 4.

What do you see instead?

Bun prints exit 4 4, prints the error, and exits with 1.

Additional information

Two more programs have the same result:

program node v26.3.0 bun
process.on("exit", () => { throw new Error("x") }); process.exit(7); 7 1
process.on("exit", () => { process.exitCode = 98; throw new Error("x") }); Promise.reject(new Error("oops")); 98 1

Node uses 1 only when no exit code is set. Bun stores 1 over the code that is set. The error is thrown after beforeExit was dispatched, so VirtualMachine::uncaught_exception (src/jsc/VirtualMachine.rs) prints it and calls the exit function with 1, and Bun__Process__exit (src/runtime/node/node_process.rs) stores that code.

#42032 changes the process.exit(7) row to 7. It does not change the other two rows. #38116 does not change this path.

Activity

  1. Jarred-Sumner commented on Oct 8, 2026

    @Jarred-Sumner
    Collaborator

    Use handoff tool. GitHub issues are for user-facing issues and a user did not run into this issue

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions