next.js
7612eaed - Consolidate unhandled rejection logging into a single listener (#95999)

Commit
12 days ago
Consolidate unhandled rejection logging into a single listener (#95999) An unhandled rejection was previously logged by up to three independent process listeners at once: the render runtime's crash-prevention handler in `process-error-handlers.ts` (a bare `console.error`), the router server's `Log.error('unhandledRejection: ', err)`, and the dev server's `logErrorWithOriginalStack`. The runtime handler must exist on every deployment target (#77997), but on self-hosted `next start`/`next dev` it shares a process with the router server's and dev server's listeners, so a single rejection was logged multiple times in different formats. THe first commits adds a test showing the current behavior where we log multiple times. The second commit introduces `registerUnhandledRejectionListener` and `isUnhandledRejectionListenerRegistered` in `process-error-handlers.ts`, and converts the router server and dev server to check-then-register instead of installing their own rejection loggers: - The listener function is shared via a `Symbol.for` key on `globalThis`, so multiple copies of the module (e.g. in the pre-compiled server bundle and in a route module bundle) register and detect a single listener instance. - The registration check queries `process.listeners('unhandledRejection')` instead of a module-global flag, so it stays accurate even after external code calls `process.removeAllListeners`. `installProcessErrorHandlers` therefore calls the register function unconditionally. The `uncaughtException` handlers are left as they are; they have the same duplication and could be consolidated the same way in a follow-up.
Author
Parents
Loading