#44170·bun

`process` listeners registered after a listener that throws still run

Author: robobunCreated Sep 28, 2026Updated Sep 28, 2026

What version of Bun is running?

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

What steps can reproduce the bug?

javascript
// k.cjs
process.on("exit", code => { console.log("first", code); throw new Error("cleanup failed"); });
process.on("exit", code => { console.log("second", code); });
process.exit(7);
bash
bun k.cjs; echo $?

What is the expected behavior?

Node v26.3.0 prints first 7, prints the error, and exits with 7. The second listener does not run.

What do you see instead?

Bun prints first 7 and second 7, prints the error, and exits with 1. The exit code is #44018. This issue is about second 7.

Additional information

A later listener can hide the failure. This program prints the error and exits with 0 on Bun. Node exits with 7.

javascript
process.on("exit", () => { throw new Error("cleanup failed"); });
process.on("exit", () => { process.exitCode = 0; });
process.exit(7);

The same happens for other events. In each program the first listener prints first and throws, and the second listener prints second. An uncaughtException listener handles the error where the table says "handled".

program Node v26.3.0 Bun
two exit listeners, process.exit(7) first first, second
two exit listeners, loop is empty, handled first first, second
two SIGUSR2 listeners, process.kill(process.pid, "SIGUSR2"), handled first first, second
two warning listeners, process.emitWarning(), handled first first, second
two message listeners in a forked child, handled first first, second
two unhandledRejection listeners first first, second
try { process.emit("custom") } catch {} first, the catch block runs first, second, the error is reported as uncaught, exit code 1

Where Bun already stops. With two exit listeners, an empty loop, and nothing that handles the error, Bun prints first 0 and exits with 1, as Node does. The same is true after a fatal error. The test a throw from an exit listener after a fatal throw still stops subsequent exit listeners in test/js/node/process/process.test.js covers that path.

Cause. process uses a native emitter. EventEmitter::innerInvokeEventListeners (src/jsc/bindings/webcore/EventEmitter.cpp) calls each listener with the catching form of JSC::call, reports the exception through Bun__reportUnhandledError, and continues with the next listener. process.exit() in Node is process.emit('exit', code) followed by process.reallyExit(code). Node's emit calls the listeners with no catch around them (the loop is in try/finally). So the first throw ends the pass and reaches the caller of emit.

The two paths where Bun stops do so for a different reason: exit_on_uncaught_exception is set there, and the error report ends the process before control returns to the loop.

What a fix must handle. I built a version that ends the pass at the first throw in that loop. It fixes each row of the table. It also changes the result of these programs, so a fix needs an answer for each one.

program Node Bun main with the stop
bun test: the first exit listener throws, the second calls process.exit(1) exit code 1 exit code 1 exit code 0, "1 pass, 0 fail"
tracing is on, and a user exit listener that runs before the trace flush throws (5 variants) file written in 5 file written in 3 file written in 0
worker, empty loop, the first exit listener throws parent gets error second runs, no error second does not run, no error
two beforeExit listeners, the first throws one time, handled first, first, second first, second as Node, but only with the extra loop turn of the last note

Notes on the rows:

  • bun test counts and prints the error but does not change the exit code. #41588 is open for that.
  • The trace flush (src/js/internal/trace_events.ts) is an exit listener in Bun. The two variants that Bun main loses are the ones where the loop is empty and nothing handles the error. The QUIC endpoint release (src/js/internal/quic/quic.ts) is an exit listener too. I did not test it.
  • The worker marks its VM as shutting down before it runs the exit listeners, so the error is dropped. #42032 is open for that.
  • After a handled error Node keeps the loop alive for one more turn (setImmediate in process._fatalException). So beforeExit is emitted again, and the listeners that the throw skipped run in the second pass. A fix that adds this turn must also hand the throw of process.emit() to its caller. If it does not, try { process.emit("custom") } catch {} inside a beforeExit listener repeats without end when an uncaughtException listener is present, because Bun reports that throw as uncaught.

I found no earlier report of this behaviour.