Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(agent_session): time out a stalled ACP handshake sooner (#5903)

Wolf Mermelstein9a0cdb17c544
diff --git a/crates/agent_session/src/domain/service.rs b/crates/agent_session/src/domain/service.rs
index 61c2abb465..58e623887e 100644
--- a/crates/agent_session/src/domain/service.rs
+++ b/crates/agent_session/src/domain/service.rs
@@ -61,6 +61,17 @@ use super::session::{CloseReason, Input};
const COMMAND_BUFFER: usize = 1028;
/// Persistence may delay lifecycle teardown, but never indefinitely.
const SESSION_PERSIST_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(60);
+/// How long a command may sit queued behind the ACP handshake
+/// (`Booting`/`Initializing`/`Opening`) before the caller gives up on it.
+/// The runtime never completes a queued command on its own until it reaches
+/// `Live` - see [`super::session::session::SessionMachine::on_command`] - so
+/// without this bound a stalled handshake (e.g. the runtime process never
+/// sends `AcpReady`, or never answers `initialize`/`session/new`) hangs the
+/// caller forever.
+#[cfg(not(test))]
+const HANDSHAKE_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(30);
+#[cfg(test)]
+const HANDSHAKE_TIMEOUT: std::time::Duration = std::time::Duration::from_millis(50);
struct ActiveSession {
commands: Option<mpsc::Sender<SessionCommand>>,
@@ -395,9 +406,29 @@ impl<R, Folds, Rt, Namer> AgentSessionServiceImpl<R, Folds, Rt, Namer> {
{
return Err(AgentSessionError::Disconnected(id));
}
- match result.await {
- Ok(result) => result,
- Err(_) => Err(AgentSessionError::Disconnected(id)),
+ // Not needed past this point, and holding it would be exactly the bug
+ // `begin_stop` exists to avoid: a live sender clone keeping the
+ // channel open no matter how many others get dropped, so the actor
+ // can only notice by hitting its own much longer internal deadline
+ // instead of promptly.
+ drop(commands);
+ match tokio::time::timeout(HANDSHAKE_TIMEOUT, result).await {
+ Ok(Ok(result)) => result,
+ Ok(Err(_)) => Err(AgentSessionError::Disconnected(id)),
+ Err(_elapsed) => {
+ // The actor is presumably still stuck in the handshake, so it
+ // is stopped directly - the same "drop the sender, the actor
+ // notices and tears itself down" mechanism `close_session`
+ // uses - rather than left to queue behind the same stall
+ // forever.
+ let (stopped, marker) = self.begin_stop(id, false);
+ Self::wait_stopped(stopped).await;
+ self.active.remove_if(&id, |_, active| {
+ Arc::ptr_eq(&active.marker, &marker) && !active.deleting
+ });
+ tracing::warn!(%id, "agent session command timed out waiting for the ACP handshake");
+ Err(AgentSessionError::Disconnected(id))
+ }
}
}
diff --git a/crates/agent_session/src/domain/service/test.rs b/crates/agent_session/src/domain/service/test.rs
index e98d85cb48..8443c8b275 100644
--- a/crates/agent_session/src/domain/service/test.rs
+++ b/crates/agent_session/src/domain/service/test.rs
@@ -568,6 +568,42 @@ async fn close_does_not_remove_a_concurrent_delete_guard() {
});
}
+/// A command sent while the handshake never completes cannot hang its caller
+/// forever - see [`HANDSHAKE_TIMEOUT`]. Without that bound, this test would
+/// simply never finish. The actor it was stuck in cannot linger afterwards
+/// either: its `commands` sender is gone from `active`, the same signal
+/// `close_session` relies on to mean the actor tore itself down.
+#[tokio::test]
+async fn a_command_stuck_behind_a_stalled_handshake_times_out_as_disconnected() {
+ let fx = fixture();
+ fx.service
+ .attach_session(fx.session, RuntimeAttachment::solo(PendingTransport))
+ .await
+ .expect("attach succeeds");
+
+ let result = fx
+ .service
+ .send_action(
+ fx.session,
+ None,
+ AgentAction::prompt("hello"),
+ AgentActionId::mint(),
+ )
+ .await;
+
+ assert!(
+ matches!(result, Err(AgentSessionError::Disconnected(id)) if id == fx.session),
+ "a stalled handshake times out rather than hanging forever, got {result:?}"
+ );
+ assert!(
+ fx.service
+ .active
+ .get(&fx.session)
+ .is_none_or(|active| active.commands.is_none()),
+ "the stuck actor's connector is released, not left running"
+ );
+}
+
#[tokio::test]
async fn cancellation_does_not_drop_an_effect_batch_after_machine_mutation() {
let repo = InMemoryAgentSessionRepo::new();