Skip to content

fix(fetch): dump 3xx redirect bodies without pausing so the connection is released - #5767

Open
pacocartones wants to merge 1 commit into
nodejs:mainfrom
pacocartones:fix/5728-redirect-dump-body
Open

fix(fetch): dump 3xx redirect bodies without pausing so the connection is released#5767
pacocartones wants to merge 1 commit into
nodejs:mainfrom
pacocartones:fix/5728-redirect-dump-body

Conversation

@pacocartones

Copy link
Copy Markdown
Contributor

Fixes #5728.

Problem

fetch({ redirect: 'follow' }) hangs when a 3xx redirect body is large, on a connection-limited Agent. In httpNetworkFetch (lib/web/fetch/index.js), onResponseData pushed each chunk into the internal Readable and called controller.pause() on backpressure. A redirect body larger than the highWaterMark (the repro uses 128 KiB) fills the buffer, pauses the socket, and — since the follow path discards the response and recurses into mainFetch — the body is never consumed. The connection stays running; with Agent({ connections: 1 }) the follow-up can never acquire the pinned socket → hang.

The request() path (lib/handler/redirect-handler.js) already discards 3xx bodies without pausing, so it never pins the connection; the fetch path lacked the equivalent.

Fix

Set this.willFollow in onResponseStart, and in onResponseData return early when fetchParams.controller.dump || this.willFollow — discarding the bytes but not pausing, so the socket drains, onResponseEnd fires, and the connection is released. Per-hop (the handler is per-dispatch), and correct because a followed 3xx body is never exposed to the caller.

Test

test/issue-5728.js: RED timed out at 15s (hang); GREEN passes in ~31ms. Full redirect regression suite green (redirect-request 83, redirect-stream 23, fetch/redirect, client-fetch 33, …), npm run lint — all green.

…tion

When redirect mode is 'follow', the 3xx response body is discarded and
never exposed to the caller, but httpNetworkFetch still buffered it into
the internal Readable. A redirect body larger than the stream's
highWaterMark applied backpressure that paused and pinned the
connection, so the follow-up request could never acquire it and fetch
hung on a connection-limited dispatcher.

Mark the hop that will be followed and drain its body (ignore the bytes
but keep reading, without pausing) so the connection completes and is
released back to the pool, mirroring how RedirectHandler ignores 3xx
bodies on the request() path.

Fixes: nodejs#5728
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.50%. Comparing base (17117f6) to head (e4ac1c2).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #5767   +/-   ##
=======================================
  Coverage   93.50%   93.50%           
=======================================
  Files         110      110           
  Lines       39072    39084   +12     
=======================================
+ Hits        36534    36547   +13     
+ Misses       2538     2537    -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.

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 follow does not dump 3xx bodies and hangs when the redirect body is large

2 participants