Skip to content

Commit

Permalink
cluster: fix inspector port assignment
Browse files Browse the repository at this point in the history
Make sure that inspector ports in cluster are inside the valid range:
`[1024, 65535]`.
Fixes flaky `test-inspector-port-zero-cluster`.

PR-URL: nodejs#18696
Fixes: nodejs#18303
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
  • Loading branch information
santigimeno authored and addaleax committed Feb 26, 2018
1 parent 15c9a71 commit dfe0bc1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
3 changes: 3 additions & 0 deletions lib/internal/cluster/master.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const intercom = new EventEmitter();
const SCHED_NONE = 1;
const SCHED_RR = 2;
const { isLegalPort } = require('internal/net');
const [ minPort, maxPort ] = [ 1024, 65535 ];

module.exports = cluster;

Expand Down Expand Up @@ -119,6 +120,8 @@ function createWorkerProcess(id, env) {
}
} else {
inspectPort = process.debugPort + debugPortOffset;
if (inspectPort > maxPort)
inspectPort = inspectPort - maxPort + minPort - 1;
debugPortOffset++;
}

Expand Down
10 changes: 10 additions & 0 deletions test/sequential/test-inspector-port-cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ function testRunnerMain() {
workers: [{ expectedPort: 9230 }]
});

spawnMaster({
execArgv: ['--inspect=65534'],
workers: [
{ expectedPort: 65535 },
{ expectedPort: 1024 },
{ expectedPort: 1025 },
{ expectedPort: 1026 }
]
});

let port = debuggerPort + offset++ * 5;

spawnMaster({
Expand Down
10 changes: 4 additions & 6 deletions test/sequential/test-inspector-port-zero-cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,14 @@ function serialFork() {
if (cluster.isMaster) {
Promise.all([serialFork(), serialFork(), serialFork()])
.then(common.mustCall((ports) => {
ports.push(process.debugPort);
ports.sort();
ports.splice(0, 0, process.debugPort);
// 4 = [master, worker1, worker2, worker3].length()
assert.strictEqual(ports.length, 4);
assert(ports.every((port) => port > 0));
assert(ports.every((port) => port < 65536));
// Ports should be consecutive.
assert.strictEqual(ports[0] + 1, ports[1]);
assert.strictEqual(ports[1] + 1, ports[2]);
assert.strictEqual(ports[2] + 1, ports[3]);
assert.strictEqual(ports[0] === 65535 ? 1024 : ports[0] + 1, ports[1]);
assert.strictEqual(ports[1] === 65535 ? 1024 : ports[1] + 1, ports[2]);
assert.strictEqual(ports[2] === 65535 ? 1024 : ports[2] + 1, ports[3]);
}))
.catch(
(err) => {
Expand Down

0 comments on commit dfe0bc1

Please sign in to comment.