Skip to content

Commit 57179a0

Browse files
joyeecheungrvagg
authored andcommitted
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: #26227 Reviewed-By: Richard Lau <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent 1333dcc commit 57179a0

File tree

3 files changed

+25
-22
lines changed

3 files changed

+25
-22
lines changed

lib/internal/bootstrap/node.js

-21
Original file line numberDiff line numberDiff line change
@@ -320,36 +320,15 @@ if (process.env.NODE_V8_COVERAGE) {
320320
};
321321
}
322322

323-
// Worker threads don't receive signals.
324-
if (isMainThread) {
325-
const {
326-
isSignal,
327-
startListeningIfSignal,
328-
stopListeningIfSignal
329-
} = mainThreadSetup.createSignalHandlers();
330-
process.on('newListener', startListeningIfSignal);
331-
process.on('removeListener', stopListeningIfSignal);
332-
// re-arm pre-existing signal event registrations
333-
// with this signal wrap capabilities.
334-
const signalEvents = process.eventNames().filter(isSignal);
335-
for (const ev of signalEvents) {
336-
process.emit('newListener', ev);
337-
}
338-
}
339-
340323
if (getOptionValue('--experimental-report')) {
341324
const {
342325
config,
343-
handleSignal,
344326
report,
345327
syncConfig
346328
} = NativeModule.require('internal/process/report');
347329
process.report = report;
348330
// Download the CLI / ENV config into JS land.
349331
syncConfig(config, false);
350-
if (config.events.includes('signal')) {
351-
process.on(config.signal, handleSignal);
352-
}
353332
}
354333

355334
function setupProcessObject() {

lib/internal/bootstrap/pre_execution.js

+25
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ let traceEventsAsyncHook;
88
function prepareMainThreadExecution() {
99
setupTraceCategoryState();
1010

11+
// Only main thread receives signals.
12+
setupSignalHandlers();
13+
1114
// If the process is spawned with env NODE_CHANNEL_FD, it's probably
1215
// spawned by our child_process module, then initialize IPC.
1316
// This attaches some internal event listeners and creates:
@@ -28,6 +31,28 @@ function prepareMainThreadExecution() {
2831
loadPreloadModules();
2932
}
3033

34+
function setupSignalHandlers() {
35+
const {
36+
createSignalHandlers
37+
} = require('internal/process/main_thread_only');
38+
const {
39+
startListeningIfSignal,
40+
stopListeningIfSignal
41+
} = createSignalHandlers();
42+
process.on('newListener', startListeningIfSignal);
43+
process.on('removeListener', stopListeningIfSignal);
44+
45+
if (getOptionValue('--experimental-report')) {
46+
const {
47+
config,
48+
handleSignal
49+
} = require('internal/process/report');
50+
if (config.events.includes('signal')) {
51+
process.on(config.signal, handleSignal);
52+
}
53+
}
54+
}
55+
3156
function setupTraceCategoryState() {
3257
const {
3358
asyncHooksEnabledInitial,

lib/internal/process/main_thread_only.js

-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ function createSignalHandlers() {
144144
}
145145

146146
return {
147-
isSignal,
148147
startListeningIfSignal,
149148
stopListeningIfSignal
150149
};

0 commit comments

Comments
 (0)