From 35f4fd978d3e5f48f5031c83bd23718dcf9dd739 Mon Sep 17 00:00:00 2001 From: Owen Lin Date: Mon, 3 Nov 2025 07:44:22 -0800 Subject: [PATCH] fix thread/archive --- .../app-server/src/codex_message_processor.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/codex-rs/app-server/src/codex_message_processor.rs b/codex-rs/app-server/src/codex_message_processor.rs index 4f80180fc6..b85dc5d92c 100644 --- a/codex-rs/app-server/src/codex_message_processor.rs +++ b/codex-rs/app-server/src/codex_message_processor.rs @@ -1202,11 +1202,20 @@ impl CodexMessageProcessor { let notify = Arc::new(tokio::sync::Notify::new()); let notify_clone = notify.clone(); let is_shutdown = tokio::spawn(async move { + // Create the notified future outside the loop to avoid losing notifications. + let notified = notify_clone.notified(); + tokio::pin!(notified); loop { select! { - _ = notify_clone.notified() => { break; } + _ = &mut notified => { break; } event = conversation_clone.next_event() => { - if let Ok(event) = event && matches!(event.msg, EventMsg::ShutdownComplete) { break; } + match event { + Ok(event) => { + if matches!(event.msg, EventMsg::ShutdownComplete) { break; } + } + // Break on errors to avoid tight loops when the agent loop has exited. + Err(_) => { break; } + } } } } @@ -1217,13 +1226,14 @@ impl CodexMessageProcessor { _ = is_shutdown => {} _ = tokio::time::sleep(Duration::from_secs(10)) => { warn!("conversation {conversation_id} shutdown timed out; proceeding with archive"); - notify.notify_one(); + // Wake any waiter; use notify_waiters to avoid missing the signal. + notify.notify_waiters(); } } } Err(err) => { error!("failed to submit Shutdown to conversation {conversation_id}: {err}"); - notify.notify_one(); + notify.notify_waiters(); } } }