Skip to content

Commit d5b466e

Browse files
cjihrigrvagg
authored andcommitted
cluster: do not unconditionally set --debug-port
Currently, each cluster worker is assigned an ever increasing --debug-port argument. A long running cluster application that does not use the debugger can run into errors related to the port range. This commit mitigates the problem by only setting the debug port if the master is started with debug arguments, or the user explicitly defines debug arguments for the worker. This commit also adds a new debug port offset counter that is only incremented when a worker is created that utilizes debugging. Fixes: nodejs/node-v0.x-archive#8159 Refs: #1524 PR-URL: #1949 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Oleg Elifantiev <oleg@elifantiev.ru>
1 parent 8ec089a commit d5b466e

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

lib/cluster.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -278,11 +278,11 @@ function masterInit() {
278278
cluster.emit('setup', settings);
279279
}
280280

281+
var debugPortOffset = 1;
282+
281283
function createWorkerProcess(id, env) {
282284
var workerEnv = util._extend({}, process.env);
283285
var execArgv = cluster.settings.execArgv.slice();
284-
var debugPort = process.debugPort + id;
285-
var hasDebugArg = false;
286286

287287
workerEnv = util._extend(workerEnv, env);
288288
workerEnv.NODE_UNIQUE_ID = '' + id;
@@ -291,14 +291,12 @@ function masterInit() {
291291
var match = execArgv[i].match(/^(--debug|--debug-(brk|port))(=\d+)?$/);
292292

293293
if (match) {
294+
let debugPort = process.debugPort + debugPortOffset;
295+
++debugPortOffset;
294296
execArgv[i] = match[1] + '=' + debugPort;
295-
hasDebugArg = true;
296297
}
297298
}
298299

299-
if (!hasDebugArg)
300-
execArgv = ['--debug-port=' + debugPort].concat(execArgv);
301-
302300
return fork(cluster.settings.exec, cluster.settings.args, {
303301
env: workerEnv,
304302
silent: cluster.settings.silent,
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const cluster = require('cluster');
5+
6+
if (cluster.isMaster) {
7+
assert.strictEqual(process.execArgv.length, 0, 'run test with no args');
8+
9+
function checkExitCode(code, signal) {
10+
assert.strictEqual(code, 0);
11+
assert.strictEqual(signal, null);
12+
}
13+
14+
console.log('forked worker should not have --debug-port');
15+
cluster.fork().on('exit', checkExitCode);
16+
17+
cluster.setupMaster({
18+
execArgv: ['--debug-port=' + process.debugPort]
19+
});
20+
21+
console.log('forked worker should have --debug-port, with offset = 1');
22+
cluster.fork({
23+
portSet: process.debugPort + 1
24+
}).on('exit', checkExitCode);
25+
26+
console.log('forked worker should have --debug-port, with offset = 2');
27+
cluster.fork({
28+
portSet: process.debugPort + 2
29+
}).on('exit', checkExitCode);
30+
} else {
31+
const hasDebugArg = process.execArgv.some(function(arg) {
32+
return /debug/.test(arg);
33+
});
34+
35+
assert.strictEqual(hasDebugArg, process.env.portSet !== undefined);
36+
assert.strictEqual(process.debugPort, +process.env.portSet || 5858);
37+
process.exit();
38+
}

0 commit comments

Comments
 (0)