From d5a046c3d3f6f85ca10d9e76acc20daf2c6b8adb Mon Sep 17 00:00:00 2001 From: Homero Thompson Date: Fri, 23 Oct 2026 21:25:09 +0000 Subject: [PATCH] Agent chat: end the agent's process group where `kill` is busybox' (#4587) (#4588) * claim: #4587 * Agent chat: end the agent's process group with the system call (#4587) The group was signalled with a `kill` program: `kill -s TERM -- -`. That works with procps' `kill` and with BSD's, but busybox' `kill` takes no `--`: it prints its usage, exits 1 and signals nothing. The agent's helpers (the CLI an adapter wraps, the MCP servers it starts) then stay alive after the agent is ended. The group is signalled with the system call instead, so no `kill` program and no shell are needed and the behaviour is the same everywhere. `nix` (MIT) is already in the tree through zip, and only on Unix: the browser build does not take it. The two tests that cover this now pass where they failed before: `agent_chat::process::tests::dropping_the_process_ends_its_group` and `agent_chat::process::tests::stopping_the_agent_leaves_no_helpers_running`. --- Cargo.lock | 1 + Cargo.toml | 3 +++ crates/ui-egui/Cargo.toml | 4 ++++ crates/ui-egui/src/agent_chat/process.rs | 21 ++++++++++++++------- 4 files changed, 22 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2b93cc301..4408e979d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4410,6 +4410,7 @@ dependencies = [ "image", "log", "minisign-verify", + "nix 0.31.3", "pollster 0.4.0", "proc-macro2", "semver", diff --git a/Cargo.toml b/Cargo.toml index 51171adbd..b04001654 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -78,6 +78,9 @@ cadmpeg-registry = { version = "=0.6.1", default-features = false, features = [" # fonts (sketch text outlines; MIT OR Apache-2.0, pure Rust) ttf-parser = { version = "0.25", default-features = false, features = ["std"] } +# Ends a process group with the system call (MIT; already in the tree through zip). +nix = { version = "0.31", default-features = false, features = ["signal"] } + # images image = { version = "0.25", default-features = false, features = ["png", "hdr"] } png = "0.18.1" diff --git a/crates/ui-egui/Cargo.toml b/crates/ui-egui/Cargo.toml index ef706b104..c64daff3f 100644 --- a/crates/ui-egui/Cargo.toml +++ b/crates/ui-egui/Cargo.toml @@ -31,6 +31,10 @@ zip = { workspace = true } # Rasterises the icon SVGs (assets/icons/ui/design/svg) at the size they are drawn. tiny-skia = { workspace = true } +[target.'cfg(unix)'.dependencies] +# Ends an agent's process group with the system call (MIT; already in the tree through zip). +nix = { workspace = true } + [target.'cfg(windows)'.dependencies] # Updates: the folder the Windows installer recorded, so an install outside Program Files is still # updated by its installer (MIT; already in the tree as a build dependency). diff --git a/crates/ui-egui/src/agent_chat/process.rs b/crates/ui-egui/src/agent_chat/process.rs index fb0e75432..0aebf1e64 100644 --- a/crates/ui-egui/src/agent_chat/process.rs +++ b/crates/ui-egui/src/agent_chat/process.rs @@ -13,6 +13,11 @@ use std::process::{Child, Command, Stdio}; use std::sync::mpsc::{Receiver, Sender, channel}; use std::time::{Duration, Instant}; +#[cfg(unix)] +use nix::sys::signal::{Signal, killpg}; +#[cfg(unix)] +use nix::unistd::Pid; + /// Longest line read from the agent; a longer one is dropped. const MAX_LINE: usize = 8 << 20; /// Lines of the agent's stderr kept to explain a failed start. @@ -73,13 +78,15 @@ fn signal_group(pgid: u32, signal: &str) { if pgid <= 1 { return; } - let mut c = Command::new("kill"); - c.args(["-s", signal]); - // procps' kill reads `-123` as an option without `--`; BSD kill takes it as is. - if cfg!(target_os = "linux") { - c.arg("--"); - } - let _ = c.arg(format!("-{pgid}")).stdin(Stdio::null()).stdout(Stdio::null()).stderr(Stdio::null()).status(); + let Ok(pgid) = i32::try_from(pgid) else { return }; + let signal = match signal { + "KILL" => Signal::SIGKILL, + _ => Signal::SIGTERM, + }; + // The group is signalled with the system call, not with a `kill` program: busybox' `kill` + // takes no `--` and signals nothing, and the shell's builtin parses the group differently + // from one shell to the next (#4587). + let _ = killpg(Pid::from_raw(pgid), signal); } /// Windows: end the agent and every process it started.