Skip to content

Commit b72a6b1

Browse files
authored
fix memory leak
For many reasons, node caches and leaves in memory (for pretty lengthy amount of time) HTTPRequest instances. Form-data doesn't unsubscribe from 'error' and 'response' event, leaving references to finish / callback. This is kind-of okay in most cases (HTTPRequest's do die after a while), it might be a problem with large payloads or other memory-hungry situations. This was only tested with NodeJS 8 - future versions might change how they are working with HTTPRequest's objects.
1 parent cd27e9a commit b72a6b1

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

lib/form_data.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,8 +462,17 @@ FormData.prototype.submit = function(params, cb) {
462462

463463
this.pipe(request);
464464
if (cb) {
465-
request.on('error', cb);
466-
request.on('response', cb.bind(this, null));
465+
function callback(err, res) {
466+
request.removeListener('error', callback);
467+
request.removeListener('response', onResponse);
468+
469+
return cb.call(this, err, res);
470+
}
471+
472+
const onResponse = callback.bind(this, null);
473+
474+
request.on('error', callback);
475+
request.on('response', onResponse);
467476
}
468477
}.bind(this));
469478

0 commit comments

Comments
 (0)