Skip to content

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.

Terminate Windows shell trees without stale per-process snapshots

Davidc2a9eafdb110
diff --git a/src-tauri/src/commands/bg_tasks.rs b/src-tauri/src/commands/bg_tasks.rs
index 87a333ea..30102b52 100644
--- a/src-tauri/src/commands/bg_tasks.rs
+++ b/src-tauri/src/commands/bg_tasks.rs
@@ -745,6 +745,31 @@ mod tests {
let pos2 = ids.iter().position(|s| *s == id2).unwrap();
assert!(pos2 < pos1, "newer task should appear first");
}
+
+ #[cfg(windows)]
+ #[tokio::test]
+ async fn cancelling_during_shell_startup_drains_the_pipes() {
+ let _isolation = super::sweep_isolation().await;
+ // Exercise the startup window repeatedly, not just a settled tree.
+ // A missed ping child holds stdout open for30seconds with the old
+ // per-process snapshot kill, even after the shell itself is gone.
+ for _ in 0..3 {
+ let started = shell_task_start_impl(&json!({ "command": sleep_cmd_30s() })).await.unwrap();
+ let id = started["id"].as_str().unwrap();
+ tokio::time::sleep(std::time::Duration::from_millis(100)).await;
+ shell_task_kill_impl(&json!({ "id": id })).await.unwrap();
+ let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
+ loop {
+ let status = shell_task_status_impl(&json!({ "id": id })).await.unwrap();
+ if status["running"] == false {
+ assert_eq!(status["cancelled"], true);
+ break;
+ }
+ assert!(std::time::Instant::now() < deadline, "cancelled tree kept its output pipes open");
+ tokio::time::sleep(std::time::Duration::from_millis(50)).await;
+ }
+ }
+ }
}
#[cfg(test)]
diff --git a/src-tauri/src/commands/shell.rs b/src-tauri/src/commands/shell.rs
index e636fc9d..ccd9f31e 100644
--- a/src-tauri/src/commands/shell.rs
+++ b/src-tauri/src/commands/shell.rs
@@ -177,6 +177,7 @@ pub(crate) fn descendants(root: u32, sys: &sysinfo::System) -> Vec<u32> {
/// shell itself, so a timed-out `npm run dev`, build script or spawned server
/// kept running after the tool call gave up — still holding its port and CPU,
/// and still writing into a pipe nobody reads.
+#[cfg(not(windows))]
pub(crate) fn kill_tree(root: u32) {
use sysinfo::{Pid, ProcessesToUpdate, System};
let mut sys = System::new();
@@ -192,6 +193,37 @@ pub(crate) fn kill_tree(root: u32) {
}
}
+#[cfg(windows)]
+pub(crate) fn kill_tree(root: u32) {
+ if root == 0 { return; }
+ // sysinfo0.33 kills each snapshot member with a separate taskkill /PID.
+ // During shell startup a new child can appear between those calls and
+ // retain the output pipe after its parent dies. Ask Windows to end the
+ // owned tree in one operation, not a stale list of individual processes.
+ let mut command = Command::new("taskkill.exe");
+ command.args(["/PID", &root.to_string(), "/T", "/F"])
+ .stdin(Stdio::null()).stdout(Stdio::null()).stderr(Stdio::null());
+ crate::process_util::suppress_window(&mut command);
+ if let Ok(mut killer) = command.spawn() {
+ let deadline = std::time::Instant::now() + std::time::Duration::from_secs(2);
+ loop {
+ match killer.try_wait() {
+ Ok(Some(_)) => break,
+ Ok(None) if std::time::Instant::now() < deadline => {
+ std::thread::sleep(std::time::Duration::from_millis(10));
+ }
+ _ => {
+ // Only the helper is killed here. Never broaden the target
+ // to an image name or another process if termination fails.
+ let _ = killer.kill();
+ let _ = killer.wait();
+ break;
+ }
+ }
+ }
+ }
+}
+
/// Run a short-lived probe command with a HARD deadline and return its stdout.
///
/// `Command::output()` waits forever, and the tools this app probes with can