Skip to content

Commit d421e85

Browse files
TrottMyles Borins
authored and
Myles Borins
committed
lib: fix cluster handle leak
It is possible to cause a resource leak in SharedHandle. This commit fixes the leak. Fixes: #2510 PR-URL: #5152 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 6c468df commit d421e85

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

lib/cluster.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,10 @@ function masterInit() {
363363
* if it has disconnected, otherwise we might
364364
* still want to access it.
365365
*/
366-
if (!worker.isConnected()) removeWorker(worker);
366+
if (!worker.isConnected()) {
367+
removeHandlesForWorker(worker);
368+
removeWorker(worker);
369+
}
367370

368371
worker.suicide = !!worker.suicide;
369372
worker.state = 'dead';
+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// On some platforms this test triggers an assertion in cluster.js.
2+
// The assertion protects against memory leaks.
3+
// https://github.com/nodejs/node/pull/3510
4+
5+
'use strict';
6+
var common = require('../common');
7+
var assert = require('assert');
8+
var net = require('net');
9+
var cluster = require('cluster');
10+
cluster.schedulingPolicy = cluster.SCHED_NONE;
11+
12+
if (cluster.isMaster) {
13+
var conn, worker1, worker2;
14+
15+
worker1 = cluster.fork();
16+
worker1.on('message', common.mustCall(function() {
17+
worker2 = cluster.fork();
18+
worker2.on('online', function() {
19+
conn = net.connect(common.PORT, common.mustCall(function() {
20+
worker1.disconnect();
21+
worker2.disconnect();
22+
}));
23+
conn.on('error', function(e) {
24+
// ECONNRESET is OK
25+
if (e.code !== 'ECONNRESET')
26+
throw e;
27+
});
28+
});
29+
}));
30+
31+
cluster.on('exit', function(worker, exitCode, signalCode) {
32+
assert(worker === worker1 || worker === worker2);
33+
assert.strictEqual(exitCode, 0);
34+
assert.strictEqual(signalCode, null);
35+
if (Object.keys(cluster.workers).length === 0)
36+
conn.destroy();
37+
});
38+
39+
return;
40+
}
41+
42+
var server = net.createServer(function(c) {
43+
c.end('bye');
44+
});
45+
46+
server.listen(common.PORT, function() {
47+
process.send('listening');
48+
});

0 commit comments

Comments
 (0)