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: https://github.com/nodejs/node/pull/19803
Signed-off-by: Shelley Vohr <shelley.vohr@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/65954
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
This commit is contained in:
Shelley Vohr authored and Antoine du Hamel committed 2026-09-25 18:01:55 +02:00
1 parent dc6b9ba5b2
commit ebc028d75d
4 files changed
+32 -3

No files matched your search

+2 -1
View File
@@ -3329,7 +3329,8 @@ category.
### `ERR_TRACE_EVENTS_UNAVAILABLE`
The `node:trace_events` module could not be loaded because Node.js was compiled
with the `--without-v8-platform` flag.
with the `--without-v8-platform` flag, or because the process was initialized by
an embedder that provides its own V8 platform.
<a id="ERR_TRAILING_JUNK_AFTER_STREAM_END"></a>
+6 -2
View File
@@ -15,10 +15,14 @@ const {
} = require('internal/errors').codes;
const { ownsProcessState } = require('internal/worker');
if (!hasTracing || !ownsProcessState)
const {
CategorySet,
getEnabledCategories,
hasAgent,
} = internalBinding('trace_events');
if (!hasTracing || !ownsProcessState || !hasAgent())
throw new ERR_TRACE_EVENTS_UNAVAILABLE();
const { CategorySet, getEnabledCategories } = internalBinding('trace_events');
const { customInspectSymbol } = require('internal/util');
const { format } = require('internal/util/inspect');
const {
+6
View File
@@ -114,6 +114,10 @@ void NodeCategorySet::Disable(const FunctionCallbackInfo<Value>& args) {
}
}
static void HasAgent(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(tracing::Agent::GetInstance() != nullptr);
}
void GetEnabledCategories(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
std::string categories =
@@ -160,6 +164,7 @@ void NodeCategorySet::Initialize(Local<Object> target,
Environment* env = Environment::GetCurrent(context);
Isolate* isolate = env->isolate();
SetMethod(context, target, "hasAgent", HasAgent);
SetMethod(context, target, "getEnabledCategories", GetEnabledCategories);
SetMethod(context,
target,
@@ -193,6 +198,7 @@ void NodeCategorySet::Initialize(Local<Object> target,
void NodeCategorySet::RegisterExternalReferences(
ExternalReferenceRegistry* registry) {
registry->Register(HasAgent);
registry->Register(GetEnabledCategories);
registry->Register(SetTraceCategoryStateUpdateHandler);
registry->Register(GetCategoryEnabledBuffer);
@@ -0,0 +1,18 @@
'use strict';
// The embedtest binary runs on its own MultiIsolatePlatform without Node's
// tracing agent, so node:trace_events must report itself as unavailable.
const common = require('../common');
const { spawnSyncAndAssert } = require('../common/child_process');
spawnSyncAndAssert(
common.resolveBuiltBinary('embedtest'),
[
'try { require("node:trace_events").createTracing({ categories: ["v8"] }); }' +
'catch (e) { console.log(e.code); }',
],
{
trim: true,
stdout: 'ERR_TRACE_EVENTS_UNAVAILABLE',
});