Skip to content

Commit

Permalink
cluster: Added test which spawns multiple workers for cluster to wor…
Browse files Browse the repository at this point in the history
…k 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
  • Loading branch information
sameer-coder committed Mar 16, 2018
1 parent 19c4ec3 commit ac832ab
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
4 changes: 2 additions & 2 deletions lib/internal/cluster/master.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,13 @@ function createWorkerProcess(id, env) {
const execArgv = cluster.settings.execArgv.slice();
const debugArgRegex = /--inspect(?:-brk|-port)?|--debug-port/;
const nodeOptions = process.env.NODE_OPTIONS ?
process.env.NODE_OPTIONS.split(' ') : [];
process.env.NODE_OPTIONS : '';

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

if (execArgv.some((arg) => arg.match(debugArgRegex)) ||
nodeOptions.some((arg) => arg.match(debugArgRegex))) {
nodeOptions.match(debugArgRegex)) {
let inspectPort;
if ('inspectPort' in cluster.settings) {
if (typeof cluster.settings.inspectPort === 'function')
Expand Down
21 changes: 13 additions & 8 deletions test/parallel/test-inspect-support-for-node_options.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,31 @@
const common = require('../common');
const cluster = require('cluster');
const assert = require('assert');
const numCPUs = require('os').cpus().length;

if (process.config.variables.node_without_node_options)
common.skip('missing NODE_OPTIONS support');

common.skipIfInspectorDisabled();

checkForInspectSupport('--inspect');

function checkForInspectSupport(flag) {
const env = Object.assign({}, process.env, { NODE_OPTIONS: flag });
const o = JSON.stringify(flag);

const nodeOptions = JSON.stringify(flag);
process.env.NODE_OPTIONS = flag;

if (cluster.isMaster) {
cluster.fork(env);
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}

cluster.on('online', (worker) => {
process.exit(0);
worker.disconnect();
});

cluster.on('exit', (worker, code, signal) => {
assert.fail(`For ${o}, failed to start a cluster with inspect option`);
if (worker.exitedAfterDisconnect === false) {
assert.fail(`For ${nodeOptions}, failed to start cluster\
with inspect option`);
}
});
}
}

0 comments on commit ac832ab

Please sign in to comment.