Skip to content

Commit a5d4996

Browse files
pi0Maiquuclaude
committed
fix(web-incoming): abort proxy request when client disconnects
Closes #85 Co-authored-by: Maiquu <[email protected]> Co-Authored-By: Claude Opus 4.6 <[email protected]>
1 parent 822a0ea commit a5d4996

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

‎src/middleware/web-incoming.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,13 @@ export const stream = defineProxyMiddleware((req, res, options, server, head, ca
119119
proxyReq.abort();
120120
});
121121

122+
// Abort proxy request when client disconnects
123+
res.on("close", function () {
124+
if (!res.writableFinished) {
125+
proxyReq.destroy();
126+
}
127+
});
128+
122129
// handle errors in proxy and incoming request, just like for forward proxy
123130
const proxyError = createErrorHandler(proxyReq, options.target);
124131
req.on("error", proxyError);

‎test/middleware/web-incoming.test.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,42 @@ describe("#createProxyServer.web() using own http server", () => {
573573
});
574574
});
575575

576+
describe("#client abort propagation", () => {
577+
it("should abort proxy request when client disconnects", async () => {
578+
const { resolve, promise } = Promise.withResolvers<void>();
579+
580+
// Target server that waits long enough for client to abort
581+
const source = http.createServer((req, _res) => {
582+
req.on("close", () => {
583+
source.close();
584+
proxyServer.close();
585+
resolve();
586+
});
587+
});
588+
const sourcePort = await listenOn(source);
589+
590+
const proxy = httpProxy.createProxyServer({
591+
target: `http://127.0.0.1:${sourcePort}`,
592+
});
593+
594+
const proxyServer = http.createServer((req, res) => {
595+
proxy.web(req, res);
596+
});
597+
const proxyPort = await listenOn(proxyServer);
598+
599+
// Make a request and abort it immediately
600+
const req = http.request(`http://127.0.0.1:${proxyPort}`, () => {});
601+
req.on("error", () => {}); // Ignore client-side errors from destroy
602+
req.end();
603+
req.on("socket", () => {
604+
// Destroy after connection is established
605+
setTimeout(() => req.destroy(), 50);
606+
});
607+
608+
await promise;
609+
});
610+
});
611+
576612
describe("#followRedirects", () => {
577613
it("should follow 301 redirect", async () => {
578614
const { resolve, promise } = Promise.withResolvers<void>();

0 commit comments

Comments
 (0)