Skip to content

Proxied servers are not notified of aborted client requests #85

Closed

Description

@Maiquu

Environment

node: 22.17.1
httpxy: 0.1.7

Reproduction

  1. Run following code where 2 servers are started. One acts as a proxy server for the other.
import { createServer } from "node:http";
import { setTimeout as delay } from 'node:timers/promises';
import { createProxyServer } from "httpxy";

const proxy = createProxyServer({});

const server = createServer(async (req, res) => {
  try {
    await proxy.web(req, res, {
      target: 'http://localhost:4000',
    });
  } catch (error) {
    console.error(error);
    res.statusCode = 500;
    res.end("Proxy error: " + error.toString());
  }
});

server.listen(3000, () => {
  console.log("Proxy is listening on http://localhost:3000");
});

const proxiedServer = createServer(async (req, res) => {
  req.on('close', () => {
    console.log('Request closed')
    if (req.aborted) {
      console.log('Request aborted by client')
    }
  })
  await delay(3000)

  res.writeHead(200, { 'Content-Type': 'text/plain' })
  res.end('Done')
})

proxiedServer.listen(4000, () => {
  console.log('Target is listening on http://localhost:4000')
})
  1. Execute curl http://localhost:3000 and abort request before its over
  2. Execute curl http://localhost:4000 and abort request before its over and observe differences

Describe the bug

When request is aborted by client, proxied server is not notified of this.

Additional context

I managed to fix this on my side via adding following to src/middleware/web-incoming.ts. Can open a PR.

@@ -114,6 +114,10 @@ const stream = defineProxyMiddleware(
       proxyReq.abort();
     });
 
+    res.on("close", function () {
+      proxyReq.abort();
+    })
+
     // handle errors in proxy and incoming request, just like for forward proxy
     const proxyError = createErrorHandler(proxyReq, options.target);
     req.on("error", proxyError);

Logs

Activity

  1. added
    bug
    on Sep 2, 2025
  2. added a commit that references this issue on Feb 20, 2026
    a5d4996
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions