trace_events: fix abort when Node.js does not own the V8 platform · nodejs/node@66fd788 · GitHub
Skip to content

Commit 66fd788

Browse files
codebytereaduh95
authored andcommitted
trace_events: fix abort when Node.js does not own the V8 platform
Requiring `node:trace_events` and calling `createTracing()` aborted the process, and `getEnabledCategories()` dereferenced a null pointer, when Node.js runs on an embedder's own platform (`kNoInitializeNodeV8Platform`): no `tracing::Agent` exists then, and `lib/trace_events.js` only checked the compile-time `hasTracing` flag and `ownsProcessState` before handing the null agent to the binding. Have the binding report whether an agent exists and throw the existing `ERR_TRACE_EVENTS_UNAVAILABLE` when it does not, as for a `--without-v8-platform` build. Refs: #19803 Signed-off-by: Shelley Vohr <shelley.vohr@gmail.com> PR-URL: #65954 Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
1 parent dda22c0 commit 66fd788

4 files changed

Lines changed: 32 additions & 3 deletions

File tree

‎doc/api/errors.md‎

Lines changed: 2 additions & 1 deletion

‎lib/trace_events.js‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@ const {
1515
} = require('internal/errors').codes;
1616

1717
const { ownsProcessState } = require('internal/worker');
18-
if (!hasTracing || !ownsProcessState)
18+
const {
19+
CategorySet,
20+
getEnabledCategories,
21+
hasAgent,
22+
} = internalBinding('trace_events');
23+
if (!hasTracing || !ownsProcessState || !hasAgent())
1924
throw new ERR_TRACE_EVENTS_UNAVAILABLE();
2025

21-
const { CategorySet, getEnabledCategories } = internalBinding('trace_events');
2226
const { customInspectSymbol } = require('internal/util');
2327
const { format } = require('internal/util/inspect');
2428
const {

‎src/node_trace_events.cc‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ void NodeCategorySet::Disable(const FunctionCallbackInfo<Value>& args) {
113113
}
114114
}
115115

116+
static void HasAgent(const FunctionCallbackInfo<Value>& args) {
117+
args.GetReturnValue().Set(tracing::Agent::GetInstance() != nullptr);
118+
}
119+
116120
void GetEnabledCategories(const FunctionCallbackInfo<Value>& args) {
117121
Environment* env = Environment::GetCurrent(args);
118122
std::string categories =
@@ -162,6 +166,7 @@ void NodeCategorySet::Initialize(Local<Object> target,
162166
Environment* env = Environment::GetCurrent(context);
163167
Isolate* isolate = env->isolate();
164168

169+
SetMethod(context, target, "hasAgent", HasAgent);
165170
SetMethod(context, target, "getEnabledCategories", GetEnabledCategories);
166171
SetMethod(context,
167172
target,
@@ -203,6 +208,7 @@ void NodeCategorySet::Initialize(Local<Object> target,
203208

204209
void NodeCategorySet::RegisterExternalReferences(
205210
ExternalReferenceRegistry* registry) {
211+
registry->Register(HasAgent);
206212
registry->Register(GetEnabledCategories);
207213
registry->Register(SetTraceCategoryStateUpdateHandler);
208214
registry->Register(GetCategoryEnabledBuffer);
Lines changed: 18 additions & 0 deletions

0 commit comments

Comments
 (0)