Skip to content

Commit 19c4ec3

Browse files
committed
cluster: Add support to 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 9a70b27 commit 19c4ec3

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

lib/internal/cluster/master.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,14 @@ function createWorkerProcess(id, env) {
102102
const workerEnv = util._extend({}, process.env);
103103
const execArgv = cluster.settings.execArgv.slice();
104104
const debugArgRegex = /--inspect(?:-brk|-port)?|--debug-port/;
105+
const nodeOptions = process.env.NODE_OPTIONS ?
106+
process.env.NODE_OPTIONS.split(' ') : [];
105107

106108
util._extend(workerEnv, env);
107109
workerEnv.NODE_UNIQUE_ID = '' + id;
108110

109-
if (execArgv.some((arg) => arg.match(debugArgRegex))) {
111+
if (execArgv.some((arg) => arg.match(debugArgRegex)) ||
112+
nodeOptions.some((arg) => arg.match(debugArgRegex))) {
110113
let inspectPort;
111114
if ('inspectPort' in cluster.settings) {
112115
if (typeof cluster.settings.inspectPort === 'function')
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
const common = require('../common');
3+
const cluster = require('cluster');
4+
const assert = require('assert');
5+
6+
if (process.config.variables.node_without_node_options)
7+
common.skip('missing NODE_OPTIONS support');
8+
9+
10+
checkForInspectSupport('--inspect');
11+
12+
function checkForInspectSupport(flag) {
13+
const env = Object.assign({}, process.env, { NODE_OPTIONS: flag });
14+
const o = JSON.stringify(flag);
15+
16+
if (cluster.isMaster) {
17+
cluster.fork(env);
18+
19+
cluster.on('online', (worker) => {
20+
process.exit(0);
21+
});
22+
23+
cluster.on('exit', (worker, code, signal) => {
24+
assert.fail(`For ${o}, failed to start a cluster with inspect option`);
25+
});
26+
}
27+
}

0 commit comments

Comments
 (0)