Skip to content

Commit 210ae56

Browse files
sam-githubMylesBorins
authored andcommitted
test: prevent workers outliving parent
test-child-process-pass-fd.js parent can exit with an error on failure to fork, in which case it will leak child processes. Limit child lifetime to that of parent. Fixes: #9255 PR-URL: #9257 Reviewed-By: Gibson Fahnestock <[email protected]> Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 07d34f9 commit 210ae56

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

test/sequential/test-child-process-pass-fd.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,23 @@ if (process.argv[2] !== 'child') {
3939
process.send('handle', socket);
4040
}
4141

42+
// As a side-effect, listening for the message event will ref the IPC channel,
43+
// so the child process will stay alive as long as it has a parent process/IPC
44+
// channel. Once this is done, we can unref our client and server sockets, and
45+
// the only thing keeping this worker alive will be IPC. This is important,
46+
// because it means a worker with no parent will have no referenced handles,
47+
// thus no work to do, and will exit immediately, preventing process leaks.
48+
process.on('message', function() {});
49+
4250
const server = net.createServer((c) => {
4351
process.once('message', function(msg) {
4452
assert.strictEqual(msg, 'got');
4553
c.end('hello');
4654
});
4755
socketConnected();
48-
});
56+
}).unref();
4957
server.listen(0, common.localhostIPv4, () => {
5058
const port = server.address().port;
51-
socket = net.connect(port, common.localhostIPv4, socketConnected);
59+
socket = net.connect(port, common.localhostIPv4, socketConnected).unref();
5260
});
5361
}

0 commit comments

Comments
 (0)