reverseproxy: prevent body close on dial-error retries#7547
Merged
francislavoie merged 1 commit intocaddyserver:masterfrom Mar 4, 2026
Merged
Conversation
) cloneRequest shallow-copies Body, so clonedReq.Body and r.Body share the same io.ReadCloser. When Go's transport hits a dial error it calls req.Body.Close() before reading any bytes, which kills the original body for all subsequent retry attempts ("http: invalid Read on closed Body" → 502). Wrap the body in io.NopCloser when retries are configured so the transport's Close is a no-op. The real body is closed by the HTTP server when the handler returns. For already-buffered bodies (via request_buffers) the buffer is extracted into the same NopCloser path so both cases share a single code block. Unlike full eager buffering this adds zero memory or latency overhead — streaming is preserved and only the Close call is intercepted.
francislavoie
approved these changes
Mar 4, 2026
Member
francislavoie
left a comment
There was a problem hiding this comment.
Yeah, that makes a lot of sense. Didn't realize dials would close the body, that's surprising, but to be expected I suppose.
Thanks!
Contributor
Author
|
Props go to @jafowler for interrogating the AI long enough to identify and repro the issue. |
4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #7546
cloneRequest shallow-copies Body, so clonedReq.Body and r.Body share the same io.ReadCloser. When Go's transport hits a dial error it calls req.Body.Close() before reading any bytes, which kills the original body for all subsequent retry attempts ("http: invalid Read on closed Body" → 502).
Wrap the body in io.NopCloser when retries are configured so the transport's Close is a no-op. The real body is closed by the HTTP server when the handler returns. For already-buffered bodies (via request_buffers) the buffer is extracted into the same NopCloser path so both cases share a single code block.
Assistance Disclosure
AI debugged the issue. I wrote the code, and AI generated the tests & commit message.