Record interrupted turns in managed daemon recovery snapshots (#45807)

## Why

Managed daemon recovery snapshots previously saved only loaded thread IDs, without identifying active turns or preserving their turn-specific options.

## What changed

- Capture regular, uncanceled turns after their input is recorded, saving the turn ID, output schema, service tier, and cyber access program alongside persisted thread IDs.
- Store interruption metadata atomically in the existing candidate array format so older servers can still read thread candidates.
- Begin snapshotting once admitted operations drain, while turns may still be running. Run snapshot collection and thread listener attachment independently of the event loop to keep forced shutdown responsive.

## Testing

Add coverage for running, completed, canceled, and compacting turns; recovery readiness for automatic and user turns; admitted resumes during shutdown; forced shutdown with a blocked rollout writer and child listener attachment; and legacy candidate-array compatibility.

GitOrigin-RevId: ed46342c3a5c71b09c48fa9acece2f15ae748e2f
This commit is contained in:
Eric Traut authored and copyberry committed 2026-09-15 23:31:44 +00:00
1 parent 7f83d4922d
commit 4d2807023a
12 files changed
+407 -38

No files matched your search

+28 -13
View File
@@ -984,6 +984,7 @@ pub async fn run_main_with_transport_options(
let mut active_admissions_rx = processor.turn_admission.subscribe_active();
let mut connections = HashMap::<ConnectionId, ConnectionState>::new();
let mut connection_cleanup_tasks = ConnectionCleanupTasks::new();
let mut thread_listener_tasks = tokio::task::JoinSet::new();
let mut remote_control_status_rx = remote_control_handle.status_receiver();
let mut remote_control_status = remote_control_status_rx.borrow().clone();
let transport_shutdown_token = transport_shutdown_token.clone();
@@ -1007,12 +1008,16 @@ pub async fn run_main_with_transport_options(
let mut listen_for_threads = true;
// Keep force signals and daemon control events responsive while saving.
let mut snapshot = Box::pin(async {
let loaded = processor.daemon_recovery_candidates().await;
if let Err(err) =
daemon_thread_recovery::snapshot(recovery_file.clone(), loaded).await
{
warn!("failed to save loaded threads during daemon shutdown: {err}");
}
let processor = Arc::clone(&processor);
let recovery_file = recovery_file.clone();
// Run independently: snapshot locks can require the event loop to make progress.
let task = tokio::spawn(async move {
let saved = processor.daemon_recovery_snapshot().await;
if let Err(err) = daemon_thread_recovery::snapshot(recovery_file, saved).await {
warn!("failed to save threads during daemon shutdown: {err}");
}
});
let _ = tokio_util::task::AbortOnDropHandle::new(task).await;
});
let mut snapshot_finished = !managed_daemon;
let mut clients_disconnected = false;
@@ -1049,7 +1054,7 @@ pub async fn run_main_with_transport_options(
}
tokio::select! {
_ = &mut snapshot, if ready_to_exit && !snapshot_finished => {
_ = &mut snapshot, if shutdown_state.requested() && active_admissions == 0 && !snapshot_finished => {
snapshot_finished = true;
}
shutdown_signal_result = shutdown_signal(), if graceful_signal_restart_enabled && !shutdown_state.forced() => {
@@ -1237,6 +1242,11 @@ pub async fn run_main_with_transport_options(
}
}
_ = connection_cleanup_tasks.reap_next() => {}
result = thread_listener_tasks.join_next(), if !thread_listener_tasks.is_empty() => {
if let Some(Err(err)) = result {
warn!("thread listener attachment failed: {err}");
}
}
changed = remote_control_status_rx.changed() => {
if changed.is_err() {
continue;
@@ -1260,12 +1270,16 @@ pub async fn run_main_with_transport_options(
initialized_connection_ids.push(*connection_id);
}
}
processor
.try_attach_thread_listener(
thread_id,
initialized_connection_ids,
)
.await;
let processor = Arc::clone(&processor);
// Attachment can wait on snapshot locks; keep force shutdown responsive.
thread_listener_tasks.spawn(async move {
processor
.try_attach_thread_listener(
thread_id,
initialized_connection_ids,
)
.await;
});
}
Err(tokio::sync::broadcast::error::RecvError::Lagged(_)) => {
// TODO(jif) handle lag.
@@ -1286,6 +1300,7 @@ pub async fn run_main_with_transport_options(
task.abort();
}
drop(snapshot);
drop(thread_listener_tasks);
if !shutdown_state.forced() {
futures::future::join_all(connections.iter().map(
|(&connection_id, connection_state)| {