Skip to content

Commit 9bea207

Browse files
bnoordhuisgibfahn
authored andcommitted
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: #15679 Backport-PR-URL: #16586 Fixes: #15651 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent ad69207 commit 9bea207

File tree

3 files changed

+6
-0
lines changed

3 files changed

+6
-0
lines changed

lib/internal/socket_list.js

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class SocketListSend extends EventEmitter {
88
super();
99
this.key = key;
1010
this.child = child;
11+
child.once('exit', () => this.emit('exit', this));
1112
}
1213

1314
_request(msg, cmd, callback) {

lib/net.js

+4
Original file line numberDiff line numberDiff line change
@@ -1664,6 +1664,10 @@ Server.prototype.listenFD = internalUtil.deprecate(function(fd, type) {
16641664
Server.prototype._setupSlave = function(socketList) {
16651665
this._usingSlaves = true;
16661666
this._slaves.push(socketList);
1667+
socketList.once('exit', (socketList) => {
1668+
const index = this._slaves.indexOf(socketList);
1669+
this._slaves.splice(index, 1);
1670+
});
16671671
};
16681672

16691673
Server.prototype.ref = function() {

test/parallel/test-child-process-fork-net2.js

+1
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ if (process.argv[2] === 'child') {
156156
}
157157

158158
process.on('exit', function() {
159+
assert.strictEqual(server._slaves.length, 0);
159160
assert.strictEqual(disconnected, count);
160161
assert.strictEqual(connected, count);
161162
});

0 commit comments

Comments
 (0)