Skip to content

Commit 1481e5b

Browse files
joyeecheungtargos
authored andcommitted
process: set the trace category update handler during bootstrap
Set the trace category update handler during bootstrap, but delay the initial invocation of it until pre-execution. In addition, do not serialize the `node.async_hooks` category state when loading the trace_event binding during bootstrap, since it depends on run time states (e.g. CLI flags). Instead, use the `isTraceCategoryEnabled` v8 intrinsics to query that value during pre-execution. PR-URL: #26605 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent 9ef0a29 commit 1481e5b

File tree

4 files changed

+27
-33
lines changed

4 files changed

+27
-33
lines changed

lib/internal/bootstrap/node.js

+7
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,13 @@ if (process.platform === 'win32') {
250250
}
251251
}
252252

253+
// Set the per-Environment callback that will be called
254+
// when the TrackingTraceStateObserver updates trace state.
255+
// Note that when NODE_USE_V8_PLATFORM is true, the observer is
256+
// attached to the per-process TracingController.
257+
const { setTraceCategoryStateUpdateHandler } = internalBinding('trace_events');
258+
setTraceCategoryStateUpdateHandler(perThreadSetup.toggleTraceCategoryState);
259+
253260
// process.allowedNodeEnvironmentFlags
254261
Object.defineProperty(process, 'allowedNodeEnvironmentFlags', {
255262
get() {

lib/internal/bootstrap/pre_execution.js

+3-23
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
'use strict';
22

33
const { getOptionValue } = require('internal/options');
4-
// Lazy load internal/trace_events_async_hooks only if the async_hooks
5-
// trace event category is enabled.
6-
let traceEventsAsyncHook;
74

85
function prepareMainThreadExecution() {
96
setupTraceCategoryState();
@@ -136,26 +133,9 @@ function initializeReportSignalHandlers() {
136133
}
137134

138135
function setupTraceCategoryState() {
139-
const {
140-
asyncHooksEnabledInitial,
141-
setTraceCategoryStateUpdateHandler
142-
} = internalBinding('trace_events');
143-
144-
toggleTraceCategoryState(asyncHooksEnabledInitial);
145-
setTraceCategoryStateUpdateHandler(toggleTraceCategoryState);
146-
}
147-
148-
// Dynamically enable/disable the traceEventsAsyncHook
149-
function toggleTraceCategoryState(asyncHooksEnabled) {
150-
if (asyncHooksEnabled) {
151-
if (!traceEventsAsyncHook) {
152-
traceEventsAsyncHook =
153-
require('internal/trace_events_async_hooks').createHook();
154-
}
155-
traceEventsAsyncHook.enable();
156-
} else if (traceEventsAsyncHook) {
157-
traceEventsAsyncHook.disable();
158-
}
136+
const { isTraceCategoryEnabled } = internalBinding('trace_events');
137+
const { toggleTraceCategoryState } = require('internal/process/per_thread');
138+
toggleTraceCategoryState(isTraceCategoryEnabled('node.async_hooks'));
159139
}
160140

161141
// In general deprecations are intialized wherever the APIs are implemented,

lib/internal/process/per_thread.js

+17
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,24 @@ function buildAllowedFlags() {
304304
));
305305
}
306306

307+
// Lazy load internal/trace_events_async_hooks only if the async_hooks
308+
// trace event category is enabled.
309+
let traceEventsAsyncHook;
310+
// Dynamically enable/disable the traceEventsAsyncHook
311+
function toggleTraceCategoryState(asyncHooksEnabled) {
312+
if (asyncHooksEnabled) {
313+
if (!traceEventsAsyncHook) {
314+
traceEventsAsyncHook =
315+
require('internal/trace_events_async_hooks').createHook();
316+
}
317+
traceEventsAsyncHook.enable();
318+
} else if (traceEventsAsyncHook) {
319+
traceEventsAsyncHook.disable();
320+
}
321+
}
322+
307323
module.exports = {
324+
toggleTraceCategoryState,
308325
assert,
309326
buildAllowedFlags,
310327
wrapProcessMethods

src/node_trace_events.cc

-10
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
namespace node {
1212

1313
using v8::Array;
14-
using v8::Boolean;
1514
using v8::Context;
1615
using v8::Function;
1716
using v8::FunctionCallbackInfo;
@@ -149,15 +148,6 @@ void NodeCategorySet::Initialize(Local<Object> target,
149148
.FromJust();
150149
target->Set(context, trace,
151150
binding->Get(context, trace).ToLocalChecked()).FromJust();
152-
153-
// Initial value of async hook trace events
154-
bool async_hooks_enabled = (*(TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(
155-
TRACING_CATEGORY_NODE1(async_hooks)))) != 0;
156-
target
157-
->Set(context,
158-
FIXED_ONE_BYTE_STRING(env->isolate(), "asyncHooksEnabledInitial"),
159-
Boolean::New(env->isolate(), async_hooks_enabled))
160-
.FromJust();
161151
}
162152

163153
} // namespace node

0 commit comments

Comments
 (0)