Repository navigation
client: release the connection after any response - #1246
Merged
Merged
Conversation
mhutchinson
approved these changes
Nov 9, 2026
HTTPFetcher returned on a 404, or any other status but 200, before it deferred closing the response body. The transport then could not reuse the connection, so every such response cost a new connection, and for as long as the request's context lived, the connection and its two transport goroutines stayed open. Close the body on every path, after reading it to the end so that the connection can be reused.
mhutchinson
force-pushed
the
http-fetcher-close-body
branch
from
November 9, 2026 09:46
9cf88d2 to
e66a44b
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 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.
Fixes #1245.
fetchreturned on a 404, or any other status but 200, before it deferred closing the response body. When that response had a body, the transport couldn't reuse the connection:This change defers closing the body as soon as
Dosucceeds, and reads the body to the end first, so that the connection can be reused. The 200 path is unchanged apart from where the close is deferred.Testing
TestHTTPFetcherReleasesConnections: 10 sequential requests against anhttptestserver, counting new connections. Onmain, 404 and 500 use 10 connections each, and 200 uses 1. With the fix, all three use 1.go test -race ./client/...passes.golangci-lintv2.13.2 reports 0 issues.main, two per leaked connection, and none with the fix.One choice to note: error bodies are now read in full before closing. If you'd rather not read a large error body, a bounded read (
io.LimitReader) would keep the leak fixed; a body larger than the limit would then just not have its connection reused. Happy to change it.My first contribution, #1238, adds me to AUTHORS and CONTRIBUTORS under the individual CLA (commit 69ba233), so this PR doesn't repeat that change.
Reviewed and submitted by me, a human. The investigation, the fix and its testing were developed with the assistance of an AI coding agent.