Repository navigation
fix(http): close response body in StrictHTTPClient.Do (V6.2) - #4622
Open
stevenvegt wants to merge 2 commits into
Open
stevenvegt wants to merge 2 commits into
stevenvegt wants to merge 2 commits into
Conversation
Do replaced the response body with an in-memory copy but never closed the original. When a response exceeded the maximum size, the error was returned with the body still open, so the connection stayed held until the client timeout. With MaxConnsPerHost = 5, a server returning oversized bodies could occupy every connection slot for that host. The original body is now closed on both the error and success paths. On overflow it is closed without draining, so the connection is dropped rather than reused. Backport of #4620 to V6.2. Assisted-by: AI
Assisted-by: AI
stevenvegt
requested review from
gerardsn,
reinkrul and
woutslakhorst
as code owners
October 9, 2026 08:29
Contributor
0 new issues
|
This branch has not been deployed
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.
Problem
StrictHTTPClient.Doreplaces the response body with an in-memory copy read throughlimitedReadAll, but never closes the original body. When a response exceeds the maximum size, the error is returned with the body still open, so the connection is held until the client timeout. WithMaxConnsPerHost = 5onSafeHttpTransport, a server returning oversized bodies can occupy every connection slot for that host. On the success path the original body was not closed either.Fix
Close the original body (deferred, right after the nil check) on both the error and success paths. On overflow the body is closed without draining, so the connection is dropped rather than reused. The limit semantics are unchanged. A release note is added under a new Unreleased block, following earlier V6.2 fix backports.
Testing
New
TestStrictHTTPClient_ClosesResponseBodywrapsSafeHttpTransportin a round tripper that countsClose()calls on the response body. It asserts the body is closed both whenDofails because the response exceedsDefaultMaxHttpResponseSizeand whenDosucceeds. The test failed on the Close assertions before the fix and passes after it.go test -race ./http/client/andgo vet ./http/client/pass.V5.4 is not affected: its strict client does not read the response body.
Backport of #4621 to V6.2, see #4620