Skip to content
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

net/http: Request.Body should specify body is not closed on errors #6981

Closed
gopherbot opened this issue Dec 18, 2013 · 5 comments
Closed

net/http: Request.Body should specify body is not closed on errors #6981

gopherbot opened this issue Dec 18, 2013 · 5 comments

Comments

@gopherbot
Copy link

by michael.schurter:

net/http.Request.Body does not mention it will *not* get closed on errors:

    // For client requests, a nil body means the request has no
    // body, such as a GET request. The HTTP Client's Transport
    // is responsible for calling the Close method.

net/http.RoundTripper does not mention Body will *not* get closed on errors:

    // RoundTrip should not modify the request, except for
    // consuming and closing the Body.


net/http.*Client.Do does not mention the behavior of Request.Body


Only net/http.*Client.Post hints the body is closed only when err == nil:

    If the provided body is also an io.Closer, it is closed after the body is successfully written to the server.



Therefore, only a consumer of the Post API will get a hint that the request body is only
closed on successful requests.

It would be nice if other methods of sending requests clarified the behavior.


Reasoning: if you're using io.Pipe() to transform your request body, it's very easy to
deadlock your process if you don't properly close the io.PipeReader.
Unless you notice the *Client.Post docs, it takes a *lot* of digging through go's source
code to find precisely where request bodies are closed.
@rsc
Copy link
Contributor

rsc commented Mar 3, 2014

Comment 1:

Labels changed: added release-go1.3, documentation.

Owner changed to @bradfitz.

Status changed to Accepted.

@bradfitz
Copy link
Contributor

Comment 3:

I tried documenting this but as I did so, it was both gross and I was questioning why we
don't just always close it.
If we failed to write the whole body to the server, the Request.Body is left at a random
position anyway, and is therefore almost certainly useless.
I'd rather just always close it. It prevents leaks and is consistent.
Objections?

@gopherbot
Copy link
Author

Comment 4:

CL https://golang.org/cl/85560045 mentions this issue.

@bradfitz
Copy link
Contributor

Comment 5:

Status changed to Started.

@bradfitz
Copy link
Contributor

Comment 6:

This issue was closed by revision a8d90ec.

Status changed to Fixed.

@rsc rsc added this to the Go1.3 milestone Apr 14, 2015
@rsc rsc removed the release-go1.3 label Apr 14, 2015
@golang golang locked and limited conversation to collaborators Jun 25, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants