From 67a4775d36dad39d2b98c152db28c1d51dba7481 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Fri, 14 Dec 2018 00:02:57 +0800 Subject: [PATCH 1/2] src: schedule destroy hooks in BeforeExit early during bootstrap Instead of doing it in the `internalBinding('async_wrap')` initialization whose first call is uncertain depending on how the native modules are loaded in JS land during bootstrap. --- src/async_wrap.cc | 12 +----------- src/async_wrap.h | 1 + src/node.cc | 8 ++++++++ 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/async_wrap.cc b/src/async_wrap.cc index e197319100abec..47a99551275e17 100644 --- a/src/async_wrap.cc +++ b/src/async_wrap.cc @@ -88,8 +88,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(); TryCatchScope try_catch(env, TryCatchScope::CatchMode::kFatal); @@ -112,13 +111,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(); @@ -446,8 +438,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 523d620b0acb52..f7fc149c941ef0 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/node.cc b/src/node.cc index 7e1a904e52740d..d056b9c98fdb7e 100644 --- a/src/node.cc +++ b/src/node.cc @@ -1227,6 +1227,14 @@ void LoadEnvironment(Environment* env) { InitDTrace(env, global); #endif + env->BeforeExit( + [](void* arg) { + Environment* env = static_cast(arg); + if (!env->destroy_async_id_list()->empty()) + AsyncWrap::DestroyAsyncIdsCallback(env, nullptr); + }, + env); + Local process = env->process_object(); // Setting global properties for the bootstrappers to use: From 726f8b81594cce128e9ea3a0afe19588e76b6229 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Tue, 18 Dec 2018 18:45:57 +0800 Subject: [PATCH 2/2] fixup! src: schedule destroy hooks in BeforeExit early during bootstrap --- src/env.cc | 8 ++++++++ src/node.cc | 8 -------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/env.cc b/src/env.cc index 023e69665d94ed..420a3ec21d57b7 100644 --- a/src/env.cc +++ b/src/env.cc @@ -211,6 +211,14 @@ Environment::Environment(IsolateData* isolate_data, } 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); diff --git a/src/node.cc b/src/node.cc index d056b9c98fdb7e..7e1a904e52740d 100644 --- a/src/node.cc +++ b/src/node.cc @@ -1227,14 +1227,6 @@ void LoadEnvironment(Environment* env) { InitDTrace(env, global); #endif - env->BeforeExit( - [](void* arg) { - Environment* env = static_cast(arg); - if (!env->destroy_async_id_list()->empty()) - AsyncWrap::DestroyAsyncIdsCallback(env, nullptr); - }, - env); - Local process = env->process_object(); // Setting global properties for the bootstrappers to use: