From 96d131665ed4ab6604ccfc92eb75350f1ab4ab62 Mon Sep 17 00:00:00 2001 From: Moshe Atlow Date: Tue, 13 Sep 2022 09:41:19 +0300 Subject: [PATCH] cluster: use inspector utils PR-URL: https://github.com/nodejs/node/pull/44592 Backport-PR-URL: https://github.com/nodejs/node/pull/44873 Reviewed-By: Matteo Collina Reviewed-By: Benjamin Gruenbaum --- lib/internal/cluster/primary.js | 38 ++++++++++----------------------- lib/internal/util/inspector.js | 18 ++++++++-------- 2 files changed, 20 insertions(+), 36 deletions(-) diff --git a/lib/internal/cluster/primary.js b/lib/internal/cluster/primary.js index 749f537734947d..fc366680fb7bf7 100644 --- a/lib/internal/cluster/primary.js +++ b/lib/internal/cluster/primary.js @@ -6,10 +6,14 @@ const { ArrayPrototypeSome, ObjectKeys, ObjectValues, - RegExpPrototypeExec, SafeMap, StringPrototypeStartsWith, } = primordials; +const { + codes: { + ERR_SOCKET_BAD_PORT, + } +} = require('internal/errors'); const assert = require('internal/assert'); const { fork } = require('child_process'); @@ -18,14 +22,12 @@ const EventEmitter = require('events'); const RoundRobinHandle = require('internal/cluster/round_robin_handle'); const SharedHandle = require('internal/cluster/shared_handle'); const Worker = require('internal/cluster/worker'); +const { getInspectPort, isUsingInspector } = require('internal/util/inspector'); const { internal, sendHelper } = require('internal/cluster/utils'); const cluster = new EventEmitter(); const intercom = new EventEmitter(); const SCHED_NONE = 1; const SCHED_RR = 2; -const minPort = 1024; -const maxPort = 65535; -const { validatePort } = require('internal/validators'); module.exports = cluster; @@ -40,7 +42,6 @@ cluster.SCHED_NONE = SCHED_NONE; // Leave it to the operating system. cluster.SCHED_RR = SCHED_RR; // Primary distributes connections. let ids = 0; -let debugPortOffset = 1; let initialized = false; // XXX(bnoordhuis) Fold cluster.schedulingPolicy into cluster.settings? @@ -117,29 +118,12 @@ function setupSettingsNT(settings) { function createWorkerProcess(id, env) { const workerEnv = { ...process.env, ...env, NODE_UNIQUE_ID: `${id}` }; const execArgv = [...cluster.settings.execArgv]; - const debugArgRegex = /--inspect(?:-brk|-port)?|--debug-port/; - const nodeOptions = process.env.NODE_OPTIONS || ''; - - // TODO(MoLow): Use getInspectPort from internal/util/inspector - if (ArrayPrototypeSome(execArgv, - (arg) => RegExpPrototypeExec(debugArgRegex, arg) !== null) || - RegExpPrototypeExec(debugArgRegex, nodeOptions) !== null) { - let inspectPort; - if ('inspectPort' in cluster.settings) { - if (typeof cluster.settings.inspectPort === 'function') - inspectPort = cluster.settings.inspectPort(); - else - inspectPort = cluster.settings.inspectPort; - - validatePort(inspectPort); - } else { - inspectPort = process.debugPort + debugPortOffset; - if (inspectPort > maxPort) - inspectPort = inspectPort - maxPort + minPort - 1; - debugPortOffset++; - } - ArrayPrototypePush(execArgv, `--inspect-port=${inspectPort}`); + if (cluster.settings.inspectPort === null) { + throw new ERR_SOCKET_BAD_PORT('Port', null, true); + } + if (isUsingInspector(cluster.settings.execArgv)) { + ArrayPrototypePush(execArgv, `--inspect-port=${getInspectPort(cluster.settings.inspectPort)}`); } return fork(cluster.settings.exec, cluster.settings.args, { diff --git a/lib/internal/util/inspector.js b/lib/internal/util/inspector.js index f1bb21d859d437..0514aece96d95e 100644 --- a/lib/internal/util/inspector.js +++ b/lib/internal/util/inspector.js @@ -8,6 +8,7 @@ const { ObjectKeys, ObjectPrototypeHasOwnProperty, RegExpPrototypeExec, + SafeWeakMap, } = primordials; const { validatePort } = require('internal/validators'); @@ -17,19 +18,18 @@ const kMaxPort = 65535; const kInspectArgRegex = /--inspect(?:-brk|-port)?|--debug-port/; const kInspectMsgRegex = /Debugger listening on ws:\/\/\[?(.+?)\]?:(\d+)\/|Debugger attached|Waiting for the debugger to disconnect\.\.\./; -let _isUsingInspector; -function isUsingInspector() { - _isUsingInspector ??= - ArrayPrototypeSome(process.execArgv, (arg) => RegExpPrototypeExec(kInspectArgRegex, arg) !== null) || - RegExpPrototypeExec(kInspectArgRegex, process.env.NODE_OPTIONS) !== null; - return _isUsingInspector; +const _isUsingInspector = new SafeWeakMap(); +function isUsingInspector(execArgv = process.execArgv) { + if (!_isUsingInspector.has(execArgv)) { + _isUsingInspector.set(execArgv, + ArrayPrototypeSome(execArgv, (arg) => RegExpPrototypeExec(kInspectArgRegex, arg) !== null) || + RegExpPrototypeExec(kInspectArgRegex, process.env.NODE_OPTIONS) !== null); + } + return _isUsingInspector.get(execArgv); } let debugPortOffset = 1; function getInspectPort(inspectPort) { - if (!isUsingInspector()) { - return null; - } if (typeof inspectPort === 'function') { inspectPort = inspectPort(); } else if (inspectPort == null) {