-
Notifications
You must be signed in to change notification settings - Fork 2.5k
fix: Close the original response body on non-2xx responses #4486
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
210afcd
test: assert the original response body is closed on non-2xx responses
yavorl 25b7434
Fixes #4484
yavorl afa7265
Add regression tests for the other 2 callers of CheckResponse()
yavorl 8c8468b
Fix the remaining call sites & update the method godoc
yavorl 7356d4a
Merge branch 'master' into fix-error-response-body-close
gmlewis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2296,6 +2296,50 @@ func TestDo_httpError(t *testing.T) { | |
| } | ||
| } | ||
|
|
||
| // closeRecorder flags when the response body handed back by the transport | ||
| // is closed. | ||
| type closeRecorder struct { | ||
| io.ReadCloser | ||
| closed *bool | ||
| } | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. having this round tripper is also a precondition for the bug. Not a necessary precondition for the test because we're asserting that the underlying cause can't happen - Close() not being called. |
||
|
|
||
| func (r *closeRecorder) Close() error { | ||
| *r.closed = true | ||
| return r.ReadCloser.Close() | ||
| } | ||
|
|
||
| // CheckResponse substitutes resp.Body with a re-readable copy on error | ||
| // responses; the network body it replaces must still be closed. | ||
| func TestDo_closesOriginalBodyOnErrorResponse(t *testing.T) { | ||
| t.Parallel() | ||
| client, mux, _ := setup(t) | ||
|
|
||
| mux.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) { | ||
| http.Error(w, `{"message":"Bad Request"}`, 400) | ||
| }) | ||
|
|
||
| var closed bool | ||
| base := client.client.Transport | ||
| if base == nil { | ||
| base = http.DefaultTransport | ||
| } | ||
| client.client.Transport = roundTripperFunc(func(req *http.Request) (*http.Response, error) { | ||
| resp, err := base.RoundTrip(req) | ||
| if resp != nil { | ||
| resp.Body = &closeRecorder{ReadCloser: resp.Body, closed: &closed} | ||
| } | ||
| return resp, err | ||
| }) | ||
|
|
||
| req, _ := client.NewRequest(t.Context(), "GET", ".", nil) | ||
| if _, err := client.Do(req, nil); err == nil { | ||
| t.Fatal("Expected HTTP 400 error, got no error.") | ||
| } | ||
| if !closed { | ||
| t.Error("original response body was not closed on an error response") | ||
| } | ||
| } | ||
|
|
||
| // Test handling of an error caused by the internal http client's Do() | ||
| // function. A redirect loop is pretty unlikely to occur within the GitHub | ||
| // API, but does allow us to exercise the right code path. | ||
|
|
||
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we centralize the fix to one place? I think it's possible to patch only
CheckResponse.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The patch is something like this:
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason I didn't do it initially is that this is a behaviour change, callers might start seeing errors where there were none before.
Reason being - Response.Body is only guaranteed to be an io.ReadCloser. Custom transports, test doubles, and direct CheckResponse callers routinely supply non-Transport bodies - so the only close semantics a public API may rely on are io.Closer's, which states that a second Close is undefined behaviour. So if someone was diligent about this before and they closed the body themselves, theoretically they can start seeing errors or panics.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That said, if you would still prefer to go with that approach after my argument. I'd be happy to apply the patch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair point. Thanks for the explanation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the review & thank you for the merge @gmlewis