Files
tkmind_go/crates/goose/src/subprocess.rs
T
fre$h b39762a1d4 fix: clean up MCP subprocesses after abrupt parent exit (#8242)
Signed-off-by: fre <anonwurcod@proton.me>
Signed-off-by: Douwe Osinga <douwe@squareup.com>
Co-authored-by: Oz <oz-agent@warp.dev>
Co-authored-by: Douwe Osinga <douwe@squareup.com>
2026-04-02 13:55:04 +00:00

60 lines
1.5 KiB
Rust

use tokio::process::Command;
#[cfg(windows)]
const CREATE_NO_WINDOW_FLAG: u32 = 0x08000000;
#[cfg(target_os = "linux")]
fn configure_parent_death_signal(command: &mut Command) {
let parent_pid = unsafe { libc::getpid() };
unsafe {
command.pre_exec(move || {
if libc::prctl(libc::PR_SET_PDEATHSIG, libc::SIGTERM) != 0 {
return Err(std::io::Error::last_os_error());
}
if libc::getppid() != parent_pid {
return Err(std::io::Error::from_raw_os_error(libc::ESRCH));
}
Ok(())
});
}
}
pub trait SubprocessExt {
fn set_no_window(&mut self) -> &mut Self;
}
impl SubprocessExt for Command {
fn set_no_window(&mut self) -> &mut Self {
#[cfg(windows)]
{
self.creation_flags(CREATE_NO_WINDOW_FLAG);
}
self
}
}
impl SubprocessExt for std::process::Command {
fn set_no_window(&mut self) -> &mut Self {
#[cfg(windows)]
{
use std::os::windows::process::CommandExt;
self.creation_flags(CREATE_NO_WINDOW_FLAG);
}
self
}
}
#[allow(unused_variables)]
pub fn configure_subprocess(command: &mut Command) {
// Isolate subprocess into its own process group so it does not receive
// SIGINT when the user presses Ctrl+C in the terminal.
#[cfg(unix)]
command.process_group(0);
#[cfg(target_os = "linux")]
configure_parent_death_signal(command);
command.set_no_window();
}