Repository navigation
Description
Evidence
bin/proxy-relay.mjs pipes the client and tunnel sockets after the CONNECT succeeds (lines 186-192 on origin/main 80c787a):
client.on('error', () => client.destroy())(line 149) destroys the client on error but never touches the upstream.upstream.on('error', ...)(line 186) only handles upstream errors.client.pipe(upstream)/upstream.pipe(client)(lines 191-192) only end the destination on the source'send. A client that is destroyed or reset does not emitend, so the upstream is never ended or destroyed.
Reproduction
Script: fake tunnel proxy on 127.0.0.1 that answers CONNECT with 200 and records socket events; relay started with RELAY_PROXY_HOST=127.0.0.1 pointing at the fake; client sends a POST, waits 500 ms, then:
- Client
resetAndDestroy()(RST, as an aborted SSE or streaming request produces): fake-side events stay['data:56']after 1.5 s. Noend, noclose. The tunnel socket is left open. - Control: client
end(): fake-side events become['data:56', 'end', 'close']. The graceful path tears down correctly.
Each aborted streaming client therefore leaves one CONNECT tunnel socket to the laptop open until the far end gives up.
Why it matters
The relay is the sandbox's only path to the laptop-hosted fleet proxies. Leaked tunnel sockets consume the tunnel proxy's connection budget and sit idle with unread buffers. bin/proxy-relay.test.mjs covers the first-line deadline, the CONNECT window and failover, but not client reset.
Overlap with open PR
Open PR #4437 (fix(proxy-relay): pause client during tunnel CONNECT selection (#2847)) adds client.once('close', () => upstream.destroy()) and client.once('error', () => upstream.destroy()) in the same pipe setup. It is not merged, and this issue has not been checked against its head. Whichever lands first, the fix should be verified with the reset regression test below. Other open relay PRs (#4034, #4137, #4557) do not change the teardown path.
Suggested fix shape
Tie the upstream's lifetime to the client's, and the client's to the upstream's:
client.on('close', () => upstream.destroy());
upstream.on('close', () => client.destroy());Acceptance:
- A regression test in
bin/proxy-relay.test.mjsthat resets the client mid-stream and asserts the upstream socket receivesclosewithin a bounded time, using the fake-proxy pattern of the existing CONNECT-window test. - Graceful-close behavior (
endpropagates) is unchanged.
Dedupe
Searched open and closed issues for proxy-relay and relay socket. Closed #2234, #2847, #3082, #1844 and #4224 are other proxy-relay defects. None covers upstream teardown on client reset.
No fix, PR or queue task created by this filing.
Acceptance criteria
- Reproduction script from the issue (client
resetAndDestroy()against the fake tunnel) shows the upstream tunnel socket receivingend/closeafter the fix, matching the graceful-close control. - Graceful client
end()behaviour is unchanged (existing teardown path still works). - No regression: normal CONNECT proxying and upstream-error handling covered by a test or scripted check committed with the fix.
Activity
developing-today commentedon Nov 24, 2026
CODE-5293
dezren39 commentedon Nov 24, 2026
Cross-link: open PR #4437 (pause client during tunnel CONNECT selection) adds an upstream teardown on client close/error. This issue is the ledger entry for the reset leak; please verify #4437 against the reset regression test here before merge.
developing-today-fleet commentedon Nov 24, 2026
Intake review
Related: older proxy-relay fixes PR #4034 and PR #4557 (different defects — port validation and endpoint failover); no merged PR addresses client-reset teardown. Notes: the leak is per aborted streaming request, so long-lived relays accumulate open CONNECT tunnels to the laptop until the far end times out. Suggested approach: destroy/end the upstream on client
error/close(not onlyend), symmetrically with the existing upstream-error handler, and guard against double-destroy. Risk: low — teardown-only change inbin/proxy-relay.mjs.🤖 Agent note · filed
fix(proxy-relay): tear the tunnel down when the client closes (#14337)
dezren39 commentedon Nov 24, 2026
Fix in PR #14501: after the pipe,
client.on('close', …upstream.destroy())andupstream.on('close', …client.destroy()). Regression testa client reset tears down the tunnel socketfails before the fix and passes after; all 4bin/proxy-relay.test.mjstests pass.