* 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 -- -<pgid>`. 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`.
This commit is contained in:
Generated
+1
@@ -4410,6 +4410,7 @@ dependencies = [
|
||||
"image",
|
||||
"log",
|
||||
"minisign-verify",
|
||||
"nix 0.31.3",
|
||||
"pollster 0.4.0",
|
||||
"proc-macro2",
|
||||
"semver",
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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).
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user