Skip to content

Commit 8b8fc53

Browse files
authored
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 62383cd commit 8b8fc53

File tree

3 files changed

+19
-46
lines changed

3 files changed

+19
-46
lines changed

src/env-inl.h

-6
Original file line numberDiff line numberDiff line change
@@ -235,12 +235,6 @@ inline uv_idle_t* Environment::immediate_idle_handle() {
235235
return &immediate_idle_handle_;
236236
}
237237

238-
inline void Environment::RegisterHandleCleanup(uv_handle_t* handle,
239-
HandleCleanupCb cb,
240-
void* arg) {
241-
handle_cleanup_queue_.push_back(HandleCleanup{handle, cb, arg});
242-
}
243-
244238
template <typename T, typename OnCloseCallback>
245239
inline void Environment::CloseHandle(T* handle, OnCloseCallback callback) {
246240
handle_cleanup_waiting_++;

src/env.cc

+16-24
Original file line numberDiff line numberDiff line change
@@ -1099,13 +1099,8 @@ void Environment::InitializeLibuv() {
10991099
}
11001100
}
11011101

1102-
// Register clean-up cb to be called to clean up the handles
1103-
// when the environment is freed, note that they are not cleaned in
1104-
// the one environment per process setup, but will be called in
1105-
// FreeEnvironment.
1106-
RegisterHandleCleanups();
1107-
11081102
StartProfilerIdleNotifier();
1103+
env_handle_initialized_ = true;
11091104
}
11101105

11111106
void Environment::InitializeCompileCache() {
@@ -1183,27 +1178,27 @@ void Environment::ExitEnv(StopFlags::Flags flags) {
11831178
});
11841179
}
11851180

1186-
void Environment::RegisterHandleCleanups() {
1187-
HandleCleanupCb close_and_finish = [](Environment* env, uv_handle_t* handle,
1188-
void* arg) {
1189-
handle->data = env;
1181+
void Environment::ClosePerEnvHandles() {
1182+
// If LoadEnvironment and InitializeLibuv are not called, like when building
1183+
// snapshots, skip closing the per environment handles.
1184+
if (!env_handle_initialized_) {
1185+
return;
1186+
}
11901187

1191-
env->CloseHandle(handle, [](uv_handle_t* handle) {
1188+
auto close_and_finish = [&](uv_handle_t* handle) {
1189+
CloseHandle(handle, [](uv_handle_t* handle) {
11921190
#ifdef DEBUG
11931191
memset(handle, 0xab, uv_handle_size(handle->type));
11941192
#endif
11951193
});
11961194
};
11971195

1198-
auto register_handle = [&](uv_handle_t* handle) {
1199-
RegisterHandleCleanup(handle, close_and_finish, nullptr);
1200-
};
1201-
register_handle(reinterpret_cast<uv_handle_t*>(timer_handle()));
1202-
register_handle(reinterpret_cast<uv_handle_t*>(immediate_check_handle()));
1203-
register_handle(reinterpret_cast<uv_handle_t*>(immediate_idle_handle()));
1204-
register_handle(reinterpret_cast<uv_handle_t*>(&idle_prepare_handle_));
1205-
register_handle(reinterpret_cast<uv_handle_t*>(&idle_check_handle_));
1206-
register_handle(reinterpret_cast<uv_handle_t*>(&task_queues_async_));
1196+
close_and_finish(reinterpret_cast<uv_handle_t*>(timer_handle()));
1197+
close_and_finish(reinterpret_cast<uv_handle_t*>(immediate_check_handle()));
1198+
close_and_finish(reinterpret_cast<uv_handle_t*>(immediate_idle_handle()));
1199+
close_and_finish(reinterpret_cast<uv_handle_t*>(&idle_prepare_handle_));
1200+
close_and_finish(reinterpret_cast<uv_handle_t*>(&idle_check_handle_));
1201+
close_and_finish(reinterpret_cast<uv_handle_t*>(&task_queues_async_));
12071202
}
12081203

12091204
void Environment::CleanupHandles() {
@@ -1223,10 +1218,6 @@ void Environment::CleanupHandles() {
12231218
for (HandleWrap* handle : handle_wrap_queue_)
12241219
handle->Close();
12251220

1226-
for (HandleCleanup& hc : handle_cleanup_queue_)
1227-
hc.cb_(this, hc.handle_, hc.arg_);
1228-
handle_cleanup_queue_.clear();
1229-
12301221
while (handle_cleanup_waiting_ != 0 ||
12311222
request_waiting_ != 0 ||
12321223
!handle_wrap_queue_.IsEmpty()) {
@@ -1280,6 +1271,7 @@ MaybeLocal<Value> Environment::RunSnapshotDeserializeMain() const {
12801271
void Environment::RunCleanup() {
12811272
started_cleanup_ = true;
12821273
TRACE_EVENT0(TRACING_CATEGORY_NODE1(environment), "RunCleanup");
1274+
ClosePerEnvHandles();
12831275
// Only BaseObject's cleanups are registered as per-realm cleanup hooks now.
12841276
// Defer the BaseObject cleanup after handles are cleaned up.
12851277
CleanupHandles();

src/env.h

+3-16
Original file line numberDiff line numberDiff line change
@@ -678,24 +678,10 @@ class Environment final : public MemoryRetainer {
678678
inline const std::vector<std::string>& argv();
679679
const std::string& exec_path() const;
680680

681-
typedef void (*HandleCleanupCb)(Environment* env,
682-
uv_handle_t* handle,
683-
void* arg);
684-
struct HandleCleanup {
685-
uv_handle_t* handle_;
686-
HandleCleanupCb cb_;
687-
void* arg_;
688-
};
689-
690-
void RegisterHandleCleanups();
691681
void CleanupHandles();
692682
void Exit(ExitCode code);
693683
void ExitEnv(StopFlags::Flags flags);
694-
695-
// Register clean-up cb to be called on environment destruction.
696-
inline void RegisterHandleCleanup(uv_handle_t* handle,
697-
HandleCleanupCb cb,
698-
void* arg);
684+
void ClosePerEnvHandles();
699685

700686
template <typename T, typename OnCloseCallback>
701687
inline void CloseHandle(T* handle, OnCloseCallback callback);
@@ -1100,6 +1086,8 @@ class Environment final : public MemoryRetainer {
11001086
std::list<binding::DLib> loaded_addons_;
11011087
v8::Isolate* const isolate_;
11021088
IsolateData* const isolate_data_;
1089+
1090+
bool env_handle_initialized_ = false;
11031091
uv_timer_t timer_handle_;
11041092
uv_check_t immediate_check_handle_;
11051093
uv_idle_t immediate_idle_handle_;
@@ -1212,7 +1200,6 @@ class Environment final : public MemoryRetainer {
12121200
CleanableQueue cleanable_queue_;
12131201
HandleWrapQueue handle_wrap_queue_;
12141202
ReqWrapQueue req_wrap_queue_;
1215-
std::list<HandleCleanup> handle_cleanup_queue_;
12161203
int handle_cleanup_waiting_ = 0;
12171204
int request_waiting_ = 0;
12181205

0 commit comments

Comments
 (0)