Skip to content

Commit bb766ae

Browse files
addaleaxBridgeAR
authored andcommitted
async_hooks: add HandleScopes to C++ embedder/addon API
Add `HandleScope`s to the public C++ API for embedders/addons, since these methods create V8 handles that should not leak into the outer scopes. In particular, for some of the methods it was not clear from the function signatures that these functions previously needed to be invoked with a `HandleScope`. PR-URL: #24285 Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
1 parent b6d2819 commit bb766ae

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

src/async_wrap.cc

+8
Original file line numberDiff line numberDiff line change
@@ -706,13 +706,17 @@ MaybeLocal<Value> AsyncWrap::MakeCallback(const Local<Function> cb,
706706

707707

708708
async_id AsyncHooksGetExecutionAsyncId(Isolate* isolate) {
709+
// Environment::GetCurrent() allocates a Local<> handle.
710+
v8::HandleScope handle_scope(isolate);
709711
Environment* env = Environment::GetCurrent(isolate);
710712
if (env == nullptr) return -1;
711713
return env->execution_async_id();
712714
}
713715

714716

715717
async_id AsyncHooksGetTriggerAsyncId(Isolate* isolate) {
718+
// Environment::GetCurrent() allocates a Local<> handle.
719+
v8::HandleScope handle_scope(isolate);
716720
Environment* env = Environment::GetCurrent(isolate);
717721
if (env == nullptr) return -1;
718722
return env->trigger_async_id();
@@ -723,6 +727,7 @@ async_context EmitAsyncInit(Isolate* isolate,
723727
Local<Object> resource,
724728
const char* name,
725729
async_id trigger_async_id) {
730+
v8::HandleScope handle_scope(isolate);
726731
Local<String> type =
727732
String::NewFromUtf8(isolate, name, v8::NewStringType::kInternalized)
728733
.ToLocalChecked();
@@ -733,6 +738,7 @@ async_context EmitAsyncInit(Isolate* isolate,
733738
Local<Object> resource,
734739
v8::Local<v8::String> name,
735740
async_id trigger_async_id) {
741+
v8::HandleScope handle_scope(isolate);
736742
Environment* env = Environment::GetCurrent(isolate);
737743
CHECK_NOT_NULL(env);
738744

@@ -753,6 +759,8 @@ async_context EmitAsyncInit(Isolate* isolate,
753759
}
754760

755761
void EmitAsyncDestroy(Isolate* isolate, async_context asyncContext) {
762+
// Environment::GetCurrent() allocates a Local<> handle.
763+
v8::HandleScope handle_scope(isolate);
756764
AsyncWrap::EmitDestroy(
757765
Environment::GetCurrent(isolate), asyncContext.async_id);
758766
}

test/addons/async-hello-world/binding.cc

+2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ void AfterAsync(uv_work_t* r) {
5757
global, 2, argv).ToLocalChecked();
5858
}
5959

60+
// None of the following operations should allocate handles into this scope.
61+
v8::SealHandleScope seal_handle_scope(isolate);
6062
// cleanup
6163
node::EmitAsyncDestroy(isolate, req->context);
6264
req->callback.Reset();

0 commit comments

Comments
 (0)