10000 src: cleanup per env handles directly without a list · nodejs/node@ef546c8 · GitHub
[go: up one dir, main page]

Skip to content

Commit ef546c8

Browse files
legendecastargos
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 06957ff commit ef546c8

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
@@ -1110,13 +1110,8 @@ void Environment::InitializeLibuv() {
11101110
}
11111111
}
11121112

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

11221117
void Environment::InitializeCompileCache() {
@@ -1194,27 +1189,27 @@ void Environment::ExitEnv(StopFlags::Flags flags) {
11941189
});
11951190
}
11961191

1197-
void Environment::RegisterHandleCleanups() {
1198-
HandleCleanupCb close_and_finish = [](Environment* env, uv_handle_t* handle,
1199-
void* arg) {
1200-
handle->data = env;
1192+
void Environment::ClosePerEnvHandles() {
1193+
// If LoadEnvironment and InitializeLibuv are not called, like when building
1194+
// snapshots, skip closing the per environment handles.
1195+
if (!env_handle_initialized_) {
1196+
return;
1197+
}
12011198

1202-
env->CloseHandle(handle, [](uv_handle_t* handle) {
1199+
auto close_and_finish = [&](uv_handle_t* handle) {
1200+
CloseHandle(handle, [](uv_handle_t* handle) {
12031201
#ifdef DEBUG
12041202
memset(handle, 0xab, uv_handle_size(handle->type));
12051203
#endif
12061204
});
12071205
};
12081206

1209-
auto register_handle = [&](uv_handle_t* handle) {
1210-
RegisterHandleCleanup(handle, close_and_finish, nullptr);
1211-
};
1212-
register_handle(reinterpret_cast<uv_handle_t*>(timer_handle()));
1213-
register_handle(reinterpret_cast<uv_handle_t*>(immediate_check_handle()));
1214-
register_handle(reinterpret_cast<uv_handle_t*>(immediate_idle_handle()));
1215-
register_handle(reinterpret_cast<uv_handle_t*>(&idle_prepare_handle_));
1216-
register_handle(reinterpret_cast<uv_handle_t*>(&idle_check_handle_));
1217-
register_handle(reinterpret_cast<uv_handle_t*>(&task_queues_async_));
1207+
close_and_finish(reinterpret_cast<uv_handle_t*>(timer_handle()));
1208+
close_and_finish(reinterpret_cast<uv_handle_t*>(immediate_check_handle()));
1209+
close_and_finish(reinterpret_cast<uv_handle_t*>(immediate_idle_handle()));
1210+
close_and_finish(reinterpret_cast<uv_handle_t*>(&idle_prepare_handle_));
1211+
close_and_finish(reinterpret_cast<uv_handle_t*>(&idle_check_handle_));
1212+
close_and_finish(reinterpret_cast<uv_handle_t*>(&task_queues_async_));
12181213
}
12191214

12201215
void Environment::CleanupHandles() {
@@ -1234,10 +1229,6 @@ void Environment::CleanupHandles() {
12341229
for (HandleWrap* handle : handle_wrap_queue_)
12351230
handle->Close();
12361231

1237-
for (HandleCleanup& hc : handle_cleanup_queue_)
1238-
hc.cb_(this, hc.handle_, hc.arg_);
1239-
handle_cleanup_queue_.clear();
1240-
12411232
while (handle_cleanup_waiting_ != 0 ||
12421233
request_waiting_ != 0 ||
12431234
!handle_wrap_queue_.IsEmpty()) {
@@ -1291,6 +1282,7 @@ MaybeLocal<Value> Environment::RunSnapshotDeserializeMain() const {
12911282
void Environment::RunCleanup() {
12921283
started_cleanup_ = true;
12931284
TRACE_EVENT0(TRACING_CATEGORY_NODE1(environment), "RunCleanup");
1285+
ClosePerEnvHandles();
12941286
// Only BaseObject's cleanups are registered as per-realm cleanup hooks now.
12951287
// Defer the BaseObject cleanup after handles are cleaned up.
12961288
CleanupHandles();

src/env.h

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -682,24 +682,10 @@ class Environment final : public MemoryRetainer {
682682
inline const std::vector<std::string>& argv();
683683
const std::string& exec_path() const;
684684

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

704690
template <typename T, typename OnCloseCallback>
705691
inline void CloseHandle(T* handle, OnCloseCallback callback);
@@ -1104,6 +1090,8 @@ class Environment final : public MemoryRetainer {
11041090
std::list<binding::DLib> loaded_addons_;
11051091
v8::Isolate* const isolate_;
11061092
IsolateData* const isolate_data_;
1093+
1094+
bool env_handle_initialized_ = false;
11071095
uv_timer_t timer_handle_;
11081096
uv_check_t immediate_check_handle_;
11091097
uv_idle_t immediate_idle_handle_;
@@ -1216,7 +1204,6 @@ class Environment final : public MemoryRetainer {
12161204
CleanableQueue cleanable_queue_;
12171205
HandleWrapQueue handle_wrap_queue_;
12181206
ReqWrapQueue req_wrap_queue_;
1219-
std::list<HandleCleanup> handle_cleanup_queue_;
12201207
int handle_cleanup_waiting_ = 0;
12211208
int request_waiting_ = 0;
12221209

0 commit comments

Comments
 (0)
0