diff --git a/src/async_wrap.cc b/src/async_wrap.cc index 42fd723e4776c0..86142d13a32a8e 100644 --- a/src/async_wrap.cc +++ b/src/async_wrap.cc @@ -85,8 +85,7 @@ struct AsyncWrapObject : public AsyncWrap { SET_SELF_SIZE(AsyncWrapObject) }; - -static void DestroyAsyncIdsCallback(Environment* env, void* data) { +void AsyncWrap::DestroyAsyncIdsCallback(Environment* env, void* data) { Local fn = env->async_hooks_destroy_function(); FatalTryCatch try_catch(env); @@ -109,13 +108,6 @@ static void DestroyAsyncIdsCallback(Environment* env, void* data) { } while (!env->destroy_async_id_list()->empty()); } -static void DestroyAsyncIdsCallback(void* arg) { - Environment* env = static_cast(arg); - if (!env->destroy_async_id_list()->empty()) - DestroyAsyncIdsCallback(env, nullptr); -} - - void Emit(Environment* env, double async_id, AsyncHooks::Fields type, Local fn) { AsyncHooks* async_hooks = env->async_hooks(); @@ -448,8 +440,6 @@ void AsyncWrap::Initialize(Local target, Isolate* isolate = env->isolate(); HandleScope scope(isolate); - env->BeforeExit(DestroyAsyncIdsCallback, env); - env->SetMethod(target, "setupHooks", SetupHooks); env->SetMethod(target, "pushAsyncIds", PushAsyncIds); env->SetMethod(target, "popAsyncIds", PopAsyncIds); diff --git a/src/async_wrap.h b/src/async_wrap.h index 164c13e9594f14..c342898b1431ce 100644 --- a/src/async_wrap.h +++ b/src/async_wrap.h @@ -141,6 +141,7 @@ class AsyncWrap : public BaseObject { static void EmitTraceEventAfter(ProviderType type, double async_id); void EmitTraceEventDestroy(); + static void DestroyAsyncIdsCallback(Environment* env, void* data); inline ProviderType provider_type() const; diff --git a/src/env.cc b/src/env.cc index 3daf75e13043f6..d48d9357ed2984 100644 --- a/src/env.cc +++ b/src/env.cc @@ -142,6 +142,14 @@ Environment::Environment(IsolateData* isolate_data, AssignToContext(context, ContextInfo("")); destroy_async_id_list_.reserve(512); + BeforeExit( + [](void* arg) { + Environment* env = static_cast(arg); + if (!env->destroy_async_id_list()->empty()) + AsyncWrap::DestroyAsyncIdsCallback(env, nullptr); + }, + this); + performance_state_.reset(new performance::performance_state(isolate())); performance_state_->Mark( performance::NODE_PERFORMANCE_MILESTONE_ENVIRONMENT);