-
Notifications
You must be signed in to change notification settings - Fork 18k
net/http: "use of closed network connection" error when reading 1MB or so #21760
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
Comments
Thanks @kevinburke, we assumed our code was broken for the longest time. |
Reproduces with Go 1.8 and 1.9 (linux/amd64) (not every run, though). |
I read the code find that the "use of closed network connection" error reason is, when net/http write request body to server, it read response from server at the same time with the same tcp connection. In your test case, the server first flush that lead to And I read https://www.w3.org/Protocols/rfc2616/rfc2616-sec8.html:
So in this test case, client sent body using a "chunked" encoding. so the right output maybe:
So the golang has a bug that it not send "chunked" end message when sees error of 'failReader'. |
It sounds like you believe net/http is violating RFC 2616. Can you explain why? I don't see a violation. When Request.Body.Read returns an error, we close the connection, which is allowed. The quoted passage from the RFC says that a client "MAY" send a zero length chunk to prematurely mark the end of the message. This is a MAY. We're not forced to, and in fact, we don't want to because doing so might result in the server incorrectly believing that we have sent the full message. This is simply a poor error message: We make an effort to prefer the error from Request.Body.Read (see link below), but this effort works only when the Request.Body.Read error happens before the server sends a response. Once the server sends a response, RoundTrip returns and the caller starts reading Response.Body, and the Request.Body.Read error is not propagated to Response.Body.Read. Line 1765 in f9cf8e5
As evidence for this: When the read limit is low, we get "look at my cool error" from http.DefaultClient.Do. When the read limit is high, we get "use of closed network connection" from io.Copy(buf, resp.Body). |
This is right, sorry for my invalid comment. |
What is the request here? To make Transport.RoundTrip prefer the Request.Body.Read error over the server conn read failure? The ~million bytes is probably because HTTP/1 servers try to consume their input before replying with any headers. (So the Flush is blocking, reading from the client). There's also a bug open somewhere to support bidirectional HTTP/1 like HTTP/2's always bidirectional support. (But HTTP/1 would probably need to be opt-in, not on by default). |
Related: #11745 |
@kevinburke, ping. |
Timeout. No replies for 2 weeks. Closing as dup of #11745. |
Originally reported as ipfs/kubo#3855 - my thanks to @timthelion, @whyrusleeping and others for helping debug.
What version of Go are you using (
go version
)?Tip
go version devel +812b34efae Mon Sep 4 00:07:33 2017 +0000 darwin/amd64
Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?What did you do?
Here's a test case exposing the error.
You can tweak the numbers as you see fit; it takes about a million bytes read to trigger the error.
What did you expect to see?
I expect to see something like
What did you see instead?
This odd error:
The text was updated successfully, but these errors were encountered: