diff --git a/lib/internal/cluster/master.js b/lib/internal/cluster/master.js index 3c6a398f117433..af787a4b9607e6 100644 --- a/lib/internal/cluster/master.js +++ b/lib/internal/cluster/master.js @@ -102,11 +102,14 @@ function createWorkerProcess(id, env) { const workerEnv = util._extend({}, process.env); const execArgv = cluster.settings.execArgv.slice(); const debugArgRegex = /--inspect(?:-brk|-port)?|--debug-port/; + const nodeOptions = process.env.NODE_OPTIONS ? + process.env.NODE_OPTIONS : ''; util._extend(workerEnv, env); workerEnv.NODE_UNIQUE_ID = '' + id; - if (execArgv.some((arg) => arg.match(debugArgRegex))) { + if (execArgv.some((arg) => arg.match(debugArgRegex)) || + nodeOptions.match(debugArgRegex)) { let inspectPort; if ('inspectPort' in cluster.settings) { if (typeof cluster.settings.inspectPort === 'function') diff --git a/test/parallel/test-inspect-support-for-node_options.js b/test/parallel/test-inspect-support-for-node_options.js new file mode 100644 index 00000000000000..05c7ec24b90d30 --- /dev/null +++ b/test/parallel/test-inspect-support-for-node_options.js @@ -0,0 +1,30 @@ +'use strict'; +const common = require('../common'); +const cluster = require('cluster'); +const assert = require('assert'); + +common.skipIfInspectorDisabled(); + +checkForInspectSupport('--inspect'); + +function checkForInspectSupport(flag) { + + const nodeOptions = JSON.stringify(flag); + const numWorkers = 2; + process.env.NODE_OPTIONS = flag; + + if (cluster.isMaster) { + for (let i = 0; i < numWorkers; i++) { + cluster.fork(); + } + + cluster.on('online', (worker) => { + worker.disconnect(); + }); + + cluster.on('exit', common.mustCall((worker, code, signal) => { + const errMsg = `For NODE_OPTIONS ${nodeOptions}, failed to start cluster`; + assert.strictEqual(worker.exitedAfterDisconnect, true, errMsg); + }, numWorkers)); + } +}