Skip to content

Commit fbf5321

Browse files
addaleaxcodebytere
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 <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
1 parent 39ccf14 commit fbf5321

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
@@ -694,13 +694,17 @@ MaybeLocal<Value> AsyncWrap::MakeCallback(const Local<Function> cb,
694694

695695

696696
async_id AsyncHooksGetExecutionAsyncId(Isolate* isolate) {
697+
// Environment::GetCurrent() allocates a Local<> handle.
698+
v8::HandleScope handle_scope(isolate);
697699
Environment* env = Environment::GetCurrent(isolate);
698700
if (env == nullptr) return -1;
699701
return env->execution_async_id();
700702
}
701703

702704

703705
async_id AsyncHooksGetTriggerAsyncId(Isolate* isolate) {
706+
// Environment::GetCurrent() allocates a Local<> handle.
707+
v8::HandleScope handle_scope(isolate);
704708
Environment* env = Environment::GetCurrent(isolate);
705709
if (env == nullptr) return -1;
706710
return env->trigger_async_id();
@@ -711,6 +715,7 @@ async_context EmitAsyncInit(Isolate* isolate,
711715
Local<Object> resource,
712716
const char* name,
713717
async_id trigger_async_id) {
718+
v8::HandleScope handle_scope(isolate);
714719
Local<String> type =
715720
String::NewFromUtf8(isolate, name, v8::NewStringType::kInternalized)
716721
.ToLocalChecked();
@@ -721,6 +726,7 @@ async_context EmitAsyncInit(Isolate* isolate,
721726
Local<Object> resource,
722727
v8::Local<v8::String> name,
723728
async_id trigger_async_id) {
729+
v8::HandleScope handle_scope(isolate);
724730
Environment* env = Environment::GetCurrent(isolate);
725731
CHECK_NOT_NULL(env);
726732

@@ -741,6 +747,8 @@ async_context EmitAsyncInit(Isolate* isolate,
741747
}
742748

743749
void EmitAsyncDestroy(Isolate* isolate, async_context asyncContext) {
750+
// Environment::GetCurrent() allocates a Local<> handle.
751+
v8::HandleScope handle_scope(isolate);
744752
AsyncWrap::EmitDestroy(
745753
Environment::GetCurrent(isolate), asyncContext.async_id);
746754
}

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)