Skip to content

Commit 7dd70b2

Browse files
committed
src: add GetActiveRequests and GetActiveHandles to async_wrap.cc
1 parent 4aecb7f commit 7dd70b2

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-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: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,36 @@ static void RegisterDestroyHook(const FunctionCallbackInfo<Value>& args) {
513513
p->env->AddCleanupHook(DestroyParamCleanupHook, p);
514514
}
515515

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

667+
env->SetMethod(target, "getActiveRequests", GetActiveRequests);
668+
env->SetMethod(target, "getActiveHandles", GetActiveHandles);
669+
637670
PropertyAttribute ReadOnlyDontDelete =
638671
static_cast<PropertyAttribute>(ReadOnly | DontDelete);
639672

@@ -732,6 +765,8 @@ void AsyncWrap::RegisterExternalReferences(
732765
registry->Register(AsyncWrap::GetProviderType);
733766
registry->Register(PromiseWrap::GetAsyncId);
734767
registry->Register(PromiseWrap::GetTriggerAsyncId);
768+
registry->Register(GetActiveRequests);
769+
registry->Register(GetActiveHandles);
735770
}
736771

737772
AsyncWrap::AsyncWrap(Environment* env,

0 commit comments

Comments
 (0)