Skip to content

Commit 5c8d0df

Browse files
committed
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.
1 parent c42d846 commit 5c8d0df

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
@@ -1101,13 +1101,8 @@ void Environment::InitializeLibuv() {
11011101
}
11021102
}
11031103

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

11131108
void Environment::InitializeCompileCache() {
@@ -1178,27 +1173,27 @@ void Environment::ExitEnv(StopFlags::Flags flags) {
11781173
});
11791174
}
11801175

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

1186-
env->CloseHandle(handle, [](uv_handle_t* handle) {
1183+
auto close_and_finish = [&](uv_handle_t* handle) {
1184+
CloseHandle(handle, [](uv_handle_t* handle) {
11871185
#ifdef DEBUG
11881186
memset(handle, 0xab, uv_handle_size(handle->type));
11891187
#endif
11901188
});
11911189
};
11921190

1193-
auto register_handle = [&](uv_handle_t* handle) {
1194-
RegisterHandleCleanup(handle, close_and_finish, nullptr);
1195-
};
1196-
register_handle(reinterpret_cast<uv_handle_t*>(timer_handle()));
1197-
register_handle(reinterpret_cast<uv_handle_t*>(immediate_check_handle()));
1198-
register_handle(reinterpret_cast<uv_handle_t*>(immediate_idle_handle()));
1199-
register_handle(reinterpret_cast<uv_handle_t*>(&idle_prepare_handle_));
1200-
register_handle(reinterpret_cast<uv_handle_t*>(&idle_check_handle_));
1201-
register_handle(reinterpret_cast<uv_handle_t*>(&task_queues_async_));
1191+
close_and_finish(reinterpret_cast<uv_handle_t*>(timer_handle()));
1192+
close_and_finish(reinterpret_cast<uv_handle_t*>(immediate_check_handle()));
1193+
close_and_finish(reinterpret_cast<uv_handle_t*>(immediate_idle_handle()));
1194+
close_and_finish(reinterpret_cast<uv_handle_t*>(&idle_prepare_handle_));
1195+
close_and_finish(reinterpret_cast<uv_handle_t*>(&idle_check_handle_));
1196+
close_and_finish(reinterpret_cast<uv_handle_t*>(&task_queues_async_));
12021197
}
12031198

12041199
void Environment::CleanupHandles() {
@@ -1218,10 +1213,6 @@ void Environment::CleanupHandles() {
12181213
for (HandleWrap* handle : handle_wrap_queue_)
12191214
handle->Close();
12201215

1221-
for (HandleCleanup& hc : handle_cleanup_queue_)
1222-
hc.cb_(this, hc.handle_, hc.arg_);
1223-
handle_cleanup_queue_.clear();
1224-
12251216
while (handle_cleanup_waiting_ != 0 ||
12261217
request_waiting_ != 0 ||
12271218
!handle_wrap_queue_.IsEmpty()) {
@@ -1275,6 +1266,7 @@ MaybeLocal<Value> Environment::RunSnapshotDeserializeMain() const {
12751266
void Environment::RunCleanup() {
12761267
started_cleanup_ = true;
12771268
TRACE_EVENT0(TRACING_CATEGORY_NODE1(environment), "RunCleanup");
1269+
ClosePerEnvHandles();
12781270
// Only BaseObject's cleanups are registered as per-realm cleanup hooks now.
12791271
// Defer the BaseObject cleanup after handles are cleaned up.
12801272
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);
@@ -1099,6 +1085,8 @@ class Environment final : public MemoryRetainer {
10991085
std::list<binding::DLib> loaded_addons_;
11001086
v8::Isolate* const isolate_;
11011087
IsolateData* const isolate_data_;
1088+
1089+
bool env_handle_initialized_ = false;
11021090
uv_timer_t timer_handle_;
11031091
uv_check_t immediate_check_handle_;
11041092
uv_idle_t immediate_idle_handle_;
@@ -1211,7 +1199,6 @@ class Environment final : public MemoryRetainer {
12111199
CleanableQueue cleanable_queue_;
12121200
HandleWrapQueue handle_wrap_queue_;
12131201
ReqWrapQueue req_wrap_queue_;
1214-
std::list<HandleCleanup> handle_cleanup_queue_;
12151202
int handle_cleanup_waiting_ = 0;
12161203
int request_waiting_ = 0;
12171204

0 commit comments

Comments
 (0)