Skip to content

Commit 1929d5b

Browse files
Trottrvagg
authored andcommitted
lib: fix cluster handle leak
It is possible to cause a resource leak in SharedHandle. This commit fixes the leak. Fixes: #2510 PR-URL: #3510 Reviewed-By: Ben Noordhuis <[email protected]>
1 parent 4c9abbd commit 1929d5b

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

lib/cluster.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,10 @@ function masterInit() {
345345
* if it has disconnected, otherwise we might
346346
* still want to access it.
347347
*/
348-
if (!worker.isConnected()) removeWorker(worker);
348+
if (!worker.isConnected()) {
349+
removeHandlesForWorker(worker);
350+
removeWorker(worker);
351+
}
349352

350353
worker.suicide = !!worker.suicide;
351354
worker.state = 'dead';
+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// In Node 4.2.1 on operating systems other than Linux, this test triggers an
2+
// assertion in cluster.js. The assertion protects against memory leaks.
3+
// https://github.com/nodejs/node/pull/3510
4+
5+
'use strict';
6+
const common = require('../common');
7+
const assert = require('assert');
8+
const net = require('net');
9+
const 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+
conn = net.connect(common.PORT, common.mustCall(function() {
19+
worker1.send('die');
20+
worker2.send('die');
21+
}));
22+
conn.on('error', function(e) {
23+
// ECONNRESET is OK
24+
if (e.code !== 'ECONNRESET')
25+
throw e;
26+
});
27+
}));
28+
29+
cluster.on('exit', function(worker, exitCode, signalCode) {
30+
assert(worker === worker1 || worker === worker2);
31+
assert.strictEqual(exitCode, 0);
32+
assert.strictEqual(signalCode, null);
33+
if (Object.keys(cluster.workers).length === 0)
34+
conn.destroy();
35+
});
36+
37+
return;
38+
}
39+
40+
var server = net.createServer(function(c) {
41+
c.end('bye');
42+
});
43+
44+
server.listen(common.PORT, function() {
45+
process.send('listening');
46+
});
47+
48+
process.on('message', function(msg) {
49+
if (msg !== 'die') return;
50+
server.close(function() {
51+
setImmediate(() => process.disconnect());
52+
});
53+
});

0 commit comments

Comments
 (0)