Skip to content

Commit 926214a

Browse files
sameer-codertargos
authored andcommitted
cluster: add support for 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 #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: #19026 PR-URL: #19165 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Richard Lau <riclau@uk.ibm.com>
1 parent 89e7a5f commit 926214a

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

lib/internal/cluster/master.js

+4-1
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 : '';
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.match(debugArgRegex)) {
110113
let inspectPort;
111114
if ('inspectPort' in cluster.settings) {
112115
if (typeof cluster.settings.inspectPort === 'function')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
const common = require('../common');
3+
const cluster = require('cluster');
4+
const assert = require('assert');
5+
6+
common.skipIfInspectorDisabled();
7+
8+
checkForInspectSupport('--inspect');
9+
10+
function checkForInspectSupport(flag) {
11+
12+
const nodeOptions = JSON.stringify(flag);
13+
const numWorkers = 2;
14+
process.env.NODE_OPTIONS = flag;
15+
16+
if (cluster.isMaster) {
17+
for (let i = 0; i < numWorkers; i++) {
18+
cluster.fork();
19+
}
20+
21+
cluster.on('online', (worker) => {
22+
worker.disconnect();
23+
});
24+
25+
cluster.on('exit', common.mustCall((worker, code, signal) => {
26+
const errMsg = `For NODE_OPTIONS ${nodeOptions}, failed to start cluster`;
27+
assert.strictEqual(worker.exitedAfterDisconnect, true, errMsg);
28+
}, numWorkers));
29+
}
30+
}

0 commit comments

Comments
 (0)