From 6ece2a03226c18f2e9e391a8da93c3bc6fc8ab07 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Sun, 29 May 2016 14:02:22 -0400 Subject: [PATCH] cluster: rewrite debug ports consistently When debug flags are passed to clustered applications, the debug port is rewritten for each worker process to avoid collisions. Prior to this commit, each debug flag would get a unique value. This commit reworks the logic to assign the same port value to all debug flags for a single worker. PR-URL: https://github.com/nodejs/node/pull/7050 Reviewed-By: Ben Noordhuis Reviewed-By: Santiago Gimeno --- lib/cluster.js | 8 ++++++-- test/parallel/test-cluster-debug-port.js | 5 +++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/cluster.js b/lib/cluster.js index 72a3b79a93eec1..da57f5cd08b30a 100644 --- a/lib/cluster.js +++ b/lib/cluster.js @@ -286,6 +286,7 @@ function masterInit() { function createWorkerProcess(id, env) { var workerEnv = util._extend({}, process.env); var execArgv = cluster.settings.execArgv.slice(); + var debugPort = 0; workerEnv = util._extend(workerEnv, env); workerEnv.NODE_UNIQUE_ID = '' + id; @@ -294,8 +295,11 @@ function masterInit() { var match = execArgv[i].match(/^(--debug|--debug-(brk|port))(=\d+)?$/); if (match) { - const debugPort = process.debugPort + debugPortOffset; - ++debugPortOffset; + if (debugPort === 0) { + debugPort = process.debugPort + debugPortOffset; + ++debugPortOffset; + } + execArgv[i] = match[1] + '=' + debugPort; } } diff --git a/test/parallel/test-cluster-debug-port.js b/test/parallel/test-cluster-debug-port.js index 076a59108b09de..efc3fae62e58a4 100644 --- a/test/parallel/test-cluster-debug-port.js +++ b/test/parallel/test-cluster-debug-port.js @@ -23,6 +23,11 @@ if (cluster.isMaster) { portSet: process.debugPort + 1 }).on('exit', checkExitCode); + cluster.setupMaster({ + execArgv: [`--debug-port=${process.debugPort}`, + `--debug=${process.debugPort}`] + }); + console.log('forked worker should have --debug-port, with offset = 2'); cluster.fork({ portSet: process.debugPort + 2