From 2188cdc9907cf0934f074a9503501edd68f708e8 Mon Sep 17 00:00:00 2001 From: "Kai (Tam Nhu) Tran" <61256810+kaitranntt@users.noreply.github.com> Date: Sat, 23 May 2026 21:44:37 -0400 Subject: [PATCH] fix(cliproxy): clean up upstream proxy on response disconnect (#1361) * fix(cliproxy): clean up upstream proxy on response disconnect * style: apply prettier formatting --- src/web-server/routes/cliproxy-local-proxy.ts | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/web-server/routes/cliproxy-local-proxy.ts b/src/web-server/routes/cliproxy-local-proxy.ts index 9c512e96..4454a297 100644 --- a/src/web-server/routes/cliproxy-local-proxy.ts +++ b/src/web-server/routes/cliproxy-local-proxy.ts @@ -134,13 +134,21 @@ export function createCliproxyLocalProxyRouter(deps: CliproxyLocalProxyDeps = {} } }); - // Clean up proxy connection only when the client aborts the request. - // Avoid res.on('close') here because Bun may emit it during local error - // responses before the JSON body is flushed, which can truncate 502 payloads. - req.on('aborted', () => { - if (!res.writableEnded) { + const cleanupProxyRequest = () => { + if (!proxyReq.destroyed) { proxyReq.destroy(); } + }; + + // Request-abort cleanup covers disconnects before the response starts. + req.on('aborted', cleanupProxyRequest); + + // Response close cleanup covers disconnects while streaming the proxied response. + // Guard on writableEnded/finished so successful proxy completions are untouched. + res.on('close', () => { + if (!res.writableEnded || !res.finished) { + cleanupProxyRequest(); + } }); if (bodyBuffer) {