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
use crate::error::ZmqError;
use crate::runtime::{ActorDropGuard, ActorType, MailboxReceiver, SystemEvent};
use crate::socket::core::state::ShutdownPhase;
use crate::socket::core::{command_processor, event_processor, shutdown, SocketCore};
use crate::socket::ISocket;
use crate::Command;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::broadcast;
use tokio::time::{interval, Interval};
/// The main actor loop for `SocketCore`.
/// It listens for user commands on its mailbox and system events on the event bus.
pub(crate) async fn run_command_loop(
core_arc: Arc<SocketCore>,
socket_logic_strong: Arc<dyn ISocket>,
command_receiver: MailboxReceiver,
mut system_event_rx: broadcast::Receiver<SystemEvent>,
) {
let core_handle = core_arc.handle;
let context_clone_for_stop = core_arc.context.clone(); // For publishing ActorStopping
let socket_type_for_log = core_arc.core_state.read().socket_type; // Get once for logging
tracing::info!(
handle = core_handle,
socket_type = ?socket_type_for_log,
"SocketCore actor main loop starting."
);
// Publish ActorStarted event after spawning
let mut actor_drop_guard = ActorDropGuard::new(
context_clone_for_stop.clone(),
core_handle,
ActorType::SocketCore,
None,
None,
);
// Linger check interval
let mut linger_check_interval: Interval = interval(Duration::from_millis(100)); // Adjusted interval
linger_check_interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
let mut final_error_for_actorstop: Option<ZmqError> = None;
// The main loop
// The Result<(), ZmqError> is conceptual for the loop's success/failure.
// If an unrecoverable error occurs, we'll set final_error_for_actorstop and break.
let _loop_result: Result<(), ()> = async { // Changed to Result<(), ()> for loop structure
loop {
let current_shutdown_phase = {
// Lock acquired and dropped quickly
core_arc.shutdown_coordinator.lock().await.state // Access state directly
};
if current_shutdown_phase == ShutdownPhase::Finished {
tracing::debug!(
handle = core_handle,
"SocketCore shutdown sequence fully finished. Breaking command loop."
);
break; // Exit the main loop
}
tokio::select! {
biased; // Prioritize shutdown-related events or commands
// --- 1. System Event Processing ---
// Only listen to system events if not fully finished shutting down.
event_result = system_event_rx.recv(), if current_shutdown_phase != ShutdownPhase::Finished => {
match event_result {
Ok(event) => {
// Delegate to event_processor module
if let Err(e) = event_processor::process_system_event(
core_arc.clone(),
&socket_logic_strong,
event,
).await {
tracing::error!(handle = core_handle, "Fatal error processing system event: {}. Initiating shutdown.", e);
final_error_for_actorstop = Some(e);
// Ensure shutdown is initiated if not already
let coord = core_arc.shutdown_coordinator.lock().await;
if coord.state == ShutdownPhase::Running {
drop(coord); // Release before async call
shutdown::initiate_core_shutdown(core_arc.clone(), &socket_logic_strong, true).await;
}
// Loop will break due to ShutdownPhase::Finished or next error check
}
}
Err(broadcast::error::RecvError::Lagged(n)) => {
tracing::warn!(handle = core_handle, skipped = n, "System event bus lagged for SocketCore!");
// Potentially treat as critical error and initiate shutdown if not already underway
if current_shutdown_phase == ShutdownPhase::Running {
final_error_for_actorstop = Some(ZmqError::Internal("SocketCore: Event bus lagged".into()));
shutdown::initiate_core_shutdown(core_arc.clone(), &socket_logic_strong, true).await;
}
}
Err(broadcast::error::RecvError::Closed) => {
tracing::error!(handle = core_handle, "System event bus closed unexpectedly!");
final_error_for_actorstop = Some(ZmqError::Internal("SocketCore: Event bus closed".into()));
if current_shutdown_phase == ShutdownPhase::Running {
shutdown::initiate_core_shutdown(core_arc.clone(), &socket_logic_strong, true).await;
}
// Loop will break due to ShutdownPhase::Finished or next error check
}
}
}
// --- 2. User Command Processing ---
// Only process commands if not fully finished.
cmd_result = command_receiver.recv(), if current_shutdown_phase != ShutdownPhase::Finished => {
match cmd_result {
Ok(command) => {
// Delegate to command_processor module
if let Err(e) = command_processor::process_socket_command(
core_arc.clone(),
&socket_logic_strong,
command,
).await {
tracing::error!(handle = core_handle, "Fatal error processing command: {}. Initiating shutdown.", e);
final_error_for_actorstop = Some(e);
let coord = core_arc.shutdown_coordinator.lock().await;
if coord.state == ShutdownPhase::Running {
drop(coord);
shutdown::initiate_core_shutdown(core_arc.clone(), &socket_logic_strong, true).await;
}
}
}
Err(_) => { // Command mailbox closed
tracing::info!(handle = core_handle, "SocketCore command mailbox closed. Initiating shutdown.");
final_error_for_actorstop = Some(ZmqError::Internal("SocketCore: Command mailbox closed".into()));
let coord = core_arc.shutdown_coordinator.lock().await;
if coord.state == ShutdownPhase::Running {
drop(coord);
shutdown::initiate_core_shutdown(core_arc.clone(), &socket_logic_strong, true).await;
}
// Loop will break due to ShutdownPhase::Finished or next error check
}
}
}
// --- 3. Linger Check (Timer) ---
// Only tick if in Lingering phase.
_ = linger_check_interval.tick(), if current_shutdown_phase == ShutdownPhase::Lingering => {
// Delegate to shutdown module
if let Err(e) = shutdown::check_and_advance_linger(
core_arc.clone(),
&socket_logic_strong,
).await {
tracing::error!(handle = core_handle, "Error during linger check: {}. Forcing cleanup.", e);
final_error_for_actorstop = final_error_for_actorstop.clone().or(Some(e));
// Force advance to cleaning phase if linger check itself errors badly
let mut coord = core_arc.shutdown_coordinator.lock().await;
if coord.state == ShutdownPhase::Lingering { // Re-check state
{
// Pass a mutable CoreState guard
let mut core_state_write_guard = core_arc.core_state.write();
shutdown::advance_to_cleaning_phase(&mut coord, core_handle, &mut core_state_write_guard);
}
// Drop guards before async call
#[cfg(feature = "inproc")]
let pipes_to_clean_val = coord.inproc_connections_to_cleanup.clone();
#[cfg(not(feature = "inproc"))]
let pipes_to_clean_val = Vec::new(); // Dummy for non-inproc
drop(coord);
shutdown::perform_final_pipe_cleanup(core_arc.clone(), &socket_logic_strong, pipes_to_clean_val).await;
} else {
drop(coord); // Still need to drop if not advancing
}
}
}
} // end tokio::select!
} // end loop
Ok(()) // Loop finished (normally implies shutdown complete)
}.await;
// --- Post-Loop Cleanup & ActorStopping Event ---
// This section runs after the main loop breaks (due to shutdown or fatal error).
tracing::debug!(
handle = core_handle,
"SocketCore loop exited. Performing final cleanup and ISocket::close()."
);
// 1. Close the public-facing API resources to unblock any waiting user tasks.
// This is the most critical part of the fix.
if let Err(e) = socket_logic_strong.process_command(Command::Stop).await {
tracing::error!(
handle = core_handle,
"Error during final ISocket::close(): {}",
e
);
if final_error_for_actorstop.is_none() {
final_error_for_actorstop = Some(e);
}
}
// 2. Drain any remaining commands that arrived after shutdown started.
// This prevents panics from senders whose receivers have been dropped.
// while let Some(cmd) = command_receiver.try_recv().ok() {
// // Log and drop the command, replying with an error if possible.
// tracing::warn!(handle = core_handle, cmd = %cmd.variant_name(), "Dropping command received during final shutdown.");
// // Best-effort attempt to notify the caller that the socket is closed.
// match cmd {
// Command::UserBind { reply_tx, .. } |
// Command::UserConnect { reply_tx, .. } |
// Command::UserDisconnect { reply_tx, .. } |
// Command::UserUnbind { reply_tx, .. } |
// Command::UserSetOpt { reply_tx, .. } |
// Command::UserMonitor { reply_tx, .. } |
// Command::UserClose { reply_tx, .. } => {
// let _ = reply_tx.send(Err(ZmqError::InvalidState("Socket closed")));
// },
// Command::UserGetOpt { reply_tx, .. } => {
// let _ = reply_tx.send(Err(ZmqError::InvalidState("Socket closed")));
// },
// Command::UserRecv { reply_tx, .. } => {
// let _ = reply_tx.send(Err(ZmqError::InvalidState("Socket closed")));
// },
// _ => {} // Other commands have no reply channel
// }
// }
// If loop exited due to an error that wasn't already part of a graceful shutdown,
// ensure shutdown is initiated and as much cleanup as possible happens.
// This is more of a failsafe.
let mut final_coord_guard = core_arc.shutdown_coordinator.lock().await;
if final_coord_guard.state != ShutdownPhase::Finished {
tracing::warn!(
handle = core_handle,
current_phase = ?final_coord_guard.state,
"SocketCore loop exited prematurely or shutdown not fully completed. Attempting final cleanup."
);
// Attempt to run the final cleanup stages if not already done.
// This is tricky because we are outside the select loop.
// For simplicity, we'll assume that if the loop broke, final_coord_guard.state
// should ideally be ShutdownPhase::Finished. If not, it might indicate an
// unhandled error path within the loop.
// A robust approach here might involve re-running parts of the shutdown sequence
// if they weren't completed, but that adds complexity.
// For now, we ensure we move to Finished state.
final_coord_guard.state = ShutdownPhase::Finished;
}
drop(final_coord_guard);
// Unregister this socket from the context (if it was registered)
// This needs to be done carefully if context itself is shutting down.
// ContextInner::unregister_socket should be robust.
core_arc.context.inner().unregister_socket(core_handle);
// Unregister any bound inproc names.
#[cfg(feature = "inproc")]
{
// Take the names to avoid holding lock during async operations
let bound_names_to_unregister = {
let mut core_s_guard = core_arc.core_state.write();
std::mem::take(&mut core_s_guard.bound_inproc_names)
};
if !bound_names_to_unregister.is_empty() {
tracing::debug!(
handle = core_handle,
"SocketCore unregistering inproc names: {:?}",
bound_names_to_unregister
);
for name_val in bound_names_to_unregister {
// ContextInner::unregister_inproc is synchronous.
core_arc.context.inner().unregister_inproc(&name_val);
}
}
}
// Publish the final ActorStopping event for this SocketCore.
if let Some(err) = final_error_for_actorstop {
actor_drop_guard.set_error(err);
} else {
actor_drop_guard.waive();
}
tracing::info!(
handle = core_handle,
socket_type = ?socket_type_for_log,
"SocketCore actor task FULLY STOPPED."
);
}