From 4fa1f3197f75263e982d60b8632bac8e2a99b8f3 Mon Sep 17 00:00:00 2001 From: Santiago Gimeno Date: Sat, 10 Feb 2018 14:52:18 +0100 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/18696 Fixes: https://github.com/nodejs/node/issues/18303 Reviewed-By: Colin Ihrig Reviewed-By: Ruben Bridgewater --- lib/internal/cluster/master.js | 3 +++ test/sequential/test-inspector-port-cluster.js | 10 ++++++++++ test/sequential/test-inspector-port-zero-cluster.js | 10 ++++------ 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/lib/internal/cluster/master.js b/lib/internal/cluster/master.js index 570cf7bc6f93d4..3c6a398f117433 100644 --- a/lib/internal/cluster/master.js +++ b/lib/internal/cluster/master.js @@ -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; @@ -119,6 +120,8 @@ function createWorkerProcess(id, env) { } } else { inspectPort = process.debugPort + debugPortOffset; + if (inspectPort > maxPort) + inspectPort = inspectPort - maxPort + minPort - 1; debugPortOffset++; } diff --git a/test/sequential/test-inspector-port-cluster.js b/test/sequential/test-inspector-port-cluster.js index 84ec408ebfb178..87469aa7ff77c5 100644 --- a/test/sequential/test-inspector-port-cluster.js +++ b/test/sequential/test-inspector-port-cluster.js @@ -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({ diff --git a/test/sequential/test-inspector-port-zero-cluster.js b/test/sequential/test-inspector-port-zero-cluster.js index f64e05f314c0c6..e522056571d1c2 100644 --- a/test/sequential/test-inspector-port-zero-cluster.js +++ b/test/sequential/test-inspector-port-zero-cluster.js @@ -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) => {