Repository navigation
fix: reuse a single drain listener when piping Node streams through gzip - #97698
Merged
gaojude merged 3 commits intoAug 25, 2026
Merged
Conversation
hamidrezahanafi
marked this pull request as ready for review
August 21, 2026 18:06
dianatofficial
left a comment
There was a problem hiding this comment.
Great catch on this edge case. Fallback logic is robust.
Contributor
Tests PassedCommit: bd44829 |
Contributor
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 (33 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 diffapp-route-ex..ntime.dev.jsDiff too large to display app-route-ex..time.prod.jsDiff too large to display app-route-tu..ntime.dev.jsDiff too large to display app-route-tu..time.prod.jsDiff too large to display app-route-tu..ntime.dev.jsDiff too large to display app-route-tu..time.prod.jsDiff too large to display app-route.runtime.dev.jsDiff too large to display app-route.ru..time.prod.jsDiff too large to display dev-validati..ntime.dev.jsfailed to diffdev-validati..ntime.dev.jsfailed to diffdev-validati..ntime.dev.jsfailed to diffdev-validati..ntime.dev.jsfailed to diffpages-api-tu..ntime.dev.jsDiff too large to display pages-api-tu..time.prod.jsDiff too large to display pages-api.runtime.dev.jsDiff too large to display pages-api.ru..time.prod.jsDiff too large to display pages-turbo...ntime.dev.jsDiff too large to display pages-turbo...time.prod.jsDiff too large to display pages.runtime.dev.jsDiff too large to display pages.runtime.prod.jsDiff too large to display server.runtime.prod.jsDiff too large to display use-cache-pr..ntime.dev.jsDiff too large to display use-cache-pr..ntime.dev.jsDiff too large to display use-cache-pr..ntime.dev.jsDiff too large to display use-cache-pr..ntime.dev.jsDiff too large to display 📎 Tarball URLCommit: bd44829 |
gaojude
approved these changes
Aug 24, 2026
pipeNodeReadableToNodeResponse used res.once('drain') on every
backpressured write. The vendored compression middleware forwards on()
to the Gzip stream but not removeListener, so those listeners accumulate
and emit MaxListenersExceededWarning. Match createWriterFromResponse.
Co-authored-by: Cursor <cursoragent@cursor.com>
Adds a regression test that drives real backpressure through pipeNodeReadableToNodeResponse and asserts one drain registration on the zlib stream, no MaxListenersExceededWarning, and a complete payload. It fails without the fix with one registration per backpressured write. Also latches the pause so drain only resumes a readable this function paused, and skips a drain that arrives after teardown. Reverts the middleware characterisation tests, which passed either way and did not belong in release-compression-stream.test.ts. Co-authored-by: Cursor <cursoragent@cursor.com>
…ment Records expressjs/compression#153 as the upstream fix for the `removeListener` asymmetry this works around. It intercepts `removeListener` so it reaches the zlib stream, which would let `res.off('drain', onDrain)` work with compression active. Open since 2019 and not in upstream 1.8.1; we vendor 1.7.4, so it is not something to wait on. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
gaojude
force-pushed
the
fix/gzip-drain-max-listeners
branch
from
August 24, 2026 19:21
a44f218 to
bd44829
Compare
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.
Summary
On Next 16.3+, App Router HTML and RSC responses are piped with
pipeNodeReadableToNodeResponse. Under gzip backpressure it calledres.once('drain')on every write that returnedfalse, which leaks one listener per backpressured write onto the zlib stream:The
compressionmiddleware overrides onlyres.on, forwarding it to the zlib stream once that stream exists (or buffering it until then). It leavesremoveListenerpointing at the response, so aoncelistener is added to the stream but never removed from it.offcannot reach it either.createWriterFromResponse, directly above in the same file, already uses a singleres.on('drain')for the response lifetime. This makes the Node-readable path match: pause onwrite() === false, resume from one listener,offonclose.Resuming on that event is correct for both cases. With compression active the wrapped
res.writewrites into the zlib stream, sofalsemeans the zlib stream's buffer is full and itsdrainis the right signal — the middleware separately wires the socket'sdraintostream.resume(). With compression inactive the listener stays on the response, whereoffdoes remove it.The pause is latched so a
drainonly resumes a readable that this function paused, and adrainarriving after teardown is ignored.Scope
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.readable.destroy()in theclosehandler alone. That is the separate abort-reason bug in Client-aborted RSC stream is reported to onRequestError as "The destination stream closed early." #96704, with fixes already open in Don't report a client-aborted RSC stream as a render error #96715 and Avoid reporting client-aborted RSC streams as request errors #96717.compression: as fix: release compression stream when client disconnects mid-response #96173 established, upstreamcompression@1.8.1still has no premature-close handling, and it is a prebuilt bundle here.setMaxListenersorcompress: falsewould only hide this.compress: falseworkaround in Next.js 16.3 Preview — Feedback #95130.Verification
packages/next/src/server/pipe-readable.test.tsdrives real backpressure (48 × 64 KiB through a throttled reader) and asserts exactly onedrainregistration on the zlib stream, noMaxListenersExceededWarning, and a complete, byte-correct payload aftergunzip. 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:
drainregistrations on[Gzip]MaxListenersExceededWarningAt 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, withContent-Encoding: gzipstill set and valid gzip on the wire.I could not run
pnpm testin 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.