Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: fix cluster handle leak #5152

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,10 @@ function masterInit() {
* if it has disconnected, otherwise we might
* still want to access it.
*/
if (!worker.isConnected()) removeWorker(worker);
if (!worker.isConnected()) {
removeHandlesForWorker(worker);
removeWorker(worker);
}

worker.suicide = !!worker.suicide;
worker.state = 'dead';
Expand Down
48 changes: 48 additions & 0 deletions test/simple/test-cluster-shared-leak.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// On some platforms this test triggers an assertion in cluster.js.
// The assertion protects against memory leaks.
// https://github.com/nodejs/node/pull/3510

'use strict';
var common = require('../common');
var assert = require('assert');
var net = require('net');
var cluster = require('cluster');
cluster.schedulingPolicy = cluster.SCHED_NONE;

if (cluster.isMaster) {
var conn, worker1, worker2;

worker1 = cluster.fork();
worker1.on('message', common.mustCall(function() {
worker2 = cluster.fork();
worker2.on('online', function() {
conn = net.connect(common.PORT, common.mustCall(function() {
worker1.disconnect();
worker2.disconnect();
}));
conn.on('error', function(e) {
// ECONNRESET is OK
if (e.code !== 'ECONNRESET')
throw e;
});
});
}));

cluster.on('exit', function(worker, exitCode, signalCode) {
assert(worker === worker1 || worker === worker2);
assert.strictEqual(exitCode, 0);
assert.strictEqual(signalCode, null);
if (Object.keys(cluster.workers).length === 0)
conn.destroy();
});

return;
}

var server = net.createServer(function(c) {
c.end('bye');
});

server.listen(common.PORT, function() {
process.send('listening');
});