diff --git a/lib/cluster.js b/lib/cluster.js index 10a55996f15cc8..7462d00c695f98 100644 --- a/lib/cluster.js +++ b/lib/cluster.js @@ -17,6 +17,8 @@ cluster.Worker = Worker; cluster.isWorker = ('NODE_UNIQUE_ID' in process.env); cluster.isMaster = (cluster.isWorker === false); +const kDebugPortMin = 1024; +const kDebugPortMax = 65535; function Worker(options) { if (!(this instanceof Worker)) @@ -279,10 +281,20 @@ function masterInit() { cluster.emit('setup', settings); } + var nextDebugPort = process.debugPort; + + function getDebugPort() { + ++nextDebugPort; + if (nextDebugPort > kDebugPortMax) { + nextDebugPort = kDebugPortMin; + } + return nextDebugPort; + } + function createWorkerProcess(id, env) { var workerEnv = util._extend({}, process.env); var execArgv = cluster.settings.execArgv.slice(); - var debugPort = process.debugPort + id; + var debugPort = getDebugPort(); var hasDebugArg = false; workerEnv = util._extend(workerEnv, env); diff --git a/test/parallel/test-cluster-debugport-overflow.js b/test/parallel/test-cluster-debugport-overflow.js new file mode 100644 index 00000000000000..4c149440c6f0d2 --- /dev/null +++ b/test/parallel/test-cluster-debugport-overflow.js @@ -0,0 +1,19 @@ +var spawn = require('child_process').spawn; +var cluster = require('cluster'); +var assert = require('assert'); + +if (process.argv[2] == 'master') { + if (cluster.isMaster) { + cluster.fork().on('exit', function(code) { + process.exit(code); + }); + } else { + process.exit(42); + } +} else { + // iojs --debug-port=65535 test-cluster-debugport-overflow.js master + var args = ['--debug-port=65535', __filename, 'master']; + spawn(process.argv[0], args).on('close', function(code) { + assert.equal(42, code, 'Worker was started'); + }); +}