src: keep ALS store in AsyncResource::MakeCallback

AsyncResource saves the async context frame when it is created, and
MakeCallback() enters it with async_context_frame::Scope. Then
node::MakeCallback() opens the callback scope with an undefined frame,
so the callback never runs in the saved one. Since AsyncContextFrame is
the default, AsyncLocalStorage loses its store in these callbacks.

Pass the saved frame to InternalMakeCallback(), as the Node-API
AsyncContext already does. This also removes the Scope from every call,
with its two Environment lookups and its global handle.

Refs: https://github.com/nodejs/node/pull/66316
Refs: https://github.com/nodejs/node/issues/43038
Refs: https://github.com/nodejs/performance/issues/24
Signed-off-by: Nigro Simone <nigro.simone@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/66326
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
This commit is contained in:
Nigro Simone authored and GitHub committed 2026-10-14 01:57:03 +00:00
1 parent 04fb145b6a
commit a0ce8f152b
4 files changed
+107 -22

No files matched your search

+30 -16
View File
@@ -1,6 +1,7 @@
#include "async_context_frame.h"
#include "env-inl.h"
#include "node.h"
#include "node_internals.h"
namespace node {
@@ -10,6 +11,7 @@ using v8::Local;
using v8::MaybeLocal;
using v8::Object;
using v8::String;
using v8::Undefined;
using v8::Value;
AsyncResource::AsyncResource(Isolate* isolate,
@@ -39,33 +41,45 @@ MaybeLocal<Value> AsyncResource::MakeCallback(Local<Function> callback,
int argc,
Local<Value>* argv) {
auto isolate = env_->isolate();
async_context_frame::Scope async_context_frame_scope(
isolate, context_frame_.Get(isolate));
return node::MakeCallback(
isolate, get_resource(), callback, argc, argv, async_context_);
// As in Node-API: node::MakeCallback() would run it with no frame.
return InternalMakeCallback(isolate,
get_resource(),
callback,
argc,
argv,
async_context_,
context_frame_.Get(isolate));
}
MaybeLocal<Value> AsyncResource::MakeCallback(const char* method,
int argc,
Local<Value>* argv) {
auto isolate = env_->isolate();
async_context_frame::Scope async_context_frame_scope(
isolate, context_frame_.Get(isolate));
return node::MakeCallback(
isolate, get_resource(), method, argc, argv, async_context_);
Local<String> method_string;
if (!String::NewFromUtf8(env_->isolate(), method).ToLocal(&method_string)) {
return {};
}
return MakeCallback(method_string, argc, argv);
}
MaybeLocal<Value> AsyncResource::MakeCallback(Local<String> symbol,
int argc,
Local<Value>* argv) {
auto isolate = env_->isolate();
async_context_frame::Scope async_context_frame_scope(
isolate, context_frame_.Get(isolate));
return node::MakeCallback(
isolate, get_resource(), symbol, argc, argv, async_context_);
// Check can_call_into_js() first because calling Get() might do so.
if (!env_->can_call_into_js()) return {};
Local<Value> callback;
{
// A getter runs in the saved frame too, so what it enters stays there.
async_context_frame::Scope async_context_frame_scope(
isolate, context_frame_.Get(isolate));
if (!get_resource()
->Get(isolate->GetCurrentContext(), symbol)
.ToLocal(&callback)) {
return {};
}
}
if (!callback->IsFunction()) return Undefined(isolate);
return MakeCallback(callback.As<Function>(), argc, argv);
}
Local<Object> AsyncResource::get_resource() {