Skip to content

Commit d51cecf

Browse files
Jimblymuratso
authored andcommitted
Fix 'aborted' detection on Node v15.5.0+
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`.
1 parent 9b96cd7 commit d51cecf

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

lib/http-proxy/passes/web-incoming.js

+17-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ var nativeAgents = { http: httpNative, https: httpsNative };
1818
* flexible.
1919
*/
2020

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

2227
module.exports = {
2328

@@ -143,9 +148,18 @@ module.exports = {
143148
}
144149

145150
// Ensure we abort proxy if request is aborted
146-
req.on('aborted', function () {
147-
proxyReq.abort();
148-
});
151+
if (hasAborted) {
152+
req.on('aborted', function () {
153+
proxyReq.abort();
154+
});
155+
} else {
156+
res.on('close', function () {
157+
var aborted = !res.writableFinished;
158+
if (aborted) {
159+
proxyReq.abort();
160+
}
161+
});
162+
}
149163

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

0 commit comments

Comments
 (0)