Skip to content

fix(fetch): apply backpressure to deflate response decoding - #6276

Open
MIbtehajNasar wants to merge 1 commit into
nodejs:mainfrom
MIbtehajNasar:fix-6275-inflate-backpressure
Open

MIbtehajNasar wants to merge 1 commit into
nodejs:mainfrom
MIbtehajNasar:fix-6275-inflate-backpressure

Conversation

@MIbtehajNasar

Copy link
Copy Markdown
Contributor

This relates to...

Fixes #6275

Rationale

deflate is the only content coding that goes through the custom InflateStream in lib/web/fetch/util.js, which picks zlib or raw inflate from the first byte. It forwarded the inner stream with inflate.on('data', this.push.bind(this)), which puts the inner stream in flowing mode and ignores push() returning false, so nothing ever paused decompression. An unconsumed deflate body was inflated into memory in full, while gzip, br and zstd stop at the high-water mark.

Changes

Bug Fixes

  • The 'data' listener pauses the inner inflate stream when push() returns false, and _read() resumes it. When the inner stream's own buffer fills, its write callbacks stop firing, so backpressure reaches the response body as with the other codings.
  • New tests in test/fetch/util.js inflate 16 MiB of zeros (zlib and raw deflate) without reading, check that less than 1 MiB is buffered, then read the stream and check all 16 MiB come through. Both fail on main (16 MiB buffered) and pass with this change.

With the reproduction from the issue (100 MiB of zeros, body not read for 1.5 s, then read in full):

arrayBuffers growth while unread
deflate, main 64.4 MiB
deflate, this PR 0.4 MiB
gzip 0.2 MiB

The full body still reads back as 104,857,600 zero bytes in every case.

Breaking Changes and Deprecations

N/A

Status

InflateStream forwarded its inner zlib stream with a 'data' listener and
ignored push()'s return value, so an unconsumed deflate body was
decompressed into memory in full, unlike gzip, br and zstd. Pause the inner
stream when the readable buffer is full and resume it on _read().

Fixes: nodejs#6275
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.79%. Comparing base (2bcc4b8) to head (09bb75f).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #6276   +/-   ##
=======================================
  Coverage   91.78%   91.79%           
=======================================
  Files         113      113           
  Lines       44920    44934   +14     
=======================================
+ Hits        41232    41245   +13     
- Misses       3688     3689    +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fetch(): deflate responses buffer the entire decompressed body when unconsumed, unlike gzip/br/zstd (InflateStream ignores backpressure)

3 participants