@@ -1110,13 +1110,8 @@ void Environment::InitializeLibuv() {
1110
1110
}
1111
1111
}
1112
1112
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
-
1119
1113
StartProfilerIdleNotifier ();
1114
+ env_handle_initialized_ = true ;
1120
1115
}
1121
1116
1122
1117
void Environment::InitializeCompileCache () {
@@ -1194,27 +1189,27 @@ void Environment::ExitEnv(StopFlags::Flags flags) {
1194
1189
});
1195
1190
}
1196
1191
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
+ }
1201
1198
1202
- env->CloseHandle (handle, [](uv_handle_t * handle) {
1199
+ auto close_and_finish = [&](uv_handle_t * handle) {
1200
+ CloseHandle (handle, [](uv_handle_t * handle) {
1203
1201
#ifdef DEBUG
1204
1202
memset (handle, 0xab , uv_handle_size (handle->type ));
1205
1203
#endif
1206
1204
});
1207
1205
};
1208
1206
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_));
1218
1213
}
1219
1214
1220
1215
void Environment::CleanupHandles () {
@@ -1234,10 +1229,6 @@ void Environment::CleanupHandles() {
1234
1229
for (HandleWrap* handle : handle_wrap_queue_)
1235
1230
handle->Close ();
1236
1231
1237
- for (HandleCleanup& hc : handle_cleanup_queue_)
1238
- hc.cb_ (this , hc.handle_ , hc.arg_ );
1239
- handle_cleanup_queue_.clear ();
1240
-
1241
1232
while (handle_cleanup_waiting_ != 0 ||
1242
1233
request_waiting_ != 0 ||
1243
1234
!handle_wrap_queue_.IsEmpty ()) {
@@ -1291,6 +1282,7 @@ MaybeLocal<Value> Environment::RunSnapshotDeserializeMain() const {
1291
1282
void Environment::RunCleanup () {
1292
1283
started_cleanup_ = true ;
1293
1284
TRACE_EVENT0 (TRACING_CATEGORY_NODE1 (environment), " RunCleanup" );
1285
+ ClosePerEnvHandles ();
1294
1286
// Only BaseObject's cleanups are registered as per-realm cleanup hooks now.
1295
1287
// Defer the BaseObject cleanup after handles are cleaned up.
1296
1288
CleanupHandles ();
0 commit comments