Skip to content

Commit

Permalink
src: schedule destroy hooks in BeforeExit early during bootstrap
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
joyeecheung authored and BethGriggs committed Apr 17, 2019
1 parent ec896f9 commit a6c55f2
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
12 changes: 1 addition & 11 deletions src/async_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<Function> fn = env->async_hooks_destroy_function();

FatalTryCatch try_catch(env);
Expand All @@ -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<Environment*>(arg);
if (!env->destroy_async_id_list()->empty())
DestroyAsyncIdsCallback(env, nullptr);
}


void Emit(Environment* env, double async_id, AsyncHooks::Fields type,
Local<Function> fn) {
AsyncHooks* async_hooks = env->async_hooks();
Expand Down Expand Up @@ -448,8 +440,6 @@ void AsyncWrap::Initialize(Local<Object> 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);
Expand Down
1 change: 1 addition & 0 deletions src/async_wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
8 changes: 8 additions & 0 deletions src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<Environment*>(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);
Expand Down

0 comments on commit a6c55f2

Please sign in to comment.