@@ -1099,13 +1099,8 @@ void Environment::InitializeLibuv() {
1099
1099
}
1100
1100
}
1101
1101
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
-
1108
1102
StartProfilerIdleNotifier ();
1103
+ env_handle_initialized_ = true ;
1109
1104
}
1110
1105
1111
1106
void Environment::InitializeCompileCache () {
@@ -1183,27 +1178,27 @@ void Environment::ExitEnv(StopFlags::Flags flags) {
1183
1178
});
1184
1179
}
1185
1180
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
+ }
1190
1187
1191
- env->CloseHandle (handle, [](uv_handle_t * handle) {
1188
+ auto close_and_finish = [&](uv_handle_t * handle) {
1189
+ CloseHandle (handle, [](uv_handle_t * handle) {
1192
1190
#ifdef DEBUG
1193
1191
memset (handle, 0xab , uv_handle_size (handle->type ));
1194
1192
#endif
1195
1193
});
1196
1194
};
1197
1195
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_));
1207
1202
}
1208
1203
1209
1204
void Environment::CleanupHandles () {
@@ -1223,10 +1218,6 @@ void Environment::CleanupHandles() {
1223
1218
for (HandleWrap* handle : handle_wrap_queue_)
1224
1219
handle->Close ();
1225
1220
1226
- for (HandleCleanup& hc : handle_cleanup_queue_)
1227
- hc.cb_ (this , hc.handle_ , hc.arg_ );
1228
- handle_cleanup_queue_.clear ();
1229
-
1230
1221
while (handle_cleanup_waiting_ != 0 ||
1231
1222
request_waiting_ != 0 ||
1232
1223
!handle_wrap_queue_.IsEmpty ()) {
@@ -1280,6 +1271,7 @@ MaybeLocal<Value> Environment::RunSnapshotDeserializeMain() const {
1280
1271
void Environment::RunCleanup () {
1281
1272
started_cleanup_ = true ;
1282
1273
TRACE_EVENT0 (TRACING_CATEGORY_NODE1 (environment), " RunCleanup" );
1274
+ ClosePerEnvHandles ();
1283
1275
// Only BaseObject's cleanups are registered as per-realm cleanup hooks now.
1284
1276
// Defer the BaseObject cleanup after handles are cleaned up.
1285
1277
CleanupHandles ();
0 commit comments