@@ -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
11111106void 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
12091204void 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 {
12801271void 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 ();
0 commit comments