Skip to content

Commit eceb902

Browse files
fix(web): silence MaxListenersExceededWarning on proxied rewrites
Next 16.3's external rewrite proxy attaches 9 `close` listeners to each ServerResponse, and Sentry adds 2 more, crossing Node's default limit of 10 on every `/ingest/*` and `/monitoring` request. The listeners are released with the response, so this is a false positive that produces ~100-200k log lines/day in prod. Raise the limit to 20 on server responses only, via the `http.server.request.start` diagnostics channel, so the warning stays meaningful for every other emitter. See vercel/next.js#97757 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 727191b commit eceb902

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

‎packages/web/src/instrumentation.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as Sentry from '@sentry/nextjs';
2+
import type { ServerResponse } from 'node:http';
23

34
export async function register() {
45
if (process.env.NEXT_RUNTIME === 'nodejs') {
@@ -10,6 +11,21 @@ export async function register() {
1011
}
1112

1213
if (process.env.NEXT_RUNTIME === 'nodejs') {
14+
// Proxied rewrites (`/ingest/*` for PostHog, the `/monitoring` Sentry
15+
// tunnel) legitimately attach 11 `close` listeners to each response: 9
16+
// from Next's rewrite proxy plus 2 from Sentry. That crosses Node's
17+
// default limit of 10 and logs a MaxListenersExceededWarning on every
18+
// such request, even though the listeners are released with the
19+
// response. The request start channel fires before Next handles the
20+
// request, and scoping the raise to responses keeps the warning
21+
// meaningful for every other emitter. This can be removed once Next
22+
// trims its proxy listeners.
23+
// @see: https://github.com/vercel/next.js/issues/97757
24+
const { subscribe } = await import('node:diagnostics_channel');
25+
subscribe('http.server.request.start', (message) => {
26+
(message as { response?: ServerResponse }).response?.setMaxListeners(20);
27+
});
28+
1329
const { startMetricsServer } = await import('./metricsServer');
1430
startMetricsServer();
1531

0 commit comments

Comments
 (0)