diff options
author | Ben Noordhuis <info@bnoordhuis.nl> | 2017-09-29 11:29:58 +0200 |
---|---|---|
committer | Gibson Fahnestock <gibfahn@gmail.com> | 2017-10-31 00:15:15 +0000 |
commit | 9bea207e83a4b0be306bba8876402ab7dee7a391 (patch) | |
tree | 344cfe5cf69b26a4b0d5afc6f943fa958087d325 | |
parent | ad692074a49f89fa9c95642362259de989094243 (diff) | |
download | node-new-9bea207e83a4b0be306bba8876402ab7dee7a391.tar.gz |
child_process: fix memory leak in .fork()
Entries in the `net.Server#_slaves` 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
Backport-PR-URL: https://github.com/nodejs/node/pull/16586
Fixes: https://github.com/nodejs/node/issues/15651
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
-rw-r--r-- | lib/internal/socket_list.js | 1 | ||||
-rw-r--r-- | lib/net.js | 4 | ||||
-rw-r--r-- | test/parallel/test-child-process-fork-net2.js | 1 |
3 files changed, 6 insertions, 0 deletions
diff --git a/lib/internal/socket_list.js b/lib/internal/socket_list.js index 961289efeb..0d36b06cd7 100644 --- a/lib/internal/socket_list.js +++ b/lib/internal/socket_list.js @@ -8,6 +8,7 @@ class SocketListSend extends EventEmitter { super(); this.key = key; this.child = child; + child.once('exit', () => this.emit('exit', this)); } _request(msg, cmd, callback) { diff --git a/lib/net.js b/lib/net.js index 753bc42e6d..70d4841d5e 100644 --- a/lib/net.js +++ b/lib/net.js @@ -1664,6 +1664,10 @@ Server.prototype.listenFD = internalUtil.deprecate(function(fd, type) { Server.prototype._setupSlave = function(socketList) { this._usingSlaves = true; this._slaves.push(socketList); + socketList.once('exit', (socketList) => { + const index = this._slaves.indexOf(socketList); + this._slaves.splice(index, 1); + }); }; Server.prototype.ref = function() { diff --git a/test/parallel/test-child-process-fork-net2.js b/test/parallel/test-child-process-fork-net2.js index b893685447..995d2d4c6c 100644 --- a/test/parallel/test-child-process-fork-net2.js +++ b/test/parallel/test-child-process-fork-net2.js @@ -156,6 +156,7 @@ if (process.argv[2] === 'child') { } process.on('exit', function() { + assert.strictEqual(server._slaves.length, 0); assert.strictEqual(disconnected, count); assert.strictEqual(connected, count); }); |