fix(fetch): drain redirect response bodies so pooled connections are released - #5747
Open
Kjubikstronk wants to merge 1 commit into
Open
fix(fetch): drain redirect response bodies so pooled connections are released#5747Kjubikstronk wants to merge 1 commit into
Kjubikstronk wants to merge 1 commit into
Conversation
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes #5728.
httpRedirectFetchfollows a redirect without ever consuming the 3xx response body. That response is never exposed to the caller, so nothing else reads it either, and the connection it arrived on stays unusable. With a pooled dispatcher each redirect can hold a slot, so aconnections: 1agent deadlocks: the follow-up request queues behind a socket that is still holding an unread body.request()does not have this problem.RedirectHandler.onResponseDataswallows 3xx chunks but keeps reading them, which drains the socket. This makesfetchdo the same.Why not just cancel the stream
That was my first attempt and it is wrong in a way worth recording. The body stream's cancel algorithm is:
It aborts
fetchParams' controller, andhttpRedirectFetchthen callsmainFetchon that same controller, so cancelling the redirect body aborts the redirect you are about to follow. The repro still hung. Draining is the only option that frees the socket without touching the controller.Test
test/fetch/redirect-body-drain.js, close to the reporter's script: a 301 carrying 128 KiB, anAgentwithconnections: 1, and a follow-up that has to reuse that socket. It passes in ~25ms with the change and hangs until the timeout without it, so it fails for the right reason.Verification
test/redirect-request.js,test/redirect-stream.js,test/redirect-pipeline.js: 109/109test/fetch/*.js: 457 passed / 13 failed, and the same 13 fail on an unmodified tree. They are the GC, finalizer and--max-http-header-sizetests that need runner flags this invocation does not pass. I checkedfire-and-forget.jsspecifically, since its name is close to this behaviour, and it fails identically with and without the change.Draining does read the redirect body rather than discarding it unread, which matches what
RedirectHandleralready does forrequest(). If you would rather destroy the connection than read the body on large redirects, I am happy to change the approach.