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
net/http.Client.Do states that the Body must both be read to EOF and closed in order for a connection to be reused. If this does not occur the next request to Do for that server will fail with an error.
If the Body is not both read to EOF and closed, the Client's underlying RoundTripper (typically Transport) may not be able to re-use a persistent TCP connection to the server for a subsequent "keep-alive" request.
Looking at the implementation details of Decode, it will read 512 bytes at a time. This means that if the buffer contains a single JSON message which has a byte length that is a multiple of 512, Decoder will read the JSON message and leave the trailing newline: https://go.dev/play/p/ZHrpoVGlizA
If an HTTP client uses Decode to parse a JSON message returned by a server it will occasionally fail due to the body not being read to EOF.
What did you expect to see?
No errors returned from subsequent HTTP client requests.
What did you see instead?
Intermittent EOF errors.
The text was updated successfully, but these errors were encountered:
Sounds like you should make sure you decode the entire body rather than just one JSON value. For that, see #36225.
Thanks - this is the approach we've taken to solve the problem. I appreciate the linked issue, it speaks to exactly this issue. I filed this task in hopes of helping the next person who Googles for intermittent EOF errors during HTTP requests as it's the intersection of these 3 common APIs which created the problem.
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?
Filing this task primarily as documentation in case someone else runs into this.
encoding/json.Encoder.Encode writes a JSON message plus a trailing newline, as described in the docs.
encoding/json.Decoder.Decode reads a JSON message from a buffer, as described.
net/http.Client.Do states that the Body must both be read to EOF and closed in order for a connection to be reused. If this does not occur the next request to
Do
for that server will fail with an error.Looking at the implementation details of Decode, it will read 512 bytes at a time. This means that if the buffer contains a single JSON message which has a byte length that is a multiple of 512, Decoder will read the JSON message and leave the trailing newline: https://go.dev/play/p/ZHrpoVGlizA
If an HTTP client uses Decode to parse a JSON message returned by a server it will occasionally fail due to the body not being read to EOF.
What did you expect to see?
No errors returned from subsequent HTTP client requests.
What did you see instead?
Intermittent EOF errors.
The text was updated successfully, but these errors were encountered: