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>
This commit is contained in:
fre$h
2026-04-02 09:55:04 -04:00
committed by GitHub
parent dba12ba6e6
commit b39762a1d4
4 changed files with 105 additions and 2 deletions
+21
View File
@@ -3,6 +3,25 @@ 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;
}
@@ -34,5 +53,7 @@ pub fn configure_subprocess(command: &mut Command) {
// 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();
}