rzmq 0.5.11

High performance, fully asynchronous, safe pure-Rust implementation of ZeroMQ (ØMQ) messaging with io_uring and TCP Cork acceleration on Linux.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
use crate::error::ZmqError;
use crate::runtime::{ActorType, Command, SystemEvent};
use crate::socket::connection_iface::ISocketConnection;
use crate::socket::core::state::{CoreState, EndpointType, ShutdownCoordinator, ShutdownPhase};
use crate::socket::core::{command_processor, pipe_manager, SocketCore};
use crate::socket::ISocket;

use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, Instant};

// --- ShutdownCoordinator Methods ---
impl ShutdownCoordinator {
  pub(crate) fn current_phase(&self) -> ShutdownPhase {
    self.state
  }

  pub(crate) fn begin_shutdown_sequence(
    &mut self,
    core_handle: usize,
    core_s_reader: &CoreState, // Pass read reference to CoreState
  ) -> bool {
    if self.state != ShutdownPhase::Running {
      return false;
    }
    tracing::debug!(
      handle = core_handle,
      "ShutdownCoordinator: Initiating shutdown. Populating pending lists."
    );

    self.pending_child_actors.clear();
    self.pending_connections_to_close.clear();
    #[cfg(feature = "inproc")]
    self.inproc_connections_to_cleanup.clear();

    for (ep_uri, ep_info) in core_s_reader.endpoints.iter() {
      match ep_info.endpoint_type {
        EndpointType::Listener => {
          // Only track listeners if SocketCore actually has a task_handle for them.
          if ep_info.task_handle.is_some() {
            self
              .pending_child_actors
              .insert(ep_info.handle_id, ep_uri.clone());
            tracing::trace!(handle=core_handle, child_id=ep_info.handle_id, uri=%ep_uri, "Registered Listener for shutdown tracking.");
          }
        }
        EndpointType::Session => {
          // For all active sessions (SessionActor or UringFd), record them for ISocketConnection::close_connection()
          self.pending_connections_to_close.insert(
            ep_info.handle_id, // Keyed by Session actor handle or RawFd (as usize)
            (ep_uri.clone(), ep_info.connection_iface.clone()),
          );
          tracing::trace!(handle=core_handle, conn_id=ep_info.handle_id, uri=%ep_uri, "Registered active Connection for ISocketConnection.close().");

          #[cfg(feature = "inproc")]
          if ep_uri.starts_with("inproc://") && !ep_info.is_outbound_connection {
            if let Some(pipe_ids) = ep_info.pipe_ids {
              // These are actual pipe IDs for inproc binder
              self
                .inproc_connections_to_cleanup
                .push((pipe_ids.0, pipe_ids.1, ep_uri.clone()));
              tracing::trace!(handle=core_handle, uri=%ep_uri, "Registered inproc binder connection for final pipe cleanup.");
            }
          }
        }
      }
    }
    true // Shutdown newly initiated
  }

  /// Records that a child actor (Listener) has stopped.
  /// Returns true if this was the last pending entity (actor or connection),
  /// triggering a potential move to Lingering.
  fn record_child_actor_stopped(&mut self, child_actor_handle: usize, core_handle: usize) -> bool {
    if self.state == ShutdownPhase::Finished {
      return false;
    }

    if self
      .pending_child_actors
      .remove(&child_actor_handle)
      .is_some()
    {
      tracing::debug!(
        handle = core_handle,
        child_id = child_actor_handle,
        "Tracked child actor stopped."
      );
      return self.pending_child_actors.is_empty() && self.pending_connections_to_close.is_empty();
    }
    false
  }

  /// Records that an active connection (Session/UringFd) has been closed/stopped.
  /// Returns true if this was the last pending entity (actor or connection),
  /// triggering a potential move to Lingering.
  fn record_connection_closed(&mut self, connection_id: usize, core_handle: usize) -> bool {
    if self.state == ShutdownPhase::Finished {
      return false;
    }

    if self
      .pending_connections_to_close
      .remove(&connection_id)
      .is_some()
    {
      tracing::debug!(
        handle = core_handle,
        conn_id = connection_id,
        "Tracked active connection closed/stopped."
      );
      return self.pending_connections_to_close.is_empty() && self.pending_child_actors.is_empty();
    }
    false
  }

  pub(crate) fn start_linger_if_needed(
    &mut self,
    linger_duration_option: Option<Duration>,
    core_handle: usize,
  ) {
    if self.state != ShutdownPhase::Lingering {
      tracing::warn!(handle=core_handle, current_phase = ?self.state, "Attempted to start LINGER in incorrect state.");
      return;
    }
    if self.linger_deadline.is_some() && linger_duration_option != Some(Duration::ZERO) {
      tracing::trace!(
        handle = core_handle,
        "Linger deadline already set or effectively active."
      );
      return;
    }
    match linger_duration_option {
      None => {
        self.linger_deadline = None;
        tracing::debug!(handle = core_handle, "Starting infinite LINGER.");
      }
      Some(d) if d.is_zero() => {
        self.linger_deadline = Some(Instant::now());
        tracing::debug!(handle = core_handle, "LINGER is zero.");
      }
      Some(d) => {
        self.linger_deadline = Some(Instant::now() + d);
        tracing::debug!(handle = core_handle, ?d, "Starting timed LINGER.");
      }
    }
  }

  pub(crate) fn is_linger_expired_or_queues_empty(
    &self,
    core_s_reader: &CoreState,
    core_handle: usize,
  ) -> bool {
    if self.state != ShutdownPhase::Lingering {
      return false;
    }
    let core_pipes_empty = core_s_reader
      .pipes_tx
      .values()
      .all(|sender| sender.is_empty());

    if core_pipes_empty {
      tracing::debug!(
        handle = core_handle,
        "Linger check: All SocketCore pipes_tx empty. Linger can complete."
      );
      return true;
    }
    if let Some(deadline) = self.linger_deadline {
      if Instant::now() >= deadline {
        tracing::debug!(
          handle = core_handle,
          "Linger deadline expired. Core pipes_tx empty: {}.",
          core_pipes_empty
        );
        return true;
      }
    }
    false
  }
}

// --- High-Level Shutdown Orchestration Functions ---

pub(crate) async fn publish_socket_closing_event(
  context: &crate::context::Context,
  socket_id: usize,
) {
  let event = SystemEvent::SocketClosing { socket_id };
  if context.event_bus().publish(event).is_err() {
    // Borrow error from SendError is not Clone.
    tracing::warn!(
      socket_handle = socket_id,
      "Failed to publish SocketClosing event for self."
    );
  } else {
    tracing::debug!(
      socket_handle = socket_id,
      "Published SocketClosing event for self."
    );
  }
}

pub(crate) async fn initiate_core_shutdown(
  core_arc: Arc<SocketCore>,
  socket_logic_strong: &Arc<dyn ISocket>,
  was_due_to_error: bool,
) {
  let core_handle = core_arc.handle;
  let mut coordinator = core_arc.shutdown_coordinator.lock().await;

  if coordinator.state != ShutdownPhase::Running {
    return;
  }
  
  tracing::info!(
    handle = core_handle,
    was_due_to_error,
    "Initiating SocketCore shutdown steps."
  );

  // Event publishing is now done by the caller (command_loop or event_processor)
  // publish_socket_closing_event(&core_arc.context, core_handle).await;

  {
    let core_s_reader = core_arc.core_state.read(); // Read lock for populating coordinator
    coordinator.begin_shutdown_sequence(core_handle, &core_s_reader);
  }

  let child_actors_to_stop = coordinator.pending_child_actors.clone();
  let connections_to_close = coordinator.pending_connections_to_close.clone();
  // `inproc_connections_to_cleanup` is used later by `perform_final_pipe_cleanup`.
  // No need to clone it here just for stopping, as inproc "connections" are stopped
  // by closing their ISocketConnection if they appear in `connections_to_close`.

  drop(coordinator); // Release coordinator lock before async calls

  stop_child_listener_actors(core_arc.clone(), child_actors_to_stop).await;
  close_active_connections(core_arc.clone(), connections_to_close).await;

  let mut coordinator = core_arc.shutdown_coordinator.lock().await; // Re-acquire lock
  if coordinator.pending_child_actors.is_empty()
    && coordinator.pending_connections_to_close.is_empty()
  {
    tracing::debug!(
      handle = core_handle,
      "No pending children/connections after initial stop signals. Moving to Lingering."
    );
    coordinator.state = ShutdownPhase::Lingering;
    let linger_opt = core_arc.core_state.read().options.linger;
    coordinator.start_linger_if_needed(linger_opt, core_handle);

    if coordinator.is_linger_expired_or_queues_empty(&core_arc.core_state.read(), core_handle) {
      {
        let mut core_s_write = core_arc.core_state.write();
        advance_to_cleaning_phase(&mut coordinator, core_handle, &mut core_s_write);
      }
      #[cfg(feature = "inproc")]
      let pipes_to_clean = coordinator.inproc_connections_to_cleanup.clone();
      #[cfg(not(feature = "inproc"))]
      let pipes_to_clean = Vec::new();
      drop(coordinator);
      perform_final_pipe_cleanup(core_arc.clone(), socket_logic_strong, pipes_to_clean).await;
    }
  } else {
    tracing::debug!(
      handle = core_handle,
      "State: StoppingChildren. Waiting for child actors and active connections to stop."
    );
    coordinator.state = ShutdownPhase::StoppingChildren;
  }
}

async fn stop_child_listener_actors(
  core_arc: Arc<SocketCore>,
  child_actors_to_stop: HashMap<usize, String>, // (handle_id, uri)
) {
  let core_handle = core_arc.handle;
  if child_actors_to_stop.is_empty() {
    return;
  }

  tracing::debug!(
    handle = core_handle,
    count = child_actors_to_stop.len(),
    "Stopping child Listener actors..."
  );
  let mut stop_futs = Vec::new();
  for (child_actor_handle_id, child_uri) in child_actors_to_stop.iter() {
    let mailbox_opt = core_arc
      .core_state
      .read()
      .endpoints
      .get(child_uri)
      .filter(|ei| ei.endpoint_type == EndpointType::Listener) // Ensure it's a listener
      .map(|ei| ei.mailbox.clone());

    if let Some(mailbox) = mailbox_opt {
      let child_id_clone = *child_actor_handle_id;
      stop_futs.push(async move {
        if mailbox.send(Command::Stop).await.is_err() {
          tracing::warn!(
            parent_handle = core_handle,
            child_handle = child_id_clone,
            "Failed to send Stop to Listener actor {}.",
            child_id_clone
          );
        }
      });
    } else {
      // This implies the child_actors_to_stop list from coordinator was stale or incorrect.
      tracing::warn!(parent_handle = core_handle, child_handle = *child_actor_handle_id, uri = %child_uri, "Could not find mailbox for Listener actor during shutdown. It may have already stopped.");
    }
  }
  if !stop_futs.is_empty() {
    futures::future::join_all(stop_futs).await;
  }
}

async fn close_active_connections(
  core_arc: Arc<SocketCore>,
  // connections_to_close is HashMap<connection_instance_id (EndpointInfo.handle_id), (uri, iface)>
  connections_to_close: HashMap<usize, (String, Arc<dyn ISocketConnection>)>,
) {
  let core_handle = core_arc.handle;
  if connections_to_close.is_empty() {
    return;
  }

  tracing::debug!(
    handle = core_handle,
    count = connections_to_close.len(),
    "Closing active connections via ISocketConnection..."
  );
  let mut close_futs = Vec::new();

  // Need to iterate carefully due to async and coordinator lock
  // Collect IDs to modify coordinator after futures.
  let mut inproc_connections_processed_ids = Vec::new();

  for (conn_id, (conn_uri, conn_iface)) in connections_to_close.iter() {
    let iface_clone = conn_iface.clone();
    let id_clone = *conn_id; // This is the EndpointInfo.handle_id
    let uri_clone = conn_uri.clone();

    close_futs.push(async move {
      let close_result = iface_clone.close_connection().await;
      if let Err(e) = close_result {
        tracing::warn!(parent_handle = core_handle, conn_id = id_clone, uri = %uri_clone, "Error from ISocketConnection.close_connection(): {}", e);
      }
      // Return the conn_id and uri if it was inproc, to process with coordinator later
      if uri_clone.starts_with("inproc://") {
        Some(id_clone)
      } else {
        None
      }
    });
  }

  let results = futures::future::join_all(close_futs).await;
  for res_opt in results {
    if let Some(inproc_conn_id) = res_opt {
      inproc_connections_processed_ids.push(inproc_conn_id);
    }
  }

  // Now, update the coordinator for all processed inproc connections
  if !inproc_connections_processed_ids.is_empty() {
    let mut coordinator = core_arc.shutdown_coordinator.lock().await;
    let mut made_progress_to_linger = false;
    for inproc_conn_id_val in inproc_connections_processed_ids {
      // If record_connection_closed returns true, it means this was the last pending entity
      // and the coordinator might have transitioned.
      if coordinator.record_connection_closed(inproc_conn_id_val, core_handle) {
        made_progress_to_linger = true;
      }
    }

    // If coordinator state advanced to Lingering due to these inproc connections closing
    if made_progress_to_linger && coordinator.state == ShutdownPhase::Lingering {
      tracing::debug!(
        handle = core_handle,
        "close_active_connections: Inproc connections closed, last pending. Advancing linger/cleaning."
      );
      let linger_opt_val = core_arc.core_state.read().options.linger;
      coordinator.start_linger_if_needed(linger_opt_val, core_handle); // Call the method on coordinator
                                                                       // The check for linger expiry and advancing to cleaning will happen in the main command_loop's linger_check_interval.
                                                                       // Or, we can replicate that check here if we want to be more proactive.
                                                                       // For now, let linger_check_interval handle the next step.
    }
  }
}

pub(crate) async fn handle_actor_stopping_event(
  core_arc: Arc<SocketCore>,
  socket_logic_strong: &Arc<dyn ISocket>,
  stopped_actor_id: usize,
  stopped_actor_type: ActorType,
  endpoint_uri_opt: Option<&str>,
  error_opt: Option<&ZmqError>,
) {
  let core_handle = core_arc.handle;
  
  // First, perform the resource cleanup regardless of the shutdown phase.
  // This removes the endpoint from the main map.
  // This function returns true if the cleanup might warrant a reconnect.
  let should_consider_reconnect = pipe_manager::cleanup_stopped_child_resources(
      core_arc.clone(),
      socket_logic_strong,
      stopped_actor_id,
      stopped_actor_type,
      endpoint_uri_opt,
      error_opt,
      false, // Assume not a full shutdown initially, we check phase below.
  ).await;

  // Now, acquire the coordinator lock to update the shutdown state.
  let mut coordinator = core_arc.shutdown_coordinator.lock().await;

  // We no longer need to check if the state is Running at the top. We handle all cases.
  match coordinator.state {
    ShutdownPhase::Running => {
      // Child stopped unexpectedly.
      tracing::warn!(
          handle = core_handle,
          child_id = stopped_actor_id,
          ?stopped_actor_type,
          uri = ?endpoint_uri_opt,
          "Child stopped unexpectedly while Core Running."
      );
      // Only reconnect if the cleanup indicated it was an outbound session that failed.
      if should_consider_reconnect {
        if let Some(uri_str) = endpoint_uri_opt {
          let target_uri = uri_str.to_string();

          // Calculate delay and update state
          let mut state = core_arc.core_state.write();
          let options = state.options.clone();
          
          // Default to 100ms if not set, consistent with ZMQ defaults
          let base = options.reconnect_ivl.unwrap_or(std::time::Duration::from_millis(100));
          let max = options.reconnect_ivl_max.unwrap_or(std::time::Duration::from_secs(60));

          let recon_state = state.reconnect_states.entry(target_uri.clone()).or_default();
          let delay = recon_state.on_connection_failure(base, max);

          tracing::info!(
            handle = core_handle,
            uri = %target_uri,
            attempt = recon_state.current_attempts,
            next_attempt_in = ?delay,
            "Session stopped. Scheduled for reconnect via passive backoff."
          );
          
          // Note: We do NOT spawn a task here. The main command_loop will pick this up.
        }
      }
    }
    ShutdownPhase::StoppingChildren => {
      // This is the expected path during a normal shutdown.
      let mut was_last_pending = false;
      match stopped_actor_type {
        ActorType::Listener => {
          if coordinator.record_child_actor_stopped(stopped_actor_id, core_handle) {
            was_last_pending = true;
          }
        }
        ActorType::Session => {
          if coordinator.record_connection_closed(stopped_actor_id, core_handle) {
            was_last_pending = true;
          }
        }
        _ => { /* Other types aren't tracked by the coordinator's lists. */ }
      }

      // If this was the last pending entity, advance the state machine.
      if was_last_pending {
        tracing::debug!(
          handle = core_handle,
          "All children/connections now stopped. Moving to Lingering."
        );
        coordinator.state = ShutdownPhase::Lingering;
        let linger_opt = core_arc.core_state.read().options.linger;
        coordinator.start_linger_if_needed(linger_opt, core_handle);
        // The main loop's linger check will handle the rest.
      }
    }
    // If the event arrives while Lingering or later, it's a late arrival.
    // The resource cleanup was still important, but we don't need to touch the counter.
    ShutdownPhase::Lingering | ShutdownPhase::CleaningPipes | ShutdownPhase::Finished => {
      tracing::debug!(
          handle = core_handle,
          child_id = stopped_actor_id,
          "Received late ActorStopping event during phase {:?}. Cleanup already done.",
          coordinator.state
      );
    }
  }
}

pub(crate) async fn check_and_advance_linger(
  core_arc: Arc<SocketCore>,
  socket_logic_strong: &Arc<dyn ISocket>,
) -> Result<(), ZmqError> {
  let core_handle = core_arc.handle;
  let mut coordinator = core_arc.shutdown_coordinator.lock().await;

  if coordinator.state != ShutdownPhase::Lingering {
    return Ok(());
  }

  if coordinator.linger_deadline.is_none()
    && core_arc.core_state.read().options.linger != Some(Duration::ZERO)
  {
    let linger_opt = core_arc.core_state.read().options.linger;
    coordinator.start_linger_if_needed(linger_opt, core_handle);
  }

  if coordinator.is_linger_expired_or_queues_empty(&core_arc.core_state.read(), core_handle) {
    tracing::debug!(
      handle = core_handle,
      "Linger complete/queues empty. Advancing to CleaningPipes."
    );
    {
      let mut core_s_write = core_arc.core_state.write();
      advance_to_cleaning_phase(&mut coordinator, core_handle, &mut core_s_write);
    }
    #[cfg(feature = "inproc")]
    let pipes_to_clean = coordinator.inproc_connections_to_cleanup.clone();
    #[cfg(not(feature = "inproc"))]
    let pipes_to_clean = Vec::new();
    drop(coordinator);
    perform_final_pipe_cleanup(core_arc.clone(), socket_logic_strong, pipes_to_clean).await;
  }
  Ok(())
}

pub(crate) fn advance_to_cleaning_phase(
  coordinator: &mut ShutdownCoordinator,
  core_handle: usize,
  _core_s_write: &mut CoreState, // Mutable borrow, though not directly used for modification here
) {
  if coordinator.state == ShutdownPhase::Lingering {
    tracing::debug!(handle = core_handle, "Advancing to CleaningPipes state.");
    coordinator.state = ShutdownPhase::CleaningPipes;
  }
}

pub(crate) async fn perform_final_pipe_cleanup(
  core_arc: Arc<SocketCore>,
  socket_logic_strong: &Arc<dyn ISocket>,
  #[cfg(feature = "inproc")] mut inproc_pipes_to_cleanup: Vec<(usize, usize, String)>,
  #[cfg(not(feature = "inproc"))] _inproc_pipes_to_cleanup: Vec<(usize, usize, String)>,
) {
  let core_handle = core_arc.handle;
  tracing::info!(
    handle = core_handle,
    "SocketCore performing final pipe and resource cleanup."
  );

  let (mut pipes_tx_map, mut reader_tasks_map) = {
    let mut core_s = core_arc.core_state.write();
    (
      std::mem::take(&mut core_s.pipes_tx),
      std::mem::take(&mut core_s.pipe_reader_task_handles),
    )
  };

  let _ = pipes_tx_map.drain();

  for (id, handle) in reader_tasks_map.drain() {
    handle.abort();
    tracing::trace!(handle = core_handle, pipe_id = id, "Aborted pipe reader.");
  }

  #[cfg(feature = "inproc")]
  {
    let mut detach_futs = Vec::new();
    for (_write_id, read_id, ref uri) in inproc_pipes_to_cleanup.drain(..) {
      tracing::debug!(handle=core_handle, pipe_read_id=read_id, %uri, "Notifying ISocket of inproc pipe detach.");
      let sl_clone = socket_logic_strong.clone();
      let r_id_clone = read_id;
      detach_futs.push(async move {
        sl_clone.pipe_detached(r_id_clone).await;
      });
    }
    if !detach_futs.is_empty() {
      futures::future::join_all(detach_futs).await;
    }
  }

  {
    core_arc
      .core_state
      .write()
      .pipe_read_id_to_endpoint_uri
      .clear();
    #[cfg(feature = "io-uring")]
    {
      for fd_to_unreg in core_arc.core_state.read().uring_fd_to_endpoint_uri.keys() {
        crate::uring::global_state::unregister_uring_fd_socket_core_mailbox(*fd_to_unreg);
      }
      core_arc.core_state.write().uring_fd_to_endpoint_uri.clear();
    }
    if !core_arc.core_state.read().endpoints.is_empty() {
      tracing::warn!(
        handle = core_handle,
        "Endpoints map not empty. Forcing clear. Rem: {}",
        core_arc.core_state.read().endpoints.len()
      );
      // Before clearing, ensure any remaining ISocketConnections are closed (best effort)
      let endpoints_to_force_close: Vec<Arc<dyn ISocketConnection>> = core_arc
        .core_state
        .read()
        .endpoints
        .values()
        .map(|ei| ei.connection_iface.clone())
        .collect();

      for iface in endpoints_to_force_close {
        let _ = iface.close_connection().await;
      }
      core_arc.core_state.write().endpoints.clear(); // Re-acquire and clear
    }
  }

  let mut coordinator = core_arc.shutdown_coordinator.lock().await;
  coordinator.state = ShutdownPhase::Finished;
  tracing::info!(
    handle = core_handle,
    "SocketCore final cleanup complete. Shutdown finished."
  );
}