From 9b34ea6161a637d6be7a68b816df8f8479d7c4ee Mon Sep 17 00:00:00 2001 From: Sameer Srivastava Date: Tue, 6 Mar 2018 14:27:49 +0530 Subject: [PATCH] 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: https://github.com/nodejs/node/issues/19026 PR-URL: https://github.com/nodejs/node/pull/19165 Reviewed-By: Colin Ihrig Reviewed-By: James M Snell Reviewed-By: Richard Lau --- lib/internal/cluster/master.js | 5 +++- .../test-inspect-support-for-node_options.js | 30 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-inspect-support-for-node_options.js diff --git a/lib/internal/cluster/master.js b/lib/internal/cluster/master.js index 7ee43689c4c..8e4f9395da6 100644 --- a/lib/internal/cluster/master.js +++ b/lib/internal/cluster/master.js @@ -103,11 +103,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 00000000000..05c7ec24b90 --- /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)); + } +}