Skip to content

process listeners registered after a listener that throws still run #44170

Closed as not planned

Description

@robobun

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 test counts 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 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. 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 (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.

Activity

  1. robobun commented on Oct 3, 2026

    @robobun
    CollaboratorAuthor

    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.

    • Do not clear a pending TerminationException inside 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 a process.nextTick callback run after a worker's process.exit() in an uncaughtException capture callback (3 of 3 runs). Main and Node run none.
    • The listener form already has this defect on bun 1.4.3-canary.1+367d939d9. With 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 prints exit 42 in both cases.
  2. 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