fix(web): silence MaxListenersExceededWarning on proxied rewrites - #1697
Conversation
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>
This comment has been minimized.
This comment has been minimized.
|
Navigate logical layers of code changes, visualize relationships, and explore their blast radius. No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Essentials Run ID: 📒 Files selected for processing (2)
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review. WalkthroughOn Node.js, instrumentation sets the maximum listener limit to 20 for responses included in HTTP request-start events. The Unreleased changelog records that the false-positive warning was silenced. ChangesResponse listener limit
Priority: ⬇️ Low Estimated code review effort: 2 (Simple) | ~8 minutes Change: Bug fix · Severity of issue fixed: Low Suggested reviewers: Merge Risk: ⚪ Minimal · up to The response-specific limit is applied before the affected listener burst, and no other material issue is established. The change appears ready to merge. 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Full details: Docstring CoverageExplanation Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 1 files. (1 skipped: 1 unsupported.)
✨ Finishing Touches 💡 1📝 Generate docstrings 💡
🧪 Generate unit tests (beta)
Comment |
Problem
The web server logs this on every request that goes through an external rewrite (
/ingest/*→ PostHog,/monitoring→ Sentry tunnel):In prod this is ~100–200k log lines/day; staging shows the same pattern.
It's a false positive. Since Next 16.3 (which we adopted in #1594), the external rewrite proxy attaches 9
closelisteners to eachServerResponse, fromrouter-utils/proxy-request.js, its bundledhttpxyand the compression stream cleanup. Sentry adds 2 more, fromserver-subscription.js(isolation scope) andrecord-request-session.js. That's 11, one over Node's default limit. All of them belong to the one response and are released with it, so nothing accumulates.Upstream issue: vercel/next.js#97757 (open; the fix PR vercel/next.js#97824 was closed unmerged).
Fix
In
instrumentation.ts, subscribe to Node'shttp.server.request.startdiagnostics channel and callresponse.setMaxListeners(20). The channel fires before Next handles the request, so the limit is in place before any listener is attached. It's scoped to server responses rather than raisingEventEmitter.defaultMaxListeners, so the warning stays meaningful for every other emitter. The comment links the upstream issue so this can be removed once Next trims its listeners.Testing
Sentry.init(no tracing, matching staging and prod), 11 listeners and one warning per proxied request./ingest/*and/monitoringeach produced warnings;/api/health, the polling APIs,/metricsand pages produced none.🤖 Generated with Claude Code
Note
Low Risk
Startup-only instrumentation that raises per-response listener limits; no auth, data, or request-handling logic changes.
Overview
Stops false-positive
MaxListenersExceededWarningspam on proxied external rewrites (PostHog/ingest/*, Sentry/monitoring), which was flooding production logs (~100–200k lines/day) after Next 16.3’s rewrite proxy plus Sentry attach 11closelisteners perServerResponse.In
instrumentation.ts, on the Node runtime it subscribes tohttp.server.request.startand callsresponse.setMaxListeners(20)before Next/Sentry attach listeners—scoped per response instead of raising the global default. The changelog Unreleased section documents the fix.Reviewed by Cursor Bugbot for commit 3a614f7. Bugbot is set up for automated code reviews on this repo. Configure here.
Summary by CodeRabbit
MaxListenersExceededWarningthat appeared on requests proxied through external rewrites.