Skip to content

Commit 2109708

Browse files
Trottjasnell
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 <info@bnoordhuis.nl>
1 parent 32149ca commit 2109708

2 files changed

Lines changed: 57 additions & 1 deletion

File tree

‎lib/cluster.js‎

Lines changed: 4 additions & 1 deletion
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';
Lines changed: 53 additions & 0 deletions
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+
constcommon=require('../common');
7+
constassert=require('assert');
8+
constnet=require('net');
9+
constcluster=require('cluster');
10+
cluster.schedulingPolicy=cluster.SCHED_NONE;
11+
12+
if(cluster.isMaster){
13+
varconn,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+
throwe;
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+
varserver=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)