Repository navigation
Description
What version of Bun is running?
1.4.3-canary.1+367d939d9. Also main at a4f1429.
What steps can reproduce the bug?
// 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);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.
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 testcounts and prints the error but does not change the exit code. bun test: exit 1 when a process.on('exit') listener throws #41588 is open for that.- The trace flush (
src/js/internal/trace_events.ts) is anexitlistener 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 anexitlistener too. I did not test it. - The worker marks its VM as shutting down before it runs the
exitlisteners, so the error is dropped. Report a loop turn's rejections before the loop-alive check, and keep the exit code when an 'exit' listener throws #42032 is open for that. - After a handled error Node keeps the loop alive for one more turn (
setImmediateinprocess._fatalException). SobeforeExitis 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 ofprocess.emit()to its caller. If it does not,try { process.emit("custom") } catch {}inside abeforeExitlistener repeats without end when anuncaughtExceptionlistener is present, because Bun reports that throw as uncaught.
I found no earlier report of this behaviour.
Activity
robobun commentedon Oct 3, 2026
One more constraint for a fix, found while I prepared it. #44486 is a first step: it fixes a worker abort on debug and ASAN builds that the listener change runs into.
VirtualMachine::uncaught_exception.JSNextTickQueue::drain(src/jsc/bindings/JSNextTickQueue.cpp) stops only when an exception is pending. A draft that took the termination in the reporter let aprocess.nextTickcallback run after a worker'sprocess.exit()in anuncaughtExceptioncapture callback (3 of 3 runs). Main and Node run none.process.on("uncaughtException", () => process.exit(42))in a worker, a tick that was queued behind the throwing tick runs. If that tick never returns,worker.terminate()never resolves. Node printsexit 42in both cases.Jarred-Sumner commentedon Oct 8, 2026
Use handoff tool. GitHub issues are for user-facing issues and a user did not run into this issue