Skip to content

src: add ExecutionAsyncId getter for any Context #57820

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/api/hooks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@ async_id AsyncHooksGetExecutionAsyncId(Isolate* isolate) {
return env->execution_async_id();
}

async_id AsyncHooksGetExecutionAsyncId(Local<Context> context) {
Environment* env = Environment::GetCurrent(context);
if (env == nullptr) return -1;
return env->execution_async_id();
}

async_id AsyncHooksGetTriggerAsyncId(Isolate* isolate) {
Environment* env = Environment::GetCurrent(isolate);
if (env == nullptr) return -1;
Expand Down
6 changes: 6 additions & 0 deletions src/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -1404,6 +1404,12 @@ NODE_EXTERN void RequestInterrupt(Environment* env,
* I/O from native code. */
NODE_EXTERN async_id AsyncHooksGetExecutionAsyncId(v8::Isolate* isolate);

/* Returns the id of the current execution context. If the return value is
* zero then no execution has been set. This will happen if the user handles
* I/O from native code. */
NODE_EXTERN async_id
AsyncHooksGetExecutionAsyncId(v8::Local<v8::Context> context);

/* Return same value as async_hooks.triggerAsyncId(); */
NODE_EXTERN async_id AsyncHooksGetTriggerAsyncId(v8::Isolate* isolate);

Expand Down
8 changes: 8 additions & 0 deletions test/addons/async-hooks-id/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,21 @@ void GetExecutionAsyncId(const FunctionCallbackInfo<Value>& args) {
node::AsyncHooksGetExecutionAsyncId(args.GetIsolate()));
}

void GetExecutionAsyncIdWithContext(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(node::AsyncHooksGetExecutionAsyncId(
args.GetIsolate()->GetCurrentContext()));
}

void GetTriggerAsyncId(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(
node::AsyncHooksGetTriggerAsyncId(args.GetIsolate()));
}

void Initialize(Local<Object> exports) {
NODE_SET_METHOD(exports, "getExecutionAsyncId", GetExecutionAsyncId);
NODE_SET_METHOD(exports,
"getExecutionAsyncIdWithContext",
GetExecutionAsyncIdWithContext);
NODE_SET_METHOD(exports, "getTriggerAsyncId", GetTriggerAsyncId);
}

Expand Down
8 changes: 8 additions & 0 deletions test/addons/async-hooks-id/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ assert.strictEqual(
binding.getExecutionAsyncId(),
async_hooks.executionAsyncId(),
);
assert.strictEqual(
binding.getExecutionAsyncIdWithContext(),
async_hooks.executionAsyncId(),
);
assert.strictEqual(
binding.getTriggerAsyncId(),
async_hooks.triggerAsyncId(),
Expand All @@ -19,6 +23,10 @@ process.nextTick(common.mustCall(() => {
binding.getExecutionAsyncId(),
async_hooks.executionAsyncId(),
);
assert.strictEqual(
binding.getExecutionAsyncIdWithContext(),
async_hooks.executionAsyncId(),
);
assert.strictEqual(
binding.getTriggerAsyncId(),
async_hooks.triggerAsyncId(),
Expand Down
Loading