Skip to content

Commit ac832ab

Browse files
committed
cluster: Added test which spawns multiple workers for cluster to work with NODE_OPTIONS="--inspect"
When using cluster and --inspect as cli argument it is correctly handled and each worker will use a different port, this was fixed by nodejs#13619. But when env var NODE_OPTIONS="--inspect" is set this logic doesn't apply and the workers will fail as they try to attach to the same port. Fixes: nodejs#19026
1 parent 19c4ec3 commit ac832ab

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

lib/internal/cluster/master.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,13 @@ function createWorkerProcess(id, env) {
103103
const execArgv = cluster.settings.execArgv.slice();
104104
const debugArgRegex = /--inspect(?:-brk|-port)?|--debug-port/;
105105
const nodeOptions = process.env.NODE_OPTIONS ?
106-
process.env.NODE_OPTIONS.split(' ') : [];
106+
process.env.NODE_OPTIONS : '';
107107

108108
util._extend(workerEnv, env);
109109
workerEnv.NODE_UNIQUE_ID = '' + id;
110110

111111
if (execArgv.some((arg) => arg.match(debugArgRegex)) ||
112-
nodeOptions.some((arg) => arg.match(debugArgRegex))) {
112+
nodeOptions.match(debugArgRegex)) {
113113
let inspectPort;
114114
if ('inspectPort' in cluster.settings) {
115115
if (typeof cluster.settings.inspectPort === 'function')

test/parallel/test-inspect-support-for-node_options.js

+13-8
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,31 @@
22
const common = require('../common');
33
const cluster = require('cluster');
44
const assert = require('assert');
5+
const numCPUs = require('os').cpus().length;
56

6-
if (process.config.variables.node_without_node_options)
7-
common.skip('missing NODE_OPTIONS support');
8-
7+
common.skipIfInspectorDisabled();
98

109
checkForInspectSupport('--inspect');
1110

1211
function checkForInspectSupport(flag) {
13-
const env = Object.assign({}, process.env, { NODE_OPTIONS: flag });
14-
const o = JSON.stringify(flag);
12+
13+
const nodeOptions = JSON.stringify(flag);
14+
process.env.NODE_OPTIONS = flag;
1515

1616
if (cluster.isMaster) {
17-
cluster.fork(env);
17+
for (let i = 0; i < numCPUs; i++) {
18+
cluster.fork();
19+
}
1820

1921
cluster.on('online', (worker) => {
20-
process.exit(0);
22+
worker.disconnect();
2123
});
2224

2325
cluster.on('exit', (worker, code, signal) => {
24-
assert.fail(`For ${o}, failed to start a cluster with inspect option`);
26+
if (worker.exitedAfterDisconnect === false) {
27+
assert.fail(`For ${nodeOptions}, failed to start cluster\
28+
with inspect option`);
29+
}
2530
});
2631
}
2732
}

0 commit comments

Comments
 (0)