Repository navigation
Don't report a client-aborted RSC stream as a render error - #96715
Conversation
…zip (#97698) ## Summary On Next 16.3+, App Router HTML and RSC responses are piped with `pipeNodeReadableToNodeResponse`. Under gzip backpressure it called `res.once('drain')` on every write that returned `false`, which leaks one listener per backpressured write onto the zlib stream: ``` MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 drain listeners added to [Gzip]. MaxListeners is 10. ``` The `compression` middleware overrides only `res.on`, forwarding it to the zlib stream once that stream exists (or buffering it until then). It leaves `removeListener` pointing at the response, so a `once` listener is added to the stream but never removed from it. `off` cannot reach it either. `createWriterFromResponse`, directly above in the same file, already uses a single `res.on('drain')` for the response lifetime. This makes the Node-readable path match: pause on `write() === false`, resume from one listener, `off` on `close`. Resuming on that event is correct for both cases. With compression active the wrapped `res.write` writes into the zlib stream, so `false` means the zlib stream's buffer is full and its `drain` is the right signal — the middleware separately wires the socket's `drain` to `stream.resume()`. With compression inactive the listener stays on the response, where `off` does remove it. The pause is latched so a `drain` only resumes a readable that this function paused, and a `drain` arriving after teardown is ignored. ### Scope - Distinct from #96173 / `releaseCompressionStream`, which destroys the zlib stream on **abort**. This warning fires on a **live** stream that is still flowing: slow clients, large streamed HTML/RSC. - Deliberately leaves the bare `readable.destroy()` in the `close` handler alone. That is the separate abort-reason bug in #96704, with fixes already open in #96715 and #96717. - Not fixable by bumping the vendored `compression`: as #96173 established, upstream `compression@1.8.1` still has no premature-close handling, and it is a prebuilt bundle here. `setMaxListeners` or `compress: false` would only hide this. - Mentioned as a `compress: false` workaround in #95130. ## Verification `packages/next/src/server/pipe-readable.test.ts` drives real backpressure (48 × 64 KiB through a throttled reader) and asserts exactly one `drain` registration on the zlib stream, no `MaxListenersExceededWarning`, and a complete, byte-correct payload after `gunzip`. The payload assertion is what catches a latch that stalls the stream. Confirmed it fails without the fix. Measured against the real compiled function on Next 16.3, patching only the drain handling: | | before | after | | --- | --- | --- | | `drain` registrations on `[Gzip]` | 48 (one per backpressured write) | **1** | | `MaxListenersExceededWarning` | 1 | **0** | | decompressed payload | complete | complete | At 120 × 64 KiB the before-count scales with writes (120 registrations); the after-count stays at 1. Separately reproduced end to end on a Next 16.3 standalone production build with `compress: true`: slow-consuming a large gzipped page emitted the warning, and after this change it did not, with `Content-Encoding: gzip` still set and valid gzip on the wire. I could not run `pnpm test` in this checkout, so please treat CI as the source of truth for harness integration. The assertions themselves were validated against the real function as described above. --------- Co-authored-by: Cursor <cursoragent@cursor.com> Co-authored-by: Jude Gao <jude.gao@vercel.com> Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
e3a23e0 to
cd10fdf
Compare
|
Maintainer note: force-pushed a rebase of your commit onto current canary (patch-id identical, authorship preserved, commit signed) so CI runs against tip. Verified locally: the new client-abort test passes in all four modes (dev/start x turbopack/webpack), the existing on-request-error basic suite still passes, and a live repro of #96704 goes from misreporting to clean with this build. @lazerg could you mark this ready for review? GitHub won't let me convert your draft. Review to follow. |
|
Marked ready for review. Thanks for the rebase and verification. |
The cache-components CI matrix force-enables cacheComponents for every e2e test, which rejects the `dynamic = 'force-dynamic'` segment config (the /hang and /error fixtures 500'd in dev and failed the prod build). Use `await connection()` inside a Suspense boundary instead, which makes the pages dynamic in every mode.
Tests PassedCommit: 2787b16 |
Stats from current PR🔴 1 regression
📊 All Metrics📖 Metrics GlossaryDev Server Metrics:
Build Metrics:
Change Thresholds:
⚡ Dev Server
📦 Dev Server (Webpack) (Legacy)📦 Dev Server (Webpack)
⚡ Production Builds
📦 Production Builds (Webpack) (Legacy)📦 Production Builds (Webpack)
📦 Bundle SizesBundle Sizes⚡ TurbopackClient Main Bundles
Server Middleware
Build DetailsBuild Manifests
Build Cache
📦 WebpackClient Main Bundles
Polyfills
Pages
Server Edge SSR
Middleware
Build DetailsBuild Manifests
Build Cache
🔄 Shared (bundler-independent)Runtimes
📝 Changed Files (12 files)Files with changes:
View diffsapp-page-exp..ntime.dev.jsfailed to diffapp-page-exp..time.prod.jsfailed to diffapp-page-tur..ntime.dev.jsfailed to diffapp-page-tur..time.prod.jsfailed to diffapp-page-tur..ntime.dev.jsfailed to diffapp-page-tur..time.prod.jsfailed to diffapp-page.runtime.dev.jsfailed to diffapp-page.runtime.prod.jsfailed to diffdev-validati..ntime.dev.jsfailed to diffdev-validati..ntime.dev.jsfailed to diffdev-validati..ntime.dev.jsfailed to diffdev-validati..ntime.dev.jsfailed to diff📎 Tarball URLCommit: 2787b16 |
gaojude
left a comment
There was a problem hiding this comment.
Reproduced #96704 on stock 16.3.x and verified this fix end to end: typed abort lands before React's generic close handler (confirmed against the compiled flight server), new test passes in dev/start x turbopack/webpack and under the cache-components matrix, and a live repro goes from misreport to clean with render cancellation preserved.
Next 16.3.0 reports a client that disconnects mid-stream as a server
render error. When the browser drops an in-flight RSC request,
pipeNodeReadableToNodeResponse destroys the PassThrough that React
streams the flight payload into. React sees its destination close
before the render ends. It cancels the render with a plain
Error('The destination stream closed early.').
Next drops client aborts in createReactServerErrorHandler, but its
isAbortError check matches only the names AbortError and
ResponseAborted. The generic error passes that filter, reaches
onRequestError and raises a Discord alert that nobody can act on.
Production started to receive these alerts after the bump to Next
16.3.4 in 44d05dc.
Skip the two messages React uses when it cancels a render because the
destination went away. Application code cannot produce either message.
Remove the guard with the upgrade to Next 16.4, which carries the
upstream fix (vercel/next.js#96715).
When a client disconnects mid-stream,
pipeNodeReadableToNodeResponsedestroys the readable React is piping into. React sees its destination close before the render ended and aborts with a genericError("The destination stream closed early."), whichonErrorhas no way to distinguish from a real render error, so it ends up inonRequestErroras a server render error.The Web streams path doesn't have this problem: cancelling the readable passes the
ResponseAbortedreason straight through to React. This does the same for the Node path by aborting the pipeable with that reason as soon as the passthrough is destroyed early, before React's own close handler runs. Genuine render errors still go through untouched.Fixes #96704