node-http-mitm-proxy icon indicating copy to clipboard operation
node-http-mitm-proxy copied to clipboard

ProxyToServer connection timeout is not handled

Open fopinappb opened this issue 1 year ago • 1 comments

Recently I bumped into the issue where my library (that uses this) was hanging after termination.

Debugging showed event loop was filled with TLSWrap and socket calls which led me to connection timeouts and to these lines:

https://github.com/joeferner/node-http-mitm-proxy/blob/310a3bc8918b07b9c7dbdb345e358f8be2a74044/lib/proxy.ts#L1099-L1102

error event does not include timeout, so even after clients give up and close connection (ClientToProxy), these were left hanging.

Google led me to https://stackoverflow.com/a/55021202 and I've added

   ctx.proxyToServerRequest.on("error", self._onError.bind(self, "PROXY_TO_SERVER_REQUEST_ERROR", ctx));
+ ctx.proxyToServerRequest.on("timeout", () => {ctx.proxyToServerRequest.destroy()});

The destroy() is now triggering on timeout and then triggering the previously set error handler.

I haven't opened a PR with this as I'm not sure of its impact, leaving this up as an issue 😄

Calling destroy() on the http agents also destroys all open sockets and properly allows termination.

Currently doing this when calling Proxy.close()

fopinappb avatar Apr 11 '23 11:04 fopinappb

same issue here error

RikaShiro avatar Apr 23 '23 13:04 RikaShiro