Skip to content

Commit

Permalink
Fix 'aborted' detection on Node v15.5.0+
Browse files Browse the repository at this point in the history
This fixes a major memory leak I encountered usign this on Node v16.
Doing a binary search of node versions, the problem appears to originate in v15.5.0 as a result of nodejs/node#33035, however later changes have quietly completely removed the 'aborted' event that `http-proxy` relies on, and later added a deprecation note about it (which seems to actually be incorrect).
Despite what the notes about [DEP0156](https://nodejs.org/dist/latest-v16.x/docs/api/deprecations.html#DEP0156) say, the only way I could get this module working reliably again was to replace `req.on('aborted')` with instead checking `res.on('close')` and looking at `res.writeableFinished`.
  • Loading branch information
Jimbly committed May 12, 2023
1 parent 9b96cd7 commit 058182a
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions lib/http-proxy/passes/web-incoming.js
Expand Up @@ -18,6 +18,11 @@ var nativeAgents = { http: httpNative, https: httpsNative };
* flexible.
*/

// 'aborted' event stopped working reliably on v15.5.0 and was later removed entirely
var supportsAbortedEvent = (function () {
var ver = process.versions.node.split('.').map(Number);
return ver[0] <= 14 || ver[0] === 15 && ver[1] <= 4;
}());

module.exports = {

Expand Down Expand Up @@ -143,9 +148,18 @@ module.exports = {
}

// Ensure we abort proxy if request is aborted
req.on('aborted', function () {
proxyReq.abort();
});
if (supportsAbortedEvent) {
req.on('aborted', function () {
proxyReq.abort();
});
} else {
res.on('close', function () {
var aborted = !res.writableFinished;
if (aborted) {
proxyReq.abort();
}
});
}

// handle errors in proxy and incoming request, just like for forward proxy
var proxyError = createErrorHandler(proxyReq, options.target);
Expand Down

0 comments on commit 058182a

Please sign in to comment.