Skip to content

Commit 6f3b421

Browse files
joyeecheungMylesBorins
authored andcommitted
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. PR-URL: #25020 Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent 3e1fe19 commit 6f3b421

File tree

3 files changed

+10
-11
lines changed

3 files changed

+10
-11
lines changed

src/async_wrap.cc

+1-11
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@ struct AsyncWrapObject : public AsyncWrap {
8888
SET_SELF_SIZE(AsyncWrapObject)
8989
};
9090

91-
92-
static void DestroyAsyncIdsCallback(Environment* env, void* data) {
91+
void AsyncWrap::DestroyAsyncIdsCallback(Environment* env, void* data) {
9392
Local<Function> fn = env->async_hooks_destroy_function();
9493

9594
TryCatchScope try_catch(env, TryCatchScope::CatchMode::kFatal);
@@ -112,13 +111,6 @@ static void DestroyAsyncIdsCallback(Environment* env, void* data) {
112111
} while (!env->destroy_async_id_list()->empty());
113112
}
114113

115-
static void DestroyAsyncIdsCallback(void* arg) {
116-
Environment* env = static_cast<Environment*>(arg);
117-
if (!env->destroy_async_id_list()->empty())
118-
DestroyAsyncIdsCallback(env, nullptr);
119-
}
120-
121-
122114
void Emit(Environment* env, double async_id, AsyncHooks::Fields type,
123115
Local<Function> fn) {
124116
AsyncHooks* async_hooks = env->async_hooks();
@@ -458,8 +450,6 @@ void AsyncWrap::Initialize(Local<Object> target,
458450
Isolate* isolate = env->isolate();
459451
HandleScope scope(isolate);
460452

461-
env->BeforeExit(DestroyAsyncIdsCallback, env);
462-
463453
env->SetMethod(target, "setupHooks", SetupHooks);
464454
env->SetMethod(target, "pushAsyncIds", PushAsyncIds);
465455
env->SetMethod(target, "popAsyncIds", PopAsyncIds);

src/async_wrap.h

+1
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ class AsyncWrap : public BaseObject {
141141
static void EmitTraceEventAfter(ProviderType type, double async_id);
142142
void EmitTraceEventDestroy();
143143

144+
static void DestroyAsyncIdsCallback(Environment* env, void* data);
144145

145146
inline ProviderType provider_type() const;
146147

src/env.cc

+8
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,14 @@ Environment::Environment(IsolateData* isolate_data,
211211
}
212212

213213
destroy_async_id_list_.reserve(512);
214+
BeforeExit(
215+
[](void* arg) {
216+
Environment* env = static_cast<Environment*>(arg);
217+
if (!env->destroy_async_id_list()->empty())
218+
AsyncWrap::DestroyAsyncIdsCallback(env, nullptr);
219+
},
220+
this);
221+
214222
performance_state_.reset(new performance::performance_state(isolate()));
215223
performance_state_->Mark(
216224
performance::NODE_PERFORMANCE_MILESTONE_ENVIRONMENT);

0 commit comments

Comments
 (0)