src: cleanup per env handles directly without a list · nodejs/node@7d01b6a · GitHub
Skip to content

Commit 7d01b6a

Browse files
legendecasmarco-ippolito
authored andcommitted
src: cleanup per env handles directly without a list
Environment handles can be cleaned up directly without saving the references in a list and iterate the list. PR-URL: #54993 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
1 parent 6f53c09 commit 7d01b6a

3 files changed

Lines changed: 19 additions & 46 deletions

File tree

src/env-inl.h

Lines changed: 0 additions & 6 deletions

src/env.cc

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,13 +1109,8 @@ void Environment::InitializeLibuv() {
11091109
}
11101110
}
11111111

1112-
// Register clean-up cb to be called to clean up the handles
1113-
// when the environment is freed, note that they are not cleaned in
1114-
// the one environment per process setup, but will be called in
1115-
// FreeEnvironment.
1116-
RegisterHandleCleanups();
1117-
11181112
StartProfilerIdleNotifier();
1113+
env_handle_initialized_ = true;
11191114
}
11201115

11211116
void Environment::ExitEnv(StopFlags::Flags flags) {
@@ -1136,27 +1131,27 @@ void Environment::ExitEnv(StopFlags::Flags flags) {
11361131
});
11371132
}
11381133

1139-
void Environment::RegisterHandleCleanups() {
1140-
HandleCleanupCb close_and_finish = [](Environment* env, uv_handle_t* handle,
1141-
void* arg) {
1142-
handle->data = env;
1134+
void Environment::ClosePerEnvHandles() {
1135+
// If LoadEnvironment and InitializeLibuv are not called, like when building
1136+
// snapshots, skip closing the per environment handles.
1137+
if (!env_handle_initialized_) {
1138+
return;
1139+
}
11431140

1144-
env->CloseHandle(handle, [](uv_handle_t* handle) {
1141+
auto close_and_finish = [&](uv_handle_t* handle) {
1142+
CloseHandle(handle, [](uv_handle_t* handle) {
11451143
#ifdef DEBUG
11461144
memset(handle, 0xab, uv_handle_size(handle->type));
11471145
#endif
11481146
});
11491147
};
11501148

1151-
auto register_handle = [&](uv_handle_t* handle) {
1152-
RegisterHandleCleanup(handle, close_and_finish, nullptr);
1153-
};
1154-
register_handle(reinterpret_cast<uv_handle_t*>(timer_handle()));
1155-
register_handle(reinterpret_cast<uv_handle_t*>(immediate_check_handle()));
1156-
register_handle(reinterpret_cast<uv_handle_t*>(immediate_idle_handle()));
1157-
register_handle(reinterpret_cast<uv_handle_t*>(&idle_prepare_handle_));
1158-
register_handle(reinterpret_cast<uv_handle_t*>(&idle_check_handle_));
1159-
register_handle(reinterpret_cast<uv_handle_t*>(&task_queues_async_));
1149+
close_and_finish(reinterpret_cast<uv_handle_t*>(timer_handle()));
1150+
close_and_finish(reinterpret_cast<uv_handle_t*>(immediate_check_handle()));
1151+
close_and_finish(reinterpret_cast<uv_handle_t*>(immediate_idle_handle()));
1152+
close_and_finish(reinterpret_cast<uv_handle_t*>(&idle_prepare_handle_));
1153+
close_and_finish(reinterpret_cast<uv_handle_t*>(&idle_check_handle_));
1154+
close_and_finish(reinterpret_cast<uv_handle_t*>(&task_queues_async_));
11601155
}
11611156

11621157
void Environment::CleanupHandles() {
@@ -1176,10 +1171,6 @@ void Environment::CleanupHandles() {
11761171
for (HandleWrap* handle : handle_wrap_queue_)
11771172
handle->Close();
11781173

1179-
for (HandleCleanup& hc : handle_cleanup_queue_)
1180-
hc.cb_(this, hc.handle_, hc.arg_);
1181-
handle_cleanup_queue_.clear();
1182-
11831174
while (handle_cleanup_waiting_ != 0 ||
11841175
request_waiting_ != 0 ||
11851176
!handle_wrap_queue_.IsEmpty()) {
@@ -1233,6 +1224,7 @@ MaybeLocal<Value> Environment::RunSnapshotDeserializeMain() const {
12331224
void Environment::RunCleanup() {
12341225
started_cleanup_ = true;
12351226
TRACE_EVENT0(TRACING_CATEGORY_NODE1(environment), "RunCleanup");
1227+
ClosePerEnvHandles();
12361228
// Only BaseObject's cleanups are registered as per-realm cleanup hooks now.
12371229
// Defer the BaseObject cleanup after handles are cleaned up.
12381230
CleanupHandles();

src/env.h

Lines changed: 3 additions & 16 deletions

0 commit comments

Comments
 (0)