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:
@@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user