Repository navigation
fix(proxy): return 502 when forwarding to upstream fails - #216
Open
a-palamarchuk wants to merge 1 commit into
Open
a-palamarchuk wants to merge 1 commit into
a-palamarchuk wants to merge 1 commit into
Conversation
When an allowed request could not be forwarded (for example, DNS failure or connection refused), forwardRequest logged the error and returned without writing a response. Transparent and explicit HTTP clients saw an empty reply. Inside a CONNECT tunnel the client waited for a response while the proxy waited for the next request, so the request hung until the client timed out. Write a 502 Bad Gateway with the reason whenever forwarding fails before a response has been written. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
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.
Problem
When an allowed request cannot be forwarded,
forwardRequestlogs the error and returns without writing a response. This happens, for example, when DNS fails, the connection is refused, or the upstream TLS handshake fails.Empty reply from server. The agent can't tell a firewall block from a network problem.handleCONNECTTunnelloops back tohttp.ReadRequestwhile the client is still waiting for its response. The request hangs until the client gives up.Change
writeBadGatewayResponse, modeled onwriteBlockedResponse. It writes502 Bad Gatewaywith a plain-text body naming the host and the forwarding error.forwardRequestcalls it on the three failures that happen before any response bytes are written: building the request,client.Do, and reading the upstream body. On the last one it also closes the upstream body.Connection: close, so a CONNECT tunnel stays usable after a failed request.Testing
New
TestProxyServerUpstreamUnreachablepoints an allowed host at a loopback port that refuses connections. It covers transparent HTTP, transparent HTTPS, explicit-proxy HTTP, and a CONNECT tunnel. The tunnel case sends two requests on one tunnel to show that it survives a failure, with a read deadline so a regression fails instead of hanging. Onmain, the first three subtests fail withEOFand the tunnel subtest times out. On this branch all four pass, including 10 runs under-race.End to end with landjail, allowing a name that never resolves.
e2e.shrunscurl -sS --max-time 10against the HTTP and HTTPS URLs and prints the status and elapsed time:make unit-test: all packages pass, except the two live-network proxy tests that also fail onmain(dev.coder.comnow redirects). test(proxy): serve proxy test upstreams locally #213 fixes those; with test(proxy): serve proxy test upstreams locally #213 merged in, the wholeproxypackage passes.make fmt-check: passes.Note: #212 (keep-alive) also edits
forwardRequest, and the two branches merge cleanly. #212 setsresp.Closeon the 403 response; whichever PR lands second should do the same for this 502 on the one-shot transparent paths.AI disclosure
I used Claude Code to trace the bug and draft the fix and tests. I reviewed the diff and ran the tests and the reproduction above myself.