caddy/modules/caddyhttp/reverseproxy
Rob Martin 00f8e91c94
reverseproxy: close backend body and hijacked socket on upgrade setup failures (#8269)
* reverseproxy: close backend body and hijacked socket on upgrade setup failures

When an upstream response indicates an HTTP upgrade (101 Switching Protocols),
handleUpgradeResponse takes ownership of res.Body and subsequently hijacks
the client connection.

However, if any setup check fails prior to starting handleUpgradeTunnel
(such as mismatched upgrade headers, non-writable body assertion failure,
hijack unsupported, or handshake buffer flush errors), the function returned
early without closing res.Body or the hijacked client socket.

Because calling Hijack() permanently disables net/http's automatic connection
cleanup, and raw TCP sockets are not closed by the garbage collector, these
early failure exits leaked file descriptors into CLOSE_WAIT/ESTABLISHED.

Fix this by:
1. Adding a deferred cleanup guard that closes res.Body and any hijacked
   front connection unless the tunnel successfully takes ownership.
2. Wrapping connections with onceCloseConn to ensure idempotent closure
   while preserving optional CloseWrite (TCP half-close) support.
3. Adding TestUpgradeResponseCleanupOnFailure to verify cleanup.

* reverseproxy: test post-hijack cleanup and remove once-close wrappers

Cover handshake flush and buffered backend-write failures after successful hijacking, checking that both connections close. Keep setup-failure cleanup while using the original connections directly.

Code and tests were AI-assisted using an OpenAI coding assistant in Pi.

(cherry picked from commit 62bc606433)
2026-11-03 00:53:19 -05:00
..
fastcgi reverseproxy: preserve request trailer values (#8246) 2026-11-03 00:53:19 -05:00
forwardauth forwardauth: error on duplicate uri subdirective (#7814) 2026-06-10 23:31:04 -04:00
active_health_test.go reverseproxy: replace only known placeholders in health check body (#8003) 2026-09-09 08:48:08 -06:00
addresses.go
addresses_test.go
admin.go reverseproxy: Track dynamic upstreams, enable passive healthchecking (#7539) 2026-03-04 15:05:26 -05:00
admin_test.go reverseproxy: Track dynamic upstreams, enable passive healthchecking (#7539) 2026-03-04 15:05:26 -05:00
ascii.go
ascii_test.go
buffering_test.go reverseproxy: Don't drop read errors with unlimited buffers (#8207) 2026-11-03 00:53:18 -05:00
caddyfile.go feat: implement the Incremental header field (RFC 10036) (#8020) 2026-10-02 20:59:34 -04:00
client_disconnect_test.go reverseproxy: log HTTP/3 client cancellations at debug level (#8135) 2026-11-03 00:52:58 -05:00
command.go cmd: prevent commas in header values from being split (#7268) 2025-09-22 21:12:06 -06:00
copyresponse.go
dynamic_upstreams_test.go reverseproxy: Track dynamic upstreams, enable passive healthchecking (#7539) 2026-03-04 15:05:26 -05:00
headers_test.go reverseproxy: strip hop-by-hop headers from interim responses (#8256) 2026-11-03 00:53:19 -05:00
healthchecks.go reverseproxy: replace only known placeholders in health check body (#8003) 2026-09-09 08:48:08 -06:00
hopheaders_test.go Merge commit from fork 2026-06-12 12:39:01 -06:00
hosts.go reverseproxy: isolate active health-check state per distinct check config (#7916) 2026-08-12 15:20:15 +10:00
httptransport.go build: bump all dependencies - (Go 1.26 floor) (#8056) 2026-09-30 10:54:12 -06:00
httptransport_test.go reverse_proxy with versions 3 silently ignores tls_trust_pool and verifies against the system roots (#8042) 2026-09-22 08:13:02 +10:00
incremental_test.go feat: implement the Incremental header field (RFC 10036) (#8020) 2026-10-02 20:59:34 -04:00
metrics.go
passive_health_test.go reverseproxy: Track dynamic upstreams, enable passive healthchecking (#7539) 2026-03-04 15:05:26 -05:00
prepare_request_bench_test.go reverseproxy: reduce allocation/CPU overhead in request hot paths (#7925) 2026-08-10 15:49:55 +10:00
request_trailers_test.go reverseproxy: preserve request trailer values (#8246) 2026-11-03 00:53:19 -05:00
retries_test.go reverseproxy: retry dial errors wrapped by a forward proxy (#8264) 2026-11-03 00:53:19 -05:00
reverseproxy.go reverseproxy: retry dial errors wrapped by a forward proxy (#8264) 2026-11-03 00:53:19 -05:00
selectionpolicies.go reverseproxy: Don't panic in weighted_round_robin with fewer weights than upstreams (#8159) 2026-11-03 00:53:03 -05:00
selectionpolicies_test.go reverseproxy: Don't panic in weighted_round_robin with fewer weights than upstreams (#8159) 2026-11-03 00:53:03 -05:00
streaming.go reverseproxy: close backend body and hijacked socket on upgrade setup failures (#8269) 2026-11-03 00:53:19 -05:00
streaming_test.go reverseproxy: close backend body and hijacked socket on upgrade setup failures (#8269) 2026-11-03 00:53:19 -05:00
upstreams.go reverseproxy: preallocate upstream slices with known sizes (#7926) 2026-08-10 15:51:50 +10:00
upstreams_bench_test.go reverseproxy: preallocate upstream slices with known sizes (#7926) 2026-08-10 15:51:50 +10:00
upstreams_test.go chore: upgrade .golangci.yml and workflow to v2 (#6924) 2025-06-03 02:24:32 +03:00