Skip to content

Commit

Permalink
child_process: fix memory leak in .fork()
Browse files Browse the repository at this point in the history
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: nodejs#15679
Fixes: nodejs#15651
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
  • Loading branch information
bnoordhuis authored and jasnell committed Oct 1, 2017
1 parent 3e4f34c commit 2e59ec0
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/internal/socket_list.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
1 change: 1 addition & 0 deletions test/parallel/test-child-process-fork-net2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down

0 comments on commit 2e59ec0

Please sign in to comment.