child_process: fix memory leak in .fork()

Entries in the `net.Server#_workers` array that is used to track handles
sent from the master to workers were not deleted when a worker exited,
resulting in a slow but inexorable memory leak.

PR-URL: https://github.com/nodejs/node/pull/15679
Fixes: https://github.com/nodejs/node/issues/15651
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
Ben Noordhuis 2017-09-29 11:29:58 +02:00 committed by James M Snell
parent 3e4f34cdb9
commit 2e59ec0c2d
3 changed files with 6 additions and 0 deletions

View File

@ -10,6 +10,7 @@ class SocketListSend extends EventEmitter {
super();
this.key = key;
this.child = child;
child.once('exit', () => this.emit('exit', this));
}
_request(msg, cmd, callback) {

View File

@ -1665,6 +1665,10 @@ Server.prototype.listenFD = internalUtil.deprecate(function(fd, type) {
Server.prototype._setupWorker = function(socketList) {
this._usingWorkers = true;
this._workers.push(socketList);
socketList.once('exit', (socketList) => {
const index = this._workers.indexOf(socketList);
this._workers.splice(index, 1);
});
};
Server.prototype.ref = function() {

View File

@ -156,6 +156,7 @@ if (process.argv[2] === 'child') {
}
process.on('exit', function() {
assert.strictEqual(server._workers.length, 0);
assert.strictEqual(disconnected, count);
assert.strictEqual(connected, count);
});