@@ -1109,13 +1109,8 @@ void Environment::InitializeLibuv() {
1109
1109
}
1110
1110
}
1111
1111
1112
- // Register clean-up cb to be called to clean up the handles
1113
- // when the environment is freed, note that they are not cleaned in
1114
- // the one environment per process setup, but will be called in
1115
- // FreeEnvironment.
1116
- RegisterHandleCleanups ();
1117
-
1118
1112
StartProfilerIdleNotifier ();
1113
+ env_handle_initialized_ = true ;
1119
1114
}
1120
1115
1121
1116
void Environment::ExitEnv (StopFlags::Flags flags) {
@@ -1136,27 +1131,27 @@ void Environment::ExitEnv(StopFlags::Flags flags) {
1136
1131
});
1137
1132
}
1138
1133
1139
- void Environment::RegisterHandleCleanups () {
1140
- HandleCleanupCb close_and_finish = [](Environment* env, uv_handle_t * handle,
1141
- void * arg) {
1142
- handle->data = env;
1134
+ void Environment::ClosePerEnvHandles () {
1135
+ // If LoadEnvironment and InitializeLibuv are not called, like when building
1136
+ // snapshots, skip closing the per environment handles.
1137
+ if (!env_handle_initialized_) {
1138
+ return ;
1139
+ }
1143
1140
1144
- env->CloseHandle (handle, [](uv_handle_t * handle) {
1141
+ auto close_and_finish = [&](uv_handle_t * handle) {
1142
+ CloseHandle (handle, [](uv_handle_t * handle) {
1145
1143
#ifdef DEBUG
1146
1144
memset (handle, 0xab , uv_handle_size (handle->type ));
1147
1145
#endif
1148
1146
});
1149
1147
};
1150
1148
1151
- auto register_handle = [&](uv_handle_t * handle) {
1152
- RegisterHandleCleanup (handle, close_and_finish, nullptr );
1153
- };
1154
- register_handle (reinterpret_cast <uv_handle_t *>(timer_handle ()));
1155
- register_handle (reinterpret_cast <uv_handle_t *>(immediate_check_handle ()));
1156
- register_handle (reinterpret_cast <uv_handle_t *>(immediate_idle_handle ()));
1157
- register_handle (reinterpret_cast <uv_handle_t *>(&idle_prepare_handle_));
1158
- register_handle (reinterpret_cast <uv_handle_t *>(&idle_check_handle_));
1159
- register_handle (reinterpret_cast <uv_handle_t *>(&task_queues_async_));
1149
+ close_and_finish (reinterpret_cast <uv_handle_t *>(timer_handle ()));
1150
+ close_and_finish (reinterpret_cast <uv_handle_t *>(immediate_check_handle ()));
1151
+ close_and_finish (reinterpret_cast <uv_handle_t *>(immediate_idle_handle ()));
1152
+ close_and_finish (reinterpret_cast <uv_handle_t *>(&idle_prepare_handle_));
1153
+ close_and_finish (reinterpret_cast <uv_handle_t *>(&idle_check_handle_));
1154
+ close_and_finish (reinterpret_cast <uv_handle_t *>(&task_queues_async_));
1160
1155
}
1161
1156
1162
1157
void Environment::CleanupHandles () {
@@ -1176,10 +1171,6 @@ void Environment::CleanupHandles() {
1176
1171
for (HandleWrap* handle : handle_wrap_queue_)
1177
1172
handle->Close ();
1178
1173
1179
- for (HandleCleanup& hc : handle_cleanup_queue_)
1180
- hc.cb_ (this , hc.handle_ , hc.arg_ );
1181
- handle_cleanup_queue_.clear ();
1182
-
1183
1174
while (handle_cleanup_waiting_ != 0 ||
1184
1175
request_waiting_ != 0 ||
1185
1176
!handle_wrap_queue_.IsEmpty ()) {
@@ -1233,6 +1224,7 @@ MaybeLocal<Value> Environment::RunSnapshotDeserializeMain() const {
1233
1224
void Environment::RunCleanup () {
1234
1225
started_cleanup_ = true ;
1235
1226
TRACE_EVENT0 (TRACING_CATEGORY_NODE1 (environment), " RunCleanup" );
1227
+ ClosePerEnvHandles ();
1236
1228
// Only BaseObject's cleanups are registered as per-realm cleanup hooks now.
1237
1229
// Defer the BaseObject cleanup after handles are cleaned up.
1238
1230
CleanupHandles ();
0 commit comments