The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.
TypeScript agentai-chatai-studioall-in-onecoding-agentdesktop-appfluximage-generationlinuxllmlocal-ailocal-llmopen-sourceprivacyself-hostedstable-diffusionuncensoreduncensored-aivideo-generationwindows
fix(agent): cancelling a background task left the real worker running
David1e2e481f2960
diff --git a/src-tauri/src/commands/bg_tasks.rs b/src-tauri/src/commands/bg_tasks.rsindex f220240f..8914e78a 100644--- a/src-tauri/src/commands/bg_tasks.rs+++ b/src-tauri/src/commands/bg_tasks.rs@@ -250,6 +250,9 @@ async fn shell_task_start_impl(args: &Value) -> CmdResult {}});+ // The pid has to be read BEFORE the wait — `child.wait()` reaps the+ // process and `id()` then returns None, leaving nothing to walk.+ let child_pid = child.id();let wait_result = tokio::select! {res = child.wait() => Ok(res),_ = cancel_rx => Err(()),@@ -258,6 +261,15 @@ async fn shell_task_start_impl(args: &Value) -> CmdResult {Ok(Ok(status)) => (status.code(), false),Ok(Err(_)) => (None, false),Err(_) => {+ // `child.kill()` sends SIGKILL to the SHELL only. This module+ // exists for `pnpm install` / `cargo build` — the commands with+ // the deepest process trees — so cancelling used to leave the+ // actual worker running detached, exactly the bug 743c310 fixed+ // for the foreground shell tool. The Windows job object does not+ // help here either: it fires when LU dies, not on a cancel.+ if let Some(pid) = child_pid {+ crate::commands::shell::kill_tree(pid);+ }let _ = child.kill().await;(None, true)}@@ -449,3 +461,60 @@ mod tests {assert!(pos2 < pos1, "newer task should appear first");}}++#[cfg(test)]+mod cancel_tests {+ use super::*;++ fn alive(pid: u32) -> bool {+ let out = std::process::Command::new("ps")+ .args(["-o", "state=", "-p", &pid.to_string()])+ .output();+ match out {+ Ok(o) => {+ let st = String::from_utf8_lossy(&o.stdout).trim().to_string();+ !st.is_empty() && !st.starts_with('Z')+ }+ Err(_) => false,+ }+ }++ /// A background task is started for the long builds — `pnpm install`,+ /// `cargo build` — so its process tree is deep by definition. Cancelling+ /// used to SIGKILL the shell alone and leave the real worker running+ /// detached, the same defect 743c310 fixed for the foreground shell tool.+ #[tokio::test]+ #[cfg_attr(target_os = "windows", ignore = "uses sh/ps")]+ async fn cancelling_takes_the_grandchild_with_it() {+ // The shell prints its grandchild's pid, then waits on it.+ let start = shell_task_start_impl(&json!({+ "command": "sleep 30 & echo $! ; wait",+ "shell": "sh",+ }))+ .await+ .expect("start");+ let id = start["id"].as_str().expect("id").to_string();++ // Wait for the pid line to land in the tail buffer.+ let mut grandchild = None;+ for _ in 0..50 {+ tokio::time::sleep(std::time::Duration::from_millis(100)).await;+ let st = shell_task_status_impl(&json!({ "id": id })).await.expect("status");+ let tail = st["output_tail"].as_str().unwrap_or("");+ if let Some(line) = tail.lines().find(|l| l.trim().parse::<u32>().is_ok()) {+ grandchild = line.trim().parse::<u32>().ok();+ break;+ }+ }+ let grandchild = grandchild.expect("grandchild never reported its pid");+ assert!(alive(grandchild), "grandchild was not running to begin with");++ shell_task_kill_impl(&json!({ "id": id })).await.expect("kill");+ tokio::time::sleep(std::time::Duration::from_millis(700)).await;++ assert!(+ !alive(grandchild),+ "cancel killed the shell but left the grandchild ({grandchild}) running",+ );+ }+}