Skip to content

Commit b9ad96f

Browse files
committed
src: add GetActiveRequests and GetActiveHandles to async_wrap.cc
1 parent 7e1659b commit b9ad96f

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

lib/internal/async_hooks.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ const { setCallbackTrampoline } = async_wrap;
4343
const {
4444
async_hook_fields,
4545
async_id_fields,
46-
execution_async_resources
46+
execution_async_resources,
47+
getActiveRequests,
48+
getActiveHandles,
4749
} = async_wrap;
4850
// Store the pair executionAsyncId and triggerAsyncId in a AliasedFloat64Array
4951
// in Environment::AsyncHooks::async_ids_stack_ which tracks the resource
@@ -540,15 +542,6 @@ function triggerAsyncId() {
540542
}
541543

542544

543-
function getActiveRequests() {
544-
return process._getActiveRequests();
545-
}
546-
547-
function getActiveHandles() {
548-
return process._getActiveHandles();
549-
}
550-
551-
552545
module.exports = {
553546
executionAsyncId,
554547
triggerAsyncId,

src/async_wrap.cc

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
#include "v8.h"
3131

32+
using v8::Array;
3233
using v8::Context;
3334
using v8::DontDelete;
3435
using v8::EscapableHandleScope;
@@ -513,6 +514,36 @@ static void RegisterDestroyHook(const FunctionCallbackInfo<Value>& args) {
513514
p->env->AddCleanupHook(DestroyParamCleanupHook, p);
514515
}
515516

517+
static void GetActiveRequests(const FunctionCallbackInfo<Value>& args) {
518+
Environment* env = Environment::GetCurrent(args);
519+
520+
std::vector<Local<Value>> request_v;
521+
for (ReqWrapBase* req_wrap : *env->req_wrap_queue()) {
522+
AsyncWrap* w = req_wrap->GetAsyncWrap();
523+
if (w->persistent().IsEmpty())
524+
continue;
525+
request_v.emplace_back(w->GetOwner());
526+
}
527+
528+
args.GetReturnValue().Set(
529+
Array::New(env->isolate(), request_v.data(), request_v.size()));
530+
}
531+
532+
// Non-static, friend of HandleWrap. Could have been a HandleWrap method but
533+
// implemented here for consistency with GetActiveRequests().
534+
void GetActiveHandles(const FunctionCallbackInfo<Value>& args) {
535+
Environment* env = Environment::GetCurrent(args);
536+
537+
std::vector<Local<Value>> handle_v;
538+
for (auto w : *env->handle_wrap_queue()) {
539+
if (!HandleWrap::HasRef(w))
540+
continue;
541+
handle_v.emplace_back(w->GetOwner());
542+
}
543+
args.GetReturnValue().Set(
544+
Array::New(env->isolate(), handle_v.data(), handle_v.size()));
545+
}
546+
516547
void AsyncWrap::GetAsyncId(const FunctionCallbackInfo<Value>& args) {
517548
AsyncWrap* wrap;
518549
args.GetReturnValue().Set(kInvalidAsyncId);
@@ -634,6 +665,9 @@ void AsyncWrap::Initialize(Local<Object> target,
634665
env->SetMethod(target, "disablePromiseHook", DisablePromiseHook);
635666
env->SetMethod(target, "registerDestroyHook", RegisterDestroyHook);
636667

668+
env->SetMethod(target, "getActiveRequests", GetActiveRequests);
669+
env->SetMethod(target, "getActiveHandles", GetActiveHandles);
670+
637671
PropertyAttribute ReadOnlyDontDelete =
638672
static_cast<PropertyAttribute>(ReadOnly | DontDelete);
639673

@@ -732,6 +766,8 @@ void AsyncWrap::RegisterExternalReferences(
732766
registry->Register(AsyncWrap::GetProviderType);
733767
registry->Register(PromiseWrap::GetAsyncId);
734768
registry->Register(PromiseWrap::GetTriggerAsyncId);
769+
registry->Register(GetActiveRequests);
770+
registry->Register(GetActiveHandles);
735771
}
736772

737773
AsyncWrap::AsyncWrap(Environment* env,

0 commit comments

Comments
 (0)