From 34470b0524cf76fc3fee985f0b1bf0874a3c90bb Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Sun, 10 Feb 2019 19:28:16 +0800 Subject: [PATCH] process: setup signal handler in prepareMainThreadExecution Because this is only necessary in the main thread. Also removes the rearming of signal events since no signal event handlers should be created during the execution of node.js now that we've made the creation of stdout and stderr streams lazy - this has been demonstrated in the test coverage. PR-URL: https://github.com/nodejs/node/pull/26227 Reviewed-By: Richard Lau Reviewed-By: James M Snell Reviewed-By: Anna Henningsen --- lib/internal/bootstrap/node.js | 21 -------------------- lib/internal/bootstrap/pre_execution.js | 25 ++++++++++++++++++++++++ lib/internal/process/main_thread_only.js | 1 - 3 files changed, 25 insertions(+), 22 deletions(-) diff --git a/lib/internal/bootstrap/node.js b/lib/internal/bootstrap/node.js index aa0a6b282e1..0dc34be96e1 100644 --- a/lib/internal/bootstrap/node.js +++ b/lib/internal/bootstrap/node.js @@ -282,36 +282,15 @@ if (process.env.NODE_V8_COVERAGE) { }; } -// Worker threads don't receive signals. -if (isMainThread) { - const { - isSignal, - startListeningIfSignal, - stopListeningIfSignal - } = mainThreadSetup.createSignalHandlers(); - process.on('newListener', startListeningIfSignal); - process.on('removeListener', stopListeningIfSignal); - // re-arm pre-existing signal event registrations - // with this signal wrap capabilities. - const signalEvents = process.eventNames().filter(isSignal); - for (const ev of signalEvents) { - process.emit('newListener', ev); - } -} - if (getOptionValue('--experimental-report')) { const { config, - handleSignal, report, syncConfig } = NativeModule.require('internal/process/report'); process.report = report; // Download the CLI / ENV config into JS land. syncConfig(config, false); - if (config.events.includes('signal')) { - process.on(config.signal, handleSignal); - } } function setupProcessObject() { diff --git a/lib/internal/bootstrap/pre_execution.js b/lib/internal/bootstrap/pre_execution.js index 556c6ba39e5..57f7420d6c2 100644 --- a/lib/internal/bootstrap/pre_execution.js +++ b/lib/internal/bootstrap/pre_execution.js @@ -8,6 +8,9 @@ let traceEventsAsyncHook; function prepareMainThreadExecution() { setupTraceCategoryState(); + // Only main thread receives signals. + setupSignalHandlers(); + // If the process is spawned with env NODE_CHANNEL_FD, it's probably // spawned by our child_process module, then initialize IPC. // This attaches some internal event listeners and creates: @@ -28,6 +31,28 @@ function prepareMainThreadExecution() { loadPreloadModules(); } +function setupSignalHandlers() { + const { + createSignalHandlers + } = require('internal/process/main_thread_only'); + const { + startListeningIfSignal, + stopListeningIfSignal + } = createSignalHandlers(); + process.on('newListener', startListeningIfSignal); + process.on('removeListener', stopListeningIfSignal); + + if (getOptionValue('--experimental-report')) { + const { + config, + handleSignal + } = require('internal/process/report'); + if (config.events.includes('signal')) { + process.on(config.signal, handleSignal); + } + } +} + function setupTraceCategoryState() { const { asyncHooksEnabledInitial, diff --git a/lib/internal/process/main_thread_only.js b/lib/internal/process/main_thread_only.js index 2572402f620..24a7b02520f 100644 --- a/lib/internal/process/main_thread_only.js +++ b/lib/internal/process/main_thread_only.js @@ -144,7 +144,6 @@ function createSignalHandlers() { } return { - isSignal, startListeningIfSignal, stopListeningIfSignal };