Skip to content

Commit e22c756

Browse files
committed
test: improve cluster-disconnect-handles test
This commit fixes two issues in test-cluster-disconnect-handles: 1. If the master's TCP connection to the worker fails, the worker process stays alive and causes many other tests that use the same common port number to also fail (with EADDRINUSE). 2. One particular problem that can cause the master's TCP connection to fail is attempting an IPv6 connection to the worker when no IPv6 network interfaces are available.
1 parent 82b8355 commit e22c756

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

test/parallel/test-cluster-disconnect-handles.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ cluster.schedulingPolicy = cluster.SCHED_RR;
2323
// is to make sure the connection is still sitting in the master's
2424
// pending handle queue.
2525
if (cluster.isMaster) {
26+
let isKilling = false;
2627
const handles = require('internal/cluster').handles;
2728
// FIXME(bnoordhuis) lib/cluster.js scans the execArgv arguments for
2829
// debugger flags and renumbers any port numbers it sees starting
@@ -55,11 +56,30 @@ if (cluster.isMaster) {
5556
}));
5657
}));
5758
process.on('exit', () => assert.deepStrictEqual(handles, {}));
59+
process.on('uncaughtException', function(ex) {
60+
// Make sure we clean up so as not to leave a stray worker process running
61+
// if we encounter a connection or other error
62+
if (!worker.isDead()) {
63+
if (!isKilling) {
64+
isKilling = true;
65+
worker.once('exit', function() {
66+
throw ex;
67+
});
68+
worker.process.kill();
69+
}
70+
return;
71+
}
72+
throw ex;
73+
});
5874
} else {
5975
const server = net.createServer(socket => socket.pipe(socket));
60-
server.listen(() => {
76+
const cb = () => {
6177
process.send(['listening', server.address()]);
6278
debugger;
63-
});
79+
};
80+
if (common.hasIPv6)
81+
server.listen(cb);
82+
else
83+
server.listen(0, common.localhostIPv4, cb);
6484
process.on('disconnect', process.exit);
6585
}

0 commit comments

Comments
 (0)