Skip to content
This repository was archived by the owner on Aug 31, 2018. It is now read-only.

Commit 912f9d3

Browse files
bnoordhuisaddaleax
authored andcommitted
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: nodejs/node#15679 Fixes: nodejs/node#15651 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent d86e347 commit 912f9d3

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
@@ -10,6 +10,7 @@ class SocketListSend extends EventEmitter {
1010
super();
1111
this.key = key;
1212
this.child = child;
13+
child.once('exit', () => this.emit('exit', this));
1314
}
1415

1516
_request(msg, cmd, callback) {

lib/net.js

+4
Original file line numberDiff line numberDiff line change
@@ -1665,6 +1665,10 @@ Server.prototype.listenFD = internalUtil.deprecate(function(fd, type) {
16651665
Server.prototype._setupWorker = function(socketList) {
16661666
this._usingWorkers = true;
16671667
this._workers.push(socketList);
1668+
socketList.once('exit', (socketList) => {
1669+
const index = this._workers.indexOf(socketList);
1670+
this._workers.splice(index, 1);
1671+
});
16681672
};
16691673

16701674
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._workers.length, 0);
159160
assert.strictEqual(disconnected, count);
160161
assert.strictEqual(connected, count);
161162
});

0 commit comments

Comments
 (0)