Skip to content

Commit 5d8ee51

Browse files
theanarkhtargos
authored andcommitted
cluster: fix fd leak
PR-URL: #43650 Reviewed-By: Paolo Insogna <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent fa5c464 commit 5d8ee51

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

lib/internal/cluster/child.js

+2
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@ function onconnection(message, handle) {
210210

211211
if (accepted)
212212
server.onconnection(0, handle);
213+
else
214+
handle.close();
213215
}
214216

215217
function send(message, cb) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
const common = require('../common');
3+
const cluster = require('cluster');
4+
const net = require('net');
5+
6+
if (cluster.isPrimary) {
7+
cluster.schedulingPolicy = cluster.SCHED_RR;
8+
cluster.fork();
9+
} else {
10+
const server = net.createServer(common.mustNotCall());
11+
server.listen(0, common.mustCall(() => {
12+
net.connect(server.address().port);
13+
}));
14+
process.prependListener('internalMessage', common.mustCallAtLeast((message, handle) => {
15+
if (message.act !== 'newconn') {
16+
return;
17+
}
18+
// Make the worker drops the connection, see `rr` and `onconnection` in child.js
19+
server.close();
20+
const close = handle.close;
21+
handle.close = common.mustCall(() => {
22+
close.call(handle, common.mustCall(() => {
23+
process.exit();
24+
}));
25+
});
26+
}));
27+
}

0 commit comments

Comments
 (0)