Skip to content

Commit bf4d074

Browse files
santigimenoMylesBorins
authored andcommitted
cluster: fix inspector port assignment
Make sure that inspector ports in cluster are inside the valid range: `[1024, 65535]`. Fixes flaky `test-inspector-port-zero-cluster`. PR-URL: #18696 Fixes: #18303 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent ab00559 commit bf4d074

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

lib/internal/cluster/master.js

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const intercom = new EventEmitter();
1414
const SCHED_NONE = 1;
1515
const SCHED_RR = 2;
1616
const { isLegalPort } = require('internal/net');
17+
const [ minPort, maxPort ] = [ 1024, 65535 ];
1718

1819
module.exports = cluster;
1920

@@ -119,6 +120,8 @@ function createWorkerProcess(id, env) {
119120
}
120121
} else {
121122
inspectPort = process.debugPort + debugPortOffset;
123+
if (inspectPort > maxPort)
124+
inspectPort = inspectPort - maxPort + minPort - 1;
122125
debugPortOffset++;
123126
}
124127

test/sequential/test-inspector-port-cluster.js

+10
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ function testRunnerMain() {
2424
workers: [{ expectedPort: 9230 }]
2525
});
2626

27+
spawnMaster({
28+
execArgv: ['--inspect=65534'],
29+
workers: [
30+
{ expectedPort: 65535 },
31+
{ expectedPort: 1024 },
32+
{ expectedPort: 1025 },
33+
{ expectedPort: 1026 }
34+
]
35+
});
36+
2737
let port = debuggerPort + offset++ * 5;
2838

2939
spawnMaster({

test/sequential/test-inspector-port-zero-cluster.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,14 @@ function serialFork() {
3030
if (cluster.isMaster) {
3131
Promise.all([serialFork(), serialFork(), serialFork()])
3232
.then(common.mustCall((ports) => {
33-
ports.push(process.debugPort);
34-
ports.sort();
33+
ports.splice(0, 0, process.debugPort);
3534
// 4 = [master, worker1, worker2, worker3].length()
3635
assert.strictEqual(ports.length, 4);
3736
assert(ports.every((port) => port > 0));
3837
assert(ports.every((port) => port < 65536));
39-
// Ports should be consecutive.
40-
assert.strictEqual(ports[0] + 1, ports[1]);
41-
assert.strictEqual(ports[1] + 1, ports[2]);
42-
assert.strictEqual(ports[2] + 1, ports[3]);
38+
assert.strictEqual(ports[0] === 65535 ? 1024 : ports[0] + 1, ports[1]);
39+
assert.strictEqual(ports[1] === 65535 ? 1024 : ports[1] + 1, ports[2]);
40+
assert.strictEqual(ports[2] === 65535 ? 1024 : ports[2] + 1, ports[3]);
4341
}))
4442
.catch(
4543
(err) => {

0 commit comments

Comments
 (0)