fix(cliproxy): clean up upstream proxy on response disconnect (#1361)

* fix(cliproxy): clean up upstream proxy on response disconnect

* style: apply prettier formatting
This commit is contained in:
Kai (Tam Nhu) Tran
2026-05-23 21:44:37 -04:00
committed by GitHub
parent e658e838f9
commit 2188cdc990
+13 -5
View File
@@ -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) {