Repository navigation
[http] Close the response body when reading it fails - #1476
Merged
Merged
Conversation
doHTTPRequest closes the body on the negative-test path and on the normal path, but the io.ReadAll error path returned without closing. An unclosed body holds its connection out of the idle pool, so a server that keeps truncating or resetting responses leaks one connection per probe rather than reusing them. Close it there too. Keeping the explicit close rather than switching the function to a deferred one preserves the existing behaviour of releasing the connection before validators run, which can be slow.
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.
doHTTPRequestcloses the response body on the negative-test path and on thenormal path, but the
io.ReadAllerror path returned without closing it:An unclosed body holds its connection out of the idle pool, so a server that
keeps truncating or resetting responses leaks one connection per probe instead
of reusing them.
Why not
defer resp.Body.Close()That would be the more idiomatic shape, but the success path closes the body
before
validators.RunValidatorsruns, and validators are user-configured andcan be slow. Deferring would hold the connection open across them and change
when it returns to the idle pool. The explicit close keeps the existing timing.
Testing
TestResponseBodyClosedOnReadErrordrivesdoHTTPRequestthrough a transportwhose response body fails every read, and asserts the body was closed. Verified
that it fails without the fix ("response body must be closed on the read-error
path") and passes with it.
go build ./...,go vet,go test -race ./probes/http/...andgofmtare allclean.
Noticed while reviewing #1410; the bug predates it.