You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
What does 'go version' print?
go version go1.4rc1 darwin/amd64
What steps reproduce the problem?
If possible, include a link to a program on play.golang.org.
1. View the docs at http://tip.golang.org/pkg/net/http/
What happened?
Docs show the following method for cleaning up response bodies:
resp, err := http.Get("http://example.com/";)
if err != nil {
// handle error
}
defer resp.Body.Close()
What should have happened instead?
The method shown above does not handle the case where both resp and err are non-nil,
which was preserved for compatibility reasons in #3795. If the response was a redirect
and the CheckRedirect func returned an error, the http.Client will return both the
Response and an error. That response's Body would never be closed under the example
shown above, and its connection would leak.
Unfortunately, I suspect this compatibility fix has probably led to lots of potential
leaks in Go code in the wild. Since breaking Go 1 compatibility is not an option, the
docs for Go 1 should show people how to correctly clean up after responses without
leaking conns & file descriptors.
Instead, the following should work and would not leak:
resp, err := http.Get("http://example.com/";)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
// handle error
}
Please provide any additional information below.
The text was updated successfully, but these errors were encountered:
…s more
Issue #8633 (and #9134) noted that we didn't document the rules about
closing the Response.Body when Client.Do returned both a non-nil
*Response and a non-nil error (which can only happen when the user's
CheckRedirect returns an error).
In the process of investigating, I cleaned this code up a bunch, but
no user-visible behavior should have changed, except perhaps some
better error messages in some cases.
It turns out it's always been the case that when a CheckRedirect error
occurs, the Response.Body is already closed. Document that.
And the new code makes that more obvious too.
Fixes#8633
Change-Id: Ibc40cc786ad7fc4e0cf470d66bb559c3b931684d
Reviewed-on: https://go-review.googlesource.com/21364
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Andrew Gerrand <adg@golang.org>
The text was updated successfully, but these errors were encountered: