Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

Agent chat: end the agent's process group where `kill` is busybox' (#4587) (#4588)

Homero Thompsond5a046c3d3f6
diff --git a/Cargo.lock b/Cargo.lock
index 2b93cc3012..4408e979d7 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 51171adbd1..b04001654b 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 ef706b1041..c64daff3fa 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 fb0e754326..0aebf1e645 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.