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

File tree

3 files changed

+19
-46
lines changed

3 files changed

+19
-46
lines changed

src/env-inl.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -260,12 +260,6 @@ inline uv_idle_t* Environment::immediate_idle_handle() {
260260
return &immediate_idle_handle_;
261261
}
262262

263-
inline void Environment::RegisterHandleCleanup(uv_handle_t* handle,
264-
HandleCleanupCb cb,
265-
void* arg) {
266-
handle_cleanup_queue_.push_back(HandleCleanup{handle, cb, arg});
267-
}
268-
269263
template <typename T, typename OnCloseCallback>
270264
inline void Environment::CloseHandle(T* handle, OnCloseCallback callback) {
271265
handle_cleanup_waiting_++;

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
Original file line numberDiff line numberDiff line change
@@ -683,24 +683,10 @@ class Environment : public MemoryRetainer {
683683
inline const std::vector<std::string>& argv();
684684
const std::string& exec_path() const;
685685

686-
typedef void (*HandleCleanupCb)(Environment* env,
687-
uv_handle_t* handle,
688-
void* arg);
689-
struct HandleCleanup {
690-
uv_handle_t* handle_;
691-
HandleCleanupCb cb_;
692-
void* arg_;
693-
};
694-
695-
void RegisterHandleCleanups();
696686
void CleanupHandles();
697687
void Exit(ExitCode code);
698688
void ExitEnv(StopFlags::Flags flags);
699-
700-
// Register clean-up cb to be called on environment destruction.
701-
inline void RegisterHandleCleanup(uv_handle_t* handle,
702-
HandleCleanupCb cb,
703-
void* arg);
689+
void ClosePerEnvHandles();
704690

705691
template <typename T, typename OnCloseCallback>
706692
inline void CloseHandle(T* handle, OnCloseCallback callback);
@@ -1090,6 +1076,8 @@ class Environment : public MemoryRetainer {
10901076
std::list<binding::DLib> loaded_addons_;
10911077
v8::Isolate* const isolate_;
10921078
IsolateData* const isolate_data_;
1079+
1080+
bool env_handle_initialized_ = false;
10931081
uv_timer_t timer_handle_;
10941082
uv_check_t immediate_check_handle_;
10951083
uv_idle_t immediate_idle_handle_;
@@ -1201,7 +1189,6 @@ class Environment : public MemoryRetainer {
12011189
CleanableQueue cleanable_queue_;
12021190
HandleWrapQueue handle_wrap_queue_;
12031191
ReqWrapQueue req_wrap_queue_;
1204-
std::list<HandleCleanup> handle_cleanup_queue_;
12051192
int handle_cleanup_waiting_ = 0;
12061193
int request_waiting_ = 0;
12071194

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy