Skip to content

Commit 5cc2574

Browse files
joyeecheungrvagg
authored andcommitted
src: move async hooks trace events setup to pre_execution.js
Reasons: - Moves more environment-dependent setup out of bootstrap/node.js - No async operations should be done before the call to the setup functions in pre_execution.js so no async hooks should be triggered before that. Therefore it is safe to delay the setup until then. PR-URL: #26062 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Richard Lau <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent f0a8166 commit 5cc2574

File tree

4 files changed

+40
-35
lines changed

4 files changed

+40
-35
lines changed

lib/internal/bootstrap/node.js

-31
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ const { getOptionValue } = NativeModule.require('internal/options');
4242
const config = internalBinding('config');
4343
const { deprecate } = NativeModule.require('internal/util');
4444

45-
setupTraceCategoryState();
46-
4745
setupProcessObject();
4846

4947
setupGlobalProxy();
@@ -354,35 +352,6 @@ if (getOptionValue('--experimental-report')) {
354352
}
355353
}
356354

357-
function setupTraceCategoryState() {
358-
const {
359-
traceCategoryState,
360-
setTraceCategoryStateUpdateHandler
361-
} = internalBinding('trace_events');
362-
const kCategoryAsyncHooks = 0;
363-
let traceEventsAsyncHook;
364-
365-
function toggleTraceCategoryState() {
366-
// Dynamically enable/disable the traceEventsAsyncHook
367-
const asyncHooksEnabled = !!traceCategoryState[kCategoryAsyncHooks];
368-
369-
if (asyncHooksEnabled) {
370-
// Lazy load internal/trace_events_async_hooks only if the async_hooks
371-
// trace event category is enabled.
372-
if (!traceEventsAsyncHook) {
373-
traceEventsAsyncHook =
374-
NativeModule.require('internal/trace_events_async_hooks');
375-
}
376-
traceEventsAsyncHook.enable();
377-
} else if (traceEventsAsyncHook) {
378-
traceEventsAsyncHook.disable();
379-
}
380-
}
381-
382-
toggleTraceCategoryState();
383-
setTraceCategoryStateUpdateHandler(toggleTraceCategoryState);
384-
}
385-
386355
function setupProcessObject() {
387356
const EventEmitter = NativeModule.require('events');
388357
const origProcProto = Object.getPrototypeOf(process);

lib/internal/bootstrap/pre_execution.js

+32-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
const { getOptionValue } = require('internal/options');
44

55
function prepareMainThreadExecution() {
6+
setupTraceCategoryState();
7+
68
// If the process is spawned with env NODE_CHANNEL_FD, it's probably
79
// spawned by our child_process module, then initialize IPC.
810
// This attaches some internal event listeners and creates:
@@ -23,6 +25,34 @@ function prepareMainThreadExecution() {
2325
loadPreloadModules();
2426
}
2527

28+
function setupTraceCategoryState() {
29+
const {
30+
traceCategoryState,
31+
setTraceCategoryStateUpdateHandler
32+
} = internalBinding('trace_events');
33+
const kCategoryAsyncHooks = 0;
34+
let traceEventsAsyncHook;
35+
36+
function toggleTraceCategoryState() {
37+
// Dynamically enable/disable the traceEventsAsyncHook
38+
const asyncHooksEnabled = !!traceCategoryState[kCategoryAsyncHooks];
39+
40+
if (asyncHooksEnabled) {
41+
// Lazy load internal/trace_events_async_hooks only if the async_hooks
42+
// trace event category is enabled.
43+
if (!traceEventsAsyncHook) {
44+
traceEventsAsyncHook = require('internal/trace_events_async_hooks');
45+
}
46+
traceEventsAsyncHook.enable();
47+
} else if (traceEventsAsyncHook) {
48+
traceEventsAsyncHook.disable();
49+
}
50+
}
51+
52+
toggleTraceCategoryState();
53+
setTraceCategoryStateUpdateHandler(toggleTraceCategoryState);
54+
}
55+
2656
// In general deprecations are intialized wherever the APIs are implemented,
2757
// this is used to deprecate APIs implemented in C++ where the deprecation
2858
// utitlities are not easily accessible.
@@ -150,5 +180,6 @@ module.exports = {
150180
prepareMainThreadExecution,
151181
initializeDeprecations,
152182
initializeESMLoader,
153-
loadPreloadModules
183+
loadPreloadModules,
184+
setupTraceCategoryState
154185
};

lib/internal/main/check_syntax.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,16 @@ if (process.argv[1] && process.argv[1] !== '-') {
2222
// Expand process.argv[1] into a full path.
2323
const path = require('path');
2424
process.argv[1] = path.resolve(process.argv[1]);
25+
26+
// TODO(joyeecheung): not every one of these are necessary
27+
prepareMainThreadExecution();
28+
2529
// Read the source.
2630
const filename = CJSModule._resolveFilename(process.argv[1]);
2731

2832
const fs = require('fs');
2933
const source = fs.readFileSync(filename, 'utf-8');
3034

31-
// TODO(joyeecheung): not every one of these are necessary
32-
prepareMainThreadExecution();
3335
markBootstrapComplete();
3436

3537
checkScriptSyntax(source, filename);

lib/internal/main/worker_thread.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
const {
77
initializeDeprecations,
88
initializeESMLoader,
9-
loadPreloadModules
9+
loadPreloadModules,
10+
setupTraceCategoryState
1011
} = require('internal/bootstrap/pre_execution');
1112

1213
const {
@@ -72,6 +73,8 @@ port.on('message', (message) => {
7273
manifestURL,
7374
hasStdin
7475
} = message;
76+
77+
setupTraceCategoryState();
7578
if (manifestSrc) {
7679
require('internal/process/policy').setup(manifestSrc, manifestURL);
7780
}

0 commit comments

Comments
 (0)