net/http: request.Body.Close can be called more than once #40382
Labels
FrozenDueToAge
NeedsInvestigation
Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Context: doing a large PUT on a remote server ( > 200 kB) with no specified Content-Length (using T-E: chunked)
I am passing a io.ReadCloser to request.Body and I want to avoid a double call on close (these are supposed to be undefined behaviour).
But sometimes, when the remote server is not consuming the entire payload and returns an error, Close() is called twice.
The following code reproduce the issue on my laptop. This may require multiple runs.
What did you expect to see?
Nothing, Close() should be called only once.
What did you see instead?
With a first glance at the code, I can see a reason for that.
In T-E: chunked, the net/http.(*transferWriter).writeBody function will close the body and then, will write the EOF chunk. This write and all the following "Flush" calls on the writer may return an error.
If there is an error, writeLoop() interpret it as a signal that the body is not closed. So any error happening after the actual close of the body in writeBody() will lead to a double close.
The text was updated successfully, but these errors were encountered: