Skip to content

Commit a66c421

Browse files
committed
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: nodejs#15679 Fixes: nodejs#15651 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent 4359a93 commit a66c421

File tree

3 files changed

+6
-0
lines changed

3 files changed

+6
-0
lines changed

lib/internal/socket_list.js

Lines changed: 1 addition & 0 deletions
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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1667,6 +1667,10 @@ Server.prototype.listenFD = internalUtil.deprecate(function(fd, type) {
16671667
Server.prototype._setupSlave = function(socketList) {
16681668
this._usingSlaves = true;
16691669
this._slaves.push(socketList);
1670+
socketList.once('exit', (socketList) => {
1671+
const index = this._slaves.indexOf(socketList);
1672+
this._slaves.splice(index, 1);
1673+
});
16701674
};
16711675

16721676
Server.prototype.ref = function() {

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

Lines changed: 1 addition & 0 deletions
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)