From 435eea6400522189c9e521bde32134b03791a576 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Sun, 10 Feb 2019 19:09:44 +0800 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/26062 Reviewed-By: Anna Henningsen Reviewed-By: Richard Lau Reviewed-By: James M Snell --- lib/internal/bootstrap/node.js | 31 ----------------------- lib/internal/bootstrap/pre_execution.js | 33 ++++++++++++++++++++++++- lib/internal/main/check_syntax.js | 6 +++-- lib/internal/main/worker_thread.js | 5 +++- 4 files changed, 40 insertions(+), 35 deletions(-) diff --git a/lib/internal/bootstrap/node.js b/lib/internal/bootstrap/node.js index 2cd13a2891068c..aa0a6b282e138a 100644 --- a/lib/internal/bootstrap/node.js +++ b/lib/internal/bootstrap/node.js @@ -42,8 +42,6 @@ const { getOptionValue } = NativeModule.require('internal/options'); const config = internalBinding('config'); const { deprecate } = NativeModule.require('internal/util'); -setupTraceCategoryState(); - setupProcessObject(); setupGlobalProxy(); @@ -316,35 +314,6 @@ if (getOptionValue('--experimental-report')) { } } -function setupTraceCategoryState() { - const { - traceCategoryState, - setTraceCategoryStateUpdateHandler - } = internalBinding('trace_events'); - const kCategoryAsyncHooks = 0; - let traceEventsAsyncHook; - - function toggleTraceCategoryState() { - // Dynamically enable/disable the traceEventsAsyncHook - const asyncHooksEnabled = !!traceCategoryState[kCategoryAsyncHooks]; - - if (asyncHooksEnabled) { - // Lazy load internal/trace_events_async_hooks only if the async_hooks - // trace event category is enabled. - if (!traceEventsAsyncHook) { - traceEventsAsyncHook = - NativeModule.require('internal/trace_events_async_hooks'); - } - traceEventsAsyncHook.enable(); - } else if (traceEventsAsyncHook) { - traceEventsAsyncHook.disable(); - } - } - - toggleTraceCategoryState(); - setTraceCategoryStateUpdateHandler(toggleTraceCategoryState); -} - function setupProcessObject() { const EventEmitter = NativeModule.require('events'); const origProcProto = Object.getPrototypeOf(process); diff --git a/lib/internal/bootstrap/pre_execution.js b/lib/internal/bootstrap/pre_execution.js index ce4af115badaab..8c9d7e1eafec73 100644 --- a/lib/internal/bootstrap/pre_execution.js +++ b/lib/internal/bootstrap/pre_execution.js @@ -3,6 +3,8 @@ const { getOptionValue } = require('internal/options'); function prepareMainThreadExecution() { + setupTraceCategoryState(); + // 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: @@ -23,6 +25,34 @@ function prepareMainThreadExecution() { loadPreloadModules(); } +function setupTraceCategoryState() { + const { + traceCategoryState, + setTraceCategoryStateUpdateHandler + } = internalBinding('trace_events'); + const kCategoryAsyncHooks = 0; + let traceEventsAsyncHook; + + function toggleTraceCategoryState() { + // Dynamically enable/disable the traceEventsAsyncHook + const asyncHooksEnabled = !!traceCategoryState[kCategoryAsyncHooks]; + + if (asyncHooksEnabled) { + // Lazy load internal/trace_events_async_hooks only if the async_hooks + // trace event category is enabled. + if (!traceEventsAsyncHook) { + traceEventsAsyncHook = require('internal/trace_events_async_hooks'); + } + traceEventsAsyncHook.enable(); + } else if (traceEventsAsyncHook) { + traceEventsAsyncHook.disable(); + } + } + + toggleTraceCategoryState(); + setTraceCategoryStateUpdateHandler(toggleTraceCategoryState); +} + // In general deprecations are intialized wherever the APIs are implemented, // this is used to deprecate APIs implemented in C++ where the deprecation // utitlities are not easily accessible. @@ -150,5 +180,6 @@ module.exports = { prepareMainThreadExecution, initializeDeprecations, initializeESMLoader, - loadPreloadModules + loadPreloadModules, + setupTraceCategoryState }; diff --git a/lib/internal/main/check_syntax.js b/lib/internal/main/check_syntax.js index 392fadb99ff668..7df70b272091ff 100644 --- a/lib/internal/main/check_syntax.js +++ b/lib/internal/main/check_syntax.js @@ -22,14 +22,16 @@ if (process.argv[1] && process.argv[1] !== '-') { // Expand process.argv[1] into a full path. const path = require('path'); process.argv[1] = path.resolve(process.argv[1]); + + // TODO(joyeecheung): not every one of these are necessary + prepareMainThreadExecution(); + // Read the source. const filename = CJSModule._resolveFilename(process.argv[1]); const fs = require('fs'); const source = fs.readFileSync(filename, 'utf-8'); - // TODO(joyeecheung): not every one of these are necessary - prepareMainThreadExecution(); markBootstrapComplete(); checkScriptSyntax(source, filename); diff --git a/lib/internal/main/worker_thread.js b/lib/internal/main/worker_thread.js index ac161b7588f04c..13c1b136edaf43 100644 --- a/lib/internal/main/worker_thread.js +++ b/lib/internal/main/worker_thread.js @@ -6,7 +6,8 @@ const { initializeDeprecations, initializeESMLoader, - loadPreloadModules + loadPreloadModules, + setupTraceCategoryState } = require('internal/bootstrap/pre_execution'); const { @@ -72,6 +73,8 @@ port.on('message', (message) => { manifestURL, hasStdin } = message; + + setupTraceCategoryState(); if (manifestSrc) { require('internal/process/policy').setup(manifestSrc, manifestURL); }