Skip to content

Commit 74b034f

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 <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent 5760e41 commit 74b034f

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
@@ -85,8 +85,7 @@ struct AsyncWrapObject : public AsyncWrap {
8585
SET_SELF_SIZE(AsyncWrapObject)
8686
};
8787

88-
89-
static void DestroyAsyncIdsCallback(Environment* env, void* data) {
88+
void AsyncWrap::DestroyAsyncIdsCallback(Environment* env, void* data) {
9089
Local<Function> fn = env->async_hooks_destroy_function();
9190

9291
FatalTryCatch try_catch(env);
@@ -109,13 +108,6 @@ static void DestroyAsyncIdsCallback(Environment* env, void* data) {
109108
} while (!env->destroy_async_id_list()->empty());
110109
}
111110

112-
static void DestroyAsyncIdsCallback(void* arg) {
113-
Environment* env = static_cast<Environment*>(arg);
114-
if (!env->destroy_async_id_list()->empty())
115-
DestroyAsyncIdsCallback(env, nullptr);
116-
}
117-
118-
119111
void Emit(Environment* env, double async_id, AsyncHooks::Fields type,
120112
Local<Function> fn) {
121113
AsyncHooks* async_hooks = env->async_hooks();
@@ -448,8 +440,6 @@ void AsyncWrap::Initialize(Local<Object> target,
448440
Isolate* isolate = env->isolate();
449441
HandleScope scope(isolate);
450442

451-
env->BeforeExit(DestroyAsyncIdsCallback, env);
452-
453443
env->SetMethod(target, "setupHooks", SetupHooks);
454444
env->SetMethod(target, "pushAsyncIds", PushAsyncIds);
455445
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
@@ -142,6 +142,14 @@ Environment::Environment(IsolateData* isolate_data,
142142
AssignToContext(context, ContextInfo(""));
143143

144144
destroy_async_id_list_.reserve(512);
145+
BeforeExit(
146+
[](void* arg) {
147+
Environment* env = static_cast<Environment*>(arg);
148+
if (!env->destroy_async_id_list()->empty())
149+
AsyncWrap::DestroyAsyncIdsCallback(env, nullptr);
150+
},
151+
this);
152+
145153
performance_state_.reset(new performance::performance_state(isolate()));
146154
performance_state_->Mark(
147155
performance::NODE_PERFORMANCE_MILESTONE_ENVIRONMENT);

0 commit comments

Comments
 (0)