From 196a470f0f2bb6afeee434cb772a7470d37f0063 Mon Sep 17 00:00:00 2001 From: RafaelGSS Date: Thu, 2 Jan 2025 17:26:31 -0300 Subject: [PATCH 1/2] src: add --disable-sigusr1 to prevent signal i/o thread This commit adds a new flag `--disable-sigusr1` to prevent the SignalIOThread to be up listening the SIGUSR1 events and then starting the debugging session. --- doc/api/cli.md | 14 +++++++++++++ src/env-inl.h | 3 ++- src/node_options.cc | 5 +++++ src/node_options.h | 1 + test/fixtures/disable-signal/sigusr1.js | 2 ++ test/parallel/test-disable-sigusr1.js | 26 +++++++++++++++++++++++++ 6 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/disable-signal/sigusr1.js create mode 100644 test/parallel/test-disable-sigusr1.js diff --git a/doc/api/cli.md b/doc/api/cli.md index 87eda0deb66b46..0b68083a0f8319 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -574,6 +574,17 @@ Disable the `Object.prototype.__proto__` property. If `mode` is `delete`, the property is removed entirely. If `mode` is `throw`, accesses to the property throw an exception with the code `ERR_PROTO_ACCESS`. +### `--disable-sigusr1` + + + +> Stability: 1.2 - Release candidate + +Disable the ability of starting a debugging session by sending a +`SIGUSR1` signal to the process. + ### `--disable-warning=code-or-type` > Stability: 1.1 - Active development @@ -1447,6 +1458,7 @@ added: v7.6.0 Set the `host:port` to be used when the inspector is activated. Useful when activating the inspector by sending the `SIGUSR1` signal. +Except when [`--disable-sigusr1`][] is passed. Default host is `127.0.0.1`. If port `0` is specified, a random available port will be used. @@ -3073,6 +3085,7 @@ one is included in the list below. * `--conditions`, `-C` * `--diagnostic-dir` * `--disable-proto` +* `--disable-sigusr1` * `--disable-warning` * `--disable-wasm-trap-handler` * `--dns-result-order` @@ -3651,6 +3664,7 @@ node --stack-trace-limit=12 -p -e "Error.stackTraceLimit" # prints 12 [`--build-snapshot`]: #--build-snapshot [`--cpu-prof-dir`]: #--cpu-prof-dir [`--diagnostic-dir`]: #--diagnostic-dirdirectory +[`--disable-sigusr1`]: #--disable-sigusr1 [`--env-file-if-exists`]: #--env-file-if-existsconfig [`--env-file`]: #--env-fileconfig [`--experimental-addon-modules`]: #--experimental-addon-modules diff --git a/src/env-inl.h b/src/env-inl.h index 79496fab1a7528..1ea46d79787866 100644 --- a/src/env-inl.h +++ b/src/env-inl.h @@ -666,7 +666,8 @@ inline bool Environment::no_global_search_paths() const { } inline bool Environment::should_start_debug_signal_handler() const { - return (flags_ & EnvironmentFlags::kNoStartDebugSignalHandler) == 0; + return ((flags_ & EnvironmentFlags::kNoStartDebugSignalHandler) == 0) && + !options_->disable_sigusr1; } inline bool Environment::no_browser_globals() const { diff --git a/src/node_options.cc b/src/node_options.cc index 0f5b55cbc643ba..8a874ab4b1beda 100644 --- a/src/node_options.cc +++ b/src/node_options.cc @@ -386,6 +386,11 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() { " (default: current working directory)", &EnvironmentOptions::diagnostic_dir, kAllowedInEnvvar); + AddOption("--disable-sigusr1", + "Disable inspector thread to be listening for SIGUSR1 signal", + &EnvironmentOptions::disable_sigusr1, + kAllowedInEnvvar, + false); AddOption("--dns-result-order", "set default value of verbatim in dns.lookup. Options are " "'ipv4first' (IPv4 addresses are placed before IPv6 addresses) " diff --git a/src/node_options.h b/src/node_options.h index 8b9f8a825e61c4..04dbe965a57010 100644 --- a/src/node_options.h +++ b/src/node_options.h @@ -116,6 +116,7 @@ class EnvironmentOptions : public Options { bool abort_on_uncaught_exception = false; std::vector conditions; bool detect_module = true; + bool disable_sigusr1 = false; bool print_required_tla = false; bool require_module = true; std::string dns_result_order; diff --git a/test/fixtures/disable-signal/sigusr1.js b/test/fixtures/disable-signal/sigusr1.js new file mode 100644 index 00000000000000..b4deb246c8cc45 --- /dev/null +++ b/test/fixtures/disable-signal/sigusr1.js @@ -0,0 +1,2 @@ +console.log('pid is', process.pid); +setInterval(() => {}, 1000); \ No newline at end of file diff --git a/test/parallel/test-disable-sigusr1.js b/test/parallel/test-disable-sigusr1.js new file mode 100644 index 00000000000000..3303d9658aa344 --- /dev/null +++ b/test/parallel/test-disable-sigusr1.js @@ -0,0 +1,26 @@ +'use strict'; + +const common = require('../common'); +const fixtures = require('../common/fixtures'); +const { it } = require('node:test'); +const assert = require('node:assert'); +const { NodeInstance } = require('../common/inspector-helper.js'); + +common.skipIfInspectorDisabled(); + +it('should not attach a debugger with SIGUSR1', { skip: common.isWindows() }, async () => { + const file = fixtures.path('disable-signal/sigusr1.js'); + const instance = new NodeInstance(['--disable-sigusr1'], undefined, file); + + instance.on('stderr', common.mustNotCall()); + const loggedPid = await new Promise((resolve) => { + instance.on('stdout', (data) => { + const matches = data.match(/pid is (\d+)/); + if (matches) resolve(Number(matches[1])); + }); + }); + + assert.ok(process.kill(instance.pid, 'SIGUSR1')); + assert.strictEqual(loggedPid, instance.pid); + assert.ok(await instance.kill()); +}); From 63d32a4262617a0036a4f12c43bffe2c5d7e5cbe Mon Sep 17 00:00:00 2001 From: RafaelGSS Date: Mon, 6 Jan 2025 20:12:58 -0300 Subject: [PATCH 2/2] fixup! src: add --disable-sigusr1 to prevent signal i/o thread --- test/parallel/test-disable-sigusr1.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-disable-sigusr1.js b/test/parallel/test-disable-sigusr1.js index 3303d9658aa344..e1d15a25ee6505 100644 --- a/test/parallel/test-disable-sigusr1.js +++ b/test/parallel/test-disable-sigusr1.js @@ -8,7 +8,7 @@ const { NodeInstance } = require('../common/inspector-helper.js'); common.skipIfInspectorDisabled(); -it('should not attach a debugger with SIGUSR1', { skip: common.isWindows() }, async () => { +it('should not attach a debugger with SIGUSR1', { skip: common.isWindows }, async () => { const file = fixtures.path('disable-signal/sigusr1.js'); const instance = new NodeInstance(['--disable-sigusr1'], undefined, file);