Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

fix(agent): keep awaited subagent cancellable via teardown (#254)

Akarshan Biswas7f921d5bb52d
diff --git a/src-tauri/src/core/agent/subagent.rs b/src-tauri/src/core/agent/subagent.rs
index de9b8fbce..07aaf9ed7 100644
--- a/src-tauri/src/core/agent/subagent.rs
+++ b/src-tauri/src/core/agent/subagent.rs
@@ -599,10 +599,7 @@ pub(crate) async fn await_subagent(
let rx = {
let mut guard = bg.inner.lock().unwrap();
match guard.get_mut(run_id).and_then(|e| e.result.take()) {
- Some(rx) => {
- guard.remove(run_id);
- rx
- }
+ Some(rx) => rx,
None => {
return Err(SubagentError::Upstream(format!(
"unknown or already-collected subagent run '{run_id}'"
@@ -610,7 +607,13 @@ pub(crate) async fn await_subagent(
}
}
};
- rx.await.unwrap_or(Err(SubagentError::Cancelled))
+ // Keep the entry (and its abort handle) in the registry while awaiting, so a
+ // parent cancellation mid-await can still reach this child via `abort_all`.
+ // Taking `result` above already makes a second await error out. Remove the
+ // now-spent entry once the await resolves (a no-op if teardown drained it).
+ let outcome = rx.await.unwrap_or(Err(SubagentError::Cancelled));
+ bg.inner.lock().unwrap().remove(run_id);
+ outcome
}
/// The model-callable subagent tools, handled by the loop's tool invoker ahead
@@ -1300,6 +1303,52 @@ mod tests {
assert!(!handle.is_finished() || handle.await.is_ok(), "child ran to completion");
}
+ #[tokio::test]
+ async fn awaited_child_stays_cancellable_via_teardown() {
+ // Regression for #254: await_subagent must not sever the abort handle from
+ // the registry, or a parent cancelled mid-await can no longer stop the
+ // child and its live event-sender clone hangs the run.
+ let bg = Arc::new(BackgroundSubagents::default());
+ let (tx, rx) = tokio::sync::oneshot::channel::<Result<String, SubagentError>>();
+ // The sender lives inside the task (as in `spawn_subagent`), so aborting the
+ // task drops it and the awaited receiver resolves to Cancelled.
+ let handle = tokio::spawn(async move {
+ let _tx = tx;
+ std::future::pending::<()>().await;
+ });
+ let (ev_tx, _ev_rx) = tokio::sync::mpsc::unbounded_channel();
+ bg.inner.lock().unwrap().insert(
+ "r1".to_string(),
+ BackgroundEntry {
+ result: Some(rx),
+ abort: handle.abort_handle(),
+ run_id: "r1".to_string(),
+ name: "reviewer".to_string(),
+ events: ev_tx,
+ },
+ );
+
+ let bg_await = bg.clone();
+ let awaiting = tokio::spawn(async move { await_subagent(&bg_await, "r1").await });
+ // Let the await take the receiver and park on it.
+ tokio::task::yield_now().await;
+ tokio::task::yield_now().await;
+
+ // The entry (with its abort handle) must still be reachable for teardown.
+ assert!(
+ bg.inner.lock().unwrap().contains_key("r1"),
+ "entry must remain registered while being awaited"
+ );
+
+ AbortOnDrop(bg.clone()); // constructs + drops -> abort_all
+ assert!(handle.await.unwrap_err().is_cancelled(), "child was aborted");
+ assert!(
+ matches!(awaiting.await.unwrap(), Err(SubagentError::Cancelled)),
+ "await resolves to Cancelled once the child is aborted"
+ );
+ assert!(bg.inner.lock().unwrap().is_empty(), "teardown drained the map");
+ }
+
#[test]
fn schemas_list_available_names_in_dispatch_description() {
let reg = registry_with("reviewer", None);