@@ -1101,13 +1101,8 @@ void Environment::InitializeLibuv() {
1101
1101
}
1102
1102
}
1103
1103
1104
- // Register clean-up cb to be called to clean up the handles
1105
- // when the environment is freed, note that they are not cleaned in
1106
- // the one environment per process setup, but will be called in
1107
- // FreeEnvironment.
1108
- RegisterHandleCleanups ();
1109
-
1110
1104
StartProfilerIdleNotifier ();
1105
+ env_handle_initialized_ = true ;
1111
1106
}
1112
1107
1113
1108
void Environment::InitializeCompileCache () {
@@ -1178,27 +1173,27 @@ void Environment::ExitEnv(StopFlags::Flags flags) {
1178
1173
});
1179
1174
}
1180
1175
1181
- void Environment::RegisterHandleCleanups () {
1182
- HandleCleanupCb close_and_finish = [](Environment* env, uv_handle_t * handle,
1183
- void * arg) {
1184
- handle->data = env;
1176
+ void Environment::ClosePerEnvHandles () {
1177
+ // If LoadEnvironment and InitializeLibuv are not called, like when building
1178
+ // snapshots, skip closing the per environment handles.
1179
+ if (!env_handle_initialized_) {
1180
+ return ;
1181
+ }
1185
1182
1186
- env->CloseHandle (handle, [](uv_handle_t * handle) {
1183
+ auto close_and_finish = [&](uv_handle_t * handle) {
1184
+ CloseHandle (handle, [](uv_handle_t * handle) {
1187
1185
#ifdef DEBUG
1188
1186
memset (handle, 0xab , uv_handle_size (handle->type ));
1189
1187
#endif
1190
1188
});
1191
1189
};
1192
1190
1193
- auto register_handle = [&](uv_handle_t * handle) {
1194
- RegisterHandleCleanup (handle, close_and_finish, nullptr );
1195
- };
1196
- register_handle (reinterpret_cast <uv_handle_t *>(timer_handle ()));
1197
- register_handle (reinterpret_cast <uv_handle_t *>(immediate_check_handle ()));
1198
- register_handle (reinterpret_cast <uv_handle_t *>(immediate_idle_handle ()));
1199
- register_handle (reinterpret_cast <uv_handle_t *>(&idle_prepare_handle_));
1200
- register_handle (reinterpret_cast <uv_handle_t *>(&idle_check_handle_));
1201
- register_handle (reinterpret_cast <uv_handle_t *>(&task_queues_async_));
1191
+ close_and_finish (reinterpret_cast <uv_handle_t *>(timer_handle ()));
1192
+ close_and_finish (reinterpret_cast <uv_handle_t *>(immediate_check_handle ()));
1193
+ close_and_finish (reinterpret_cast <uv_handle_t *>(immediate_idle_handle ()));
1194
+ close_and_finish (reinterpret_cast <uv_handle_t *>(&idle_prepare_handle_));
1195
+ close_and_finish (reinterpret_cast <uv_handle_t *>(&idle_check_handle_));
1196
+ close_and_finish (reinterpret_cast <uv_handle_t *>(&task_queues_async_));
1202
1197
}
1203
1198
1204
1199
void Environment::CleanupHandles () {
@@ -1218,10 +1213,6 @@ void Environment::CleanupHandles() {
1218
1213
for (HandleWrap* handle : handle_wrap_queue_)
1219
1214
handle->Close ();
1220
1215
1221
- for (HandleCleanup& hc : handle_cleanup_queue_)
1222
- hc.cb_ (this , hc.handle_ , hc.arg_ );
1223
- handle_cleanup_queue_.clear ();
1224
-
1225
1216
while (handle_cleanup_waiting_ != 0 ||
1226
1217
request_waiting_ != 0 ||
1227
1218
!handle_wrap_queue_.IsEmpty ()) {
@@ -1275,6 +1266,7 @@ MaybeLocal<Value> Environment::RunSnapshotDeserializeMain() const {
1275
1266
void Environment::RunCleanup () {
1276
1267
started_cleanup_ = true ;
1277
1268
TRACE_EVENT0 (TRACING_CATEGORY_NODE1 (environment), " RunCleanup" );
1269
+ ClosePerEnvHandles ();
1278
1270
// Only BaseObject's cleanups are registered as per-realm cleanup hooks now.
1279
1271
// Defer the BaseObject cleanup after handles are cleaned up.
1280
1272
CleanupHandles ();
0 commit comments