Skip to content

Commit

Permalink
src: move async hooks trace events setup to pre_execution.js
Browse files Browse the repository at this point in the history
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: nodejs#26062
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Richard Lau <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
joyeecheung committed Feb 21, 2019
1 parent dec3dad commit 435eea6
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 35 deletions.
31 changes: 0 additions & 31 deletions lib/internal/bootstrap/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ const { getOptionValue } = NativeModule.require('internal/options');
const config = internalBinding('config');
const { deprecate } = NativeModule.require('internal/util');

setupTraceCategoryState();

setupProcessObject();

setupGlobalProxy();
Expand Down Expand Up @@ -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);
Expand Down
33 changes: 32 additions & 1 deletion lib/internal/bootstrap/pre_execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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.
Expand Down Expand Up @@ -150,5 +180,6 @@ module.exports = {
prepareMainThreadExecution,
initializeDeprecations,
initializeESMLoader,
loadPreloadModules
loadPreloadModules,
setupTraceCategoryState
};
6 changes: 4 additions & 2 deletions lib/internal/main/check_syntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 4 additions & 1 deletion lib/internal/main/worker_thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
const {
initializeDeprecations,
initializeESMLoader,
loadPreloadModules
loadPreloadModules,
setupTraceCategoryState
} = require('internal/bootstrap/pre_execution');

const {
Expand Down Expand Up @@ -72,6 +73,8 @@ port.on('message', (message) => {
manifestURL,
hasStdin
} = message;

setupTraceCategoryState();
if (manifestSrc) {
require('internal/process/policy').setup(manifestSrc, manifestURL);
}
Expand Down

0 comments on commit 435eea6

Please sign in to comment.