nodejs/node · #65954
trace_events: fix abort when Node.js does not own the V8 platform
doc/api/errors.md2 + / 1 −
@@ -3381,7 +3381,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> lib/trace_events.js6 + / 2 −
@@ -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 {src/node_trace_events.cc6 + / 0 −
@@ -113,6 +113,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 =@@ -162,6 +166,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,@@ -203,6 +208,7 @@ void NodeCategorySet::Initialize(Local<Object> target, void NodeCategorySet::RegisterExternalReferences( ExternalReferenceRegistry* registry) {+ registry->Register(HasAgent); registry->Register(GetEnabledCategories); registry->Register(SetTraceCategoryStateUpdateHandler); registry->Register(GetCategoryEnabledBuffer);test/embedding/test-embedding-trace-events-unavailable.jsadded18 + / 0 −
@@ -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',+ });