diff --git a/lib/cluster.js b/lib/cluster.js index bb4b60a2fc489e..f84fb695a50aeb 100644 --- a/lib/cluster.js +++ b/lib/cluster.js @@ -278,11 +278,11 @@ function masterInit() { cluster.emit('setup', settings); } + var debugPortOffset = 1; + function createWorkerProcess(id, env) { var workerEnv = util._extend({}, process.env); var execArgv = cluster.settings.execArgv.slice(); - var debugPort = process.debugPort + id; - var hasDebugArg = false; workerEnv = util._extend(workerEnv, env); workerEnv.NODE_UNIQUE_ID = '' + id; @@ -291,14 +291,12 @@ function masterInit() { var match = execArgv[i].match(/^(--debug|--debug-(brk|port))(=\d+)?$/); if (match) { + let debugPort = process.debugPort + debugPortOffset; + ++debugPortOffset; execArgv[i] = match[1] + '=' + debugPort; - hasDebugArg = true; } } - if (!hasDebugArg) - execArgv = ['--debug-port=' + debugPort].concat(execArgv); - return fork(cluster.settings.exec, cluster.settings.args, { env: workerEnv, silent: cluster.settings.silent, diff --git a/test/parallel/test-cluster-debug-port.js b/test/parallel/test-cluster-debug-port.js new file mode 100644 index 00000000000000..1a42850e9b2bc1 --- /dev/null +++ b/test/parallel/test-cluster-debug-port.js @@ -0,0 +1,38 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const cluster = require('cluster'); + +if (cluster.isMaster) { + assert.strictEqual(process.execArgv.length, 0, 'run test with no args'); + + function checkExitCode(code, signal) { + assert.strictEqual(code, 0); + assert.strictEqual(signal, null); + } + + console.log('forked worker should not have --debug-port'); + cluster.fork().on('exit', checkExitCode); + + cluster.setupMaster({ + execArgv: ['--debug-port=' + process.debugPort] + }); + + console.log('forked worker should have --debug-port, with offset = 1'); + cluster.fork({ + portSet: process.debugPort + 1 + }).on('exit', checkExitCode); + + console.log('forked worker should have --debug-port, with offset = 2'); + cluster.fork({ + portSet: process.debugPort + 2 + }).on('exit', checkExitCode); +} else { + const hasDebugArg = process.execArgv.some(function(arg) { + return /debug/.test(arg); + }); + + assert.strictEqual(hasDebugArg, process.env.portSet !== undefined); + assert.strictEqual(process.debugPort, +process.env.portSet || 5858); + process.exit(); +}