@@ -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
11221117void 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
12201215void 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 {
12911282void 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 ();
0 commit comments