Repository navigation
Conversation
eejihoon
marked this pull request as ready for review
August 5, 2026 10:45
gaojude
pushed a commit
that referenced
this pull request
Aug 25, 2026
…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 `[email protected]` 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 <[email protected]> Co-authored-by: Jude Gao <[email protected]> Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
|
Closing as superseded: #96715 landed in 0c7f81e and fixes #96704 at the shared stream helper, which covers this path too. Thank you for the excellent issue report, the bisection help, and this PR — your connection()-based test fixture approach also directly informed the cache-components fix on the merged branch. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What?
Why?
ResponseAbortedreason."The destination stream closed early."throughonRequestError.How?
onRequestError.Fixes #96704
Validation
pnpm test-dev-turbo test/e2e/on-request-error/basic/basic.test.tspnpm test-dev-webpack test/e2e/on-request-error/basic/basic.test.tspnpm test-start-turbo test/e2e/on-request-error/basic/basic.test.tspnpm test-start-webpack test/e2e/on-request-error/basic/basic.test.tspnpm --filter=next build