From 96400ac6be5f544d3b61f27142a17311e8d502fc Mon Sep 17 00:00:00 2001 From: Cameron Motevasselani Date: Tue, 13 Oct 2026 17:38:03 -0700 Subject: [PATCH] fix(supervisor): terminate an exec's process group when its channel closes When the gateway's exec deadline expires, or a caller cancels an exec stream, the gateway closes the SSH channel. The supervisor did nothing to the child, so the command kept running until it exited on its own or the pod went away. Each exec, shell, and sftp channel now runs its command as the leader of its own process group. If the channel or the SSH connection closes before the leader exits, the supervisor sends SIGTERM to the group, then SIGKILL after a 5s grace period if any member is still running. The leader stays unreaped until the escalation finishes, so a signal never reaches a recycled PID or process group. Canonical main-session attachments are unaffected. Signed-off-by: Cameron Motevasselani --- architecture/sandbox.md | 9 + .../src/exec_group.rs | 443 ++++++++++++++++++ .../openshell-supervisor-process/src/lib.rs | 2 + .../openshell-supervisor-process/src/ssh.rs | 60 ++- 4 files changed, 502 insertions(+), 12 deletions(-) create mode 100644 crates/openshell-supervisor-process/src/exec_group.rs diff --git a/architecture/sandbox.md b/architecture/sandbox.md index ba5b3c59b0..1d91b93221 100644 --- a/architecture/sandbox.md +++ b/architecture/sandbox.md @@ -458,6 +458,15 @@ sandbox workload directly. The relay supports: - Tar-based file sync. - Port forwarding where supported by the CLI/TUI surface. +Each exec, shell, or sftp channel runs its command as the leader of its own +process group. If the channel or SSH connection closes before that leader +exits, as it does when the gateway's exec deadline expires or the caller +cancels, the supervisor sends `SIGTERM` to the group and `SIGKILL` five seconds +later if any member is still running. The leader stays unreaped until then, so +the group ID cannot be recycled. Processes that start their own session or +process group are outside this cleanup. Canonical main-process attachments are +unaffected. + Sandbox logs are emitted locally and can also be pushed back to the gateway. Security-relevant sandbox behavior uses OCSF structured events; internal diagnostics use ordinary tracing. diff --git a/crates/openshell-supervisor-process/src/exec_group.rs b/crates/openshell-supervisor-process/src/exec_group.rs new file mode 100644 index 0000000000..469041dff0 --- /dev/null +++ b/crates/openshell-supervisor-process/src/exec_group.rs @@ -0,0 +1,443 @@ +// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +//! Process-group lifetime for SSH exec and shell channels. +//! +//! Each SSH channel that spawns its own child (pipe exec, PTY shell or exec, +//! sftp) places that child at the head of a new process group and tracks it +//! with an [`ExecProcessGroup`]. When the channel closes, or the SSH +//! connection drops, before the child has exited, the whole group receives +//! `SIGTERM` and, if any member is still running after +//! [`EXEC_TERMINATION_GRACE`], `SIGKILL`. A gateway exec deadline or a +//! cancelled exec stream closes the channel, so this is what actually stops +//! the command. +//! +//! The group is only ever signalled while its leader is known not to have +//! been reaped, so a signal can never land on a recycled PID: +//! +//! - The exit waiter observes the leader's exit with `waitid(WNOWAIT)`, which +//! leaves it a zombie, and only reaps it after recording the exit under the +//! same lock that signals are sent under. +//! - While termination is in progress the waiter defers reaping, so the +//! leader's PID, and with it the process group ID, stays reserved until the +//! final `SIGKILL` has been sent. That lets the escalation reach members +//! that ignored `SIGTERM` after the leader itself has exited. +//! +//! The leader is registered with `managed_children`, so the supervisor's +//! orphan reaper never reaps it out from under the waiter. + +use nix::sys::signal::{Signal, killpg}; +use nix::unistd::Pid; +use openshell_ocsf::{ + ActionId, ActivityId, DispositionId, Process as OcsfProcess, ProcessActivityBuilder, + SeverityId, StatusId, ocsf_emit, +}; +use rustix::process::{WaitId, WaitIdOptions, waitid}; +use std::process::{Child, ExitStatus}; +use std::sync::{Arc, Condvar, Mutex, MutexGuard}; +use std::time::{Duration, Instant}; + +/// How long a terminated exec's process group has to exit after `SIGTERM` +/// before it is sent `SIGKILL`. +pub const EXEC_TERMINATION_GRACE: Duration = Duration::from_secs(5); + +/// How often termination checks whether the process group has exited. +const GROUP_EXIT_POLL_INTERVAL: Duration = Duration::from_millis(50); + +/// A spawned SSH channel child that leads its own process group. +pub struct ExecProcessGroup { + pgid: Pid, + state: Mutex, + termination_done: Condvar, +} + +#[derive(Default)] +struct GroupState { + /// The leader has exited. It may still be an unreaped zombie. + leader_exited: bool, + /// The waiter is about to reap, or has reaped, the leader. Its PID may be + /// reused from here on, so the group must not be signalled again. + reaped: bool, + /// A termination is in progress and the leader must stay unreaped. + terminating: bool, +} + +impl ExecProcessGroup { + /// Track `child`, which must have been spawned as the leader of a new + /// process group (`setsid` or `process_group(0)`), so its PID is also + /// its process group ID. + #[must_use] + pub fn new(child: &Child) -> Arc { + let pid = i32::try_from(child.id()).unwrap_or(i32::MAX); + Arc::new(Self { + pgid: Pid::from_raw(pid), + state: Mutex::new(GroupState::default()), + termination_done: Condvar::new(), + }) + } + + /// Wait for the leader to exit, then reap it. + /// + /// Replaces `child.wait()` for a tracked child. If a termination is in + /// progress when the leader exits, reaping waits for it to finish. + /// + /// # Errors + /// + /// Returns an error if reaping the child fails. + pub fn wait(&self, child: &mut Child) -> std::io::Result { + self.wait_for_leader_exit(); + let mut state = self.lock_state(); + state.leader_exited = true; + while state.terminating { + state = self + .termination_done + .wait(state) + .unwrap_or_else(std::sync::PoisonError::into_inner); + } + state.reaped = true; + drop(state); + child.wait() + } + + /// Terminate the process group because its SSH channel went away. + /// + /// Sends `SIGTERM` now and `SIGKILL` from a background thread if any + /// member is still running after [`EXEC_TERMINATION_GRACE`]. Returns + /// `false` without signalling if the leader has already exited or a + /// termination is already in progress. Never blocks the caller. + pub fn terminate(self: &Arc, reason: &str) -> bool { + self.terminate_with_grace(reason, EXEC_TERMINATION_GRACE) + } + + fn terminate_with_grace(self: &Arc, reason: &str, grace: Duration) -> bool { + { + let mut state = self.lock_state(); + if state.leader_exited || state.terminating { + return false; + } + state.terminating = true; + ocsf_emit!( + ProcessActivityBuilder::new(openshell_ocsf::ctx::ctx()) + .activity(ActivityId::Close) + .action(ActionId::Denied) + .disposition(DispositionId::Blocked) + .severity(SeverityId::Low) + .status(StatusId::Success) + .process(OcsfProcess::new("ssh-exec", i64::from(self.pgid.as_raw()))) + .message(format!( + "SSH exec terminated: {reason} before process exited, sending SIGTERM to process group {}", + self.pgid + )) + .build() + ); + self.signal_group(&state, Signal::SIGTERM); + } + + let group = Arc::clone(self); + let escalation = std::thread::Builder::new() + .name("openshell-exec-kill".into()) + .spawn(move || group.escalate_after(grace)); + if escalation.is_err() { + // Without a thread to wait out the grace period, kill now rather + // than leave the group running or block the caller. + let mut state = self.lock_state(); + self.signal_group(&state, Signal::SIGKILL); + state.terminating = false; + self.termination_done.notify_all(); + } + true + } + + /// Wait up to `grace` for the group to exit, `SIGKILL` it if it has not, + /// then let the waiter reap the leader. + fn escalate_after(&self, grace: Duration) { + let deadline = Instant::now() + grace; + loop { + if !self.group_running() { + break; + } + if Instant::now() >= deadline { + let state = self.lock_state(); + ocsf_emit!( + ProcessActivityBuilder::new(openshell_ocsf::ctx::ctx()) + .activity(ActivityId::Close) + .action(ActionId::Denied) + .disposition(DispositionId::Blocked) + .severity(SeverityId::Medium) + .status(StatusId::Success) + .process(OcsfProcess::new("ssh-exec", i64::from(self.pgid.as_raw()))) + .message(format!( + "SSH exec process group {} still running {}s after SIGTERM, sending SIGKILL", + self.pgid, + grace.as_secs() + )) + .build() + ); + self.signal_group(&state, Signal::SIGKILL); + break; + } + std::thread::sleep(GROUP_EXIT_POLL_INTERVAL); + } + self.lock_state().terminating = false; + self.termination_done.notify_all(); + } + + /// Signal every member of the group. The caller holds the state lock, + /// which keeps the waiter from reaping the leader concurrently. + fn signal_group(&self, state: &GroupState, signal: Signal) { + if state.reaped { + return; + } + match killpg(self.pgid, signal) { + Ok(()) | Err(nix::errno::Errno::ESRCH) => {} + Err(error) => { + ocsf_emit!( + ProcessActivityBuilder::new(openshell_ocsf::ctx::ctx()) + .activity(ActivityId::Close) + .severity(SeverityId::Medium) + .status(StatusId::Failure) + .process(OcsfProcess::new("ssh-exec", i64::from(self.pgid.as_raw()))) + .message(format!( + "Failed to send {signal} to SSH exec process group {}: {error}", + self.pgid + )) + .build() + ); + } + } + } + + /// Whether any member of the group is still running (not a zombie). + fn group_running(&self) -> bool { + #[cfg(target_os = "linux")] + if let Some(running) = proc_group_has_running_member(self.pgid) { + return running; + } + // Without /proc the leader stands in for the whole group. + !self.lock_state().leader_exited + } + + /// Block until the leader exits, leaving it unreaped. + fn wait_for_leader_exit(&self) { + let Some(pid) = rustix::process::Pid::from_raw(self.pgid.as_raw()) else { + return; + }; + loop { + match waitid( + WaitId::Pid(pid), + WaitIdOptions::EXITED | WaitIdOptions::NOWAIT, + ) { + Err(rustix::io::Errno::INTR) => {} + // Exited, or no longer waitable (ECHILD); either way it is + // no longer running. + Ok(_) | Err(_) => return, + } + } + } + + fn lock_state(&self) -> MutexGuard<'_, GroupState> { + self.state + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner) + } +} + +/// Scan `/proc` for a non-zombie process in group `pgid`. Returns `None` if +/// `/proc` cannot be read. +#[cfg(target_os = "linux")] +fn proc_group_has_running_member(pgid: Pid) -> Option { + let entries = std::fs::read_dir("/proc").ok()?; + let group = pgid.as_raw().to_string(); + Some(entries.flatten().any(|entry| { + let name = entry.file_name(); + let Some(process) = name.to_str() else { + return false; + }; + if !process.bytes().all(|b| b.is_ascii_digit()) { + return false; + } + let Ok(stat) = std::fs::read_to_string(format!("/proc/{process}/stat")) else { + return false; + }; + // `pid (comm) state ppid pgrp ...`; comm may contain spaces and + // parentheses, so split after the last ')'. + let Some((_, fields)) = stat.rsplit_once(')') else { + return false; + }; + let mut fields = fields.split_whitespace(); + let state = fields.next(); + let pgrp = fields.nth(1); + pgrp == Some(group.as_str()) && !matches!(state, Some("Z" | "X")) + })) +} + +#[cfg(test)] +mod tests { + use super::*; + use std::io::{BufRead, BufReader}; + use std::os::unix::process::CommandExt; + use std::os::unix::process::ExitStatusExt; + use std::process::{Command, Stdio}; + use std::sync::mpsc; + + const TEST_GRACE: Duration = Duration::from_millis(500); + + /// Spawn `script` under `sh` as a process-group leader, the way + /// `spawn_pipe_exec` does, and return it with the PID it prints first. + fn spawn_group_leader(script: &str) -> (Child, i32) { + let mut child = Command::new("sh") + .arg("-c") + .arg(script) + .stdout(Stdio::piped()) + .process_group(0) + .spawn() + .expect("spawn sh"); + let mut line = String::new(); + BufReader::new(child.stdout.take().expect("stdout piped")) + .read_line(&mut line) + .expect("read grandchild pid"); + let grandchild = line.trim().parse().expect("grandchild pid"); + (child, grandchild) + } + + /// Run the exit waiter on its own thread, as the SSH spawn paths do. + fn spawn_waiter( + group: &Arc, + mut child: Child, + ) -> mpsc::Receiver> { + let (tx, rx) = mpsc::channel(); + let group = Arc::clone(group); + std::thread::spawn(move || { + let _ = tx.send(group.wait(&mut child)); + }); + rx + } + + /// Whether `pid` has exited. Orphaned grandchildren are reaped by init, + /// so a zombie counts as exited. + fn exited(pid: i32) -> bool { + #[cfg(target_os = "linux")] + if let Ok(stat) = std::fs::read_to_string(format!("/proc/{pid}/stat")) { + let state = stat.rsplit_once(')').map(|(_, f)| f.trim_start()); + return state.is_some_and(|s| s.starts_with('Z') || s.starts_with('X')); + } + nix::sys::signal::kill(Pid::from_raw(pid), None) == Err(nix::errno::Errno::ESRCH) + } + + fn wait_until_exited(pid: i32, within: Duration) -> bool { + let deadline = Instant::now() + within; + while Instant::now() < deadline { + if exited(pid) { + return true; + } + std::thread::sleep(Duration::from_millis(20)); + } + exited(pid) + } + + #[test] + fn terminate_stops_leader_and_grandchild_with_sigterm() { + let (child, grandchild) = spawn_group_leader("sleep 300 & echo $!; wait"); + let group = ExecProcessGroup::new(&child); + let leader = i32::try_from(child.id()).unwrap(); + let waiter = spawn_waiter(&group, child); + + let started = Instant::now(); + assert!(group.terminate_with_grace("test channel closed", Duration::from_secs(30))); + + let status = waiter + .recv_timeout(Duration::from_secs(10)) + .expect("leader still running after SIGTERM") + .expect("reap leader"); + assert_eq!(status.signal(), Some(libc::SIGTERM)); + assert!( + started.elapsed() < Duration::from_secs(10), + "SIGTERM should end the group well before the grace period" + ); + assert!(exited(leader)); + assert!( + wait_until_exited(grandchild, Duration::from_secs(5)), + "grandchild {grandchild} survived its group's SIGTERM" + ); + } + + #[test] + fn terminate_escalates_to_sigkill_when_leader_ignores_sigterm() { + let (child, grandchild) = spawn_group_leader("trap '' TERM; sleep 300 & echo $!; wait"); + let group = ExecProcessGroup::new(&child); + let waiter = spawn_waiter(&group, child); + + let started = Instant::now(); + assert!(group.terminate_with_grace("test channel closed", TEST_GRACE)); + + let status = waiter + .recv_timeout(Duration::from_secs(10)) + .expect("leader survived SIGKILL escalation") + .expect("reap leader"); + assert_eq!(status.signal(), Some(libc::SIGKILL)); + assert!(started.elapsed() >= TEST_GRACE, "SIGKILL sent before grace"); + assert!( + wait_until_exited(grandchild, Duration::from_secs(5)), + "grandchild {grandchild} survived SIGKILL escalation" + ); + } + + /// The leader honours SIGTERM but a grandchild ignores it. The leader + /// stays an unreaped zombie until escalation, so the group ID is still + /// reserved and SIGKILL reaches the grandchild. Needs `/proc` to see + /// that the group is still running after its leader exits. + #[cfg(target_os = "linux")] + #[test] + fn terminate_kills_grandchild_ignoring_sigterm_after_leader_exits() { + // The grandchild prints its PID only once its SIGTERM trap is set. + let (child, grandchild) = + spawn_group_leader("sh -c 'trap \"\" TERM; echo $$; exec sleep 300' & wait"); + let group = ExecProcessGroup::new(&child); + let waiter = spawn_waiter(&group, child); + + let started = Instant::now(); + assert!(group.terminate_with_grace("test channel closed", TEST_GRACE)); + + let status = waiter + .recv_timeout(Duration::from_secs(10)) + .expect("waiter never reaped the leader") + .expect("reap leader"); + assert_eq!(status.signal(), Some(libc::SIGTERM)); + assert!( + started.elapsed() >= TEST_GRACE, + "leader was reaped before escalation finished" + ); + assert!( + wait_until_exited(grandchild, Duration::from_secs(5)), + "grandchild {grandchild} ignoring SIGTERM was not killed" + ); + } + + #[test] + fn terminate_after_normal_exit_sends_no_signal() { + let (child, _) = spawn_group_leader("echo 0"); + let group = ExecProcessGroup::new(&child); + let status = spawn_waiter(&group, child) + .recv_timeout(Duration::from_secs(10)) + .expect("child did not exit") + .expect("reap child"); + assert!(status.success()); + + assert!(!group.terminate("test channel closed")); + assert!(!group.lock_state().terminating); + } + + #[test] + fn terminate_is_idempotent() { + let (child, _) = spawn_group_leader("sleep 300 & echo $!; wait"); + let group = ExecProcessGroup::new(&child); + let waiter = spawn_waiter(&group, child); + + assert!(group.terminate_with_grace("test channel closed", TEST_GRACE)); + assert!(!group.terminate("test connection closed")); + waiter + .recv_timeout(Duration::from_secs(10)) + .expect("leader still running") + .expect("reap leader"); + } +} diff --git a/crates/openshell-supervisor-process/src/lib.rs b/crates/openshell-supervisor-process/src/lib.rs index 692d40ed69..d5eb500c99 100644 --- a/crates/openshell-supervisor-process/src/lib.rs +++ b/crates/openshell-supervisor-process/src/lib.rs @@ -11,6 +11,8 @@ pub mod child_env; pub mod debug_rpc; #[cfg(unix)] +mod exec_group; +#[cfg(unix)] pub mod gateway_fleet; pub mod identity; pub mod log_push; diff --git a/crates/openshell-supervisor-process/src/ssh.rs b/crates/openshell-supervisor-process/src/ssh.rs index fbd6d9275b..254a518f0c 100644 --- a/crates/openshell-supervisor-process/src/ssh.rs +++ b/crates/openshell-supervisor-process/src/ssh.rs @@ -4,6 +4,7 @@ //! Embedded SSH server for sandbox access. use crate::child_env; +use crate::exec_group::ExecProcessGroup; use crate::main_session::{MainOutput, MainSession}; #[cfg(target_os = "linux")] use crate::managed_children; @@ -401,6 +402,19 @@ struct ChannelState { main_read_only: bool, main_detach_prefix_pending: bool, main_output_task: Option, + /// Process group of the child this channel spawned (exec, shell, or + /// sftp), terminated if the channel goes away before the child exits. + exec_group: Option>, +} + +impl ChannelState { + /// Terminate this channel's own child process group if it is still + /// running. Canonical main-session attachments never own one. + fn terminate_exec(&mut self, reason: &str) { + if let Some(group) = self.exec_group.take() { + group.terminate(reason); + } + } } const MAIN_DETACH_PREFIX: u8 = 0x10; // Ctrl-P @@ -470,6 +484,7 @@ struct SshHandler { impl Drop for SshHandler { fn drop(&mut self) { for state in self.channels.values_mut() { + state.terminate_exec("SSH connection closed"); if state.main_attached { self.main_session.end_terminal_attachment(); state.main_attached = false; @@ -544,13 +559,16 @@ impl russh::server::Handler for SshHandler { /// /// This is the final cleanup and subsumes `channel_eof` — if `channel_close` /// fires without a preceding `channel_eof`, all resources (`pty_master` File, - /// `input_sender`) are dropped here. + /// `input_sender`) are dropped here. A child the channel spawned that is + /// still running is terminated: the peer closing first means the caller + /// gave up on it (exec deadline or cancellation). async fn channel_close( &mut self, channel: ChannelId, _session: &mut Session, ) -> Result<(), Self::Error> { - if let Some(state) = self.channels.remove(&channel) { + if let Some(mut state) = self.channels.remove(&channel) { + state.terminate_exec("SSH channel closed"); if state.main_attached { self.main_session.end_terminal_attachment(); } @@ -846,7 +864,7 @@ impl russh::server::Handler for SshHandler { // which is exactly what spawn_pipe_exec wires up. This enables // modern scp (SFTP-based, OpenSSH 9.0+) and SFTP clients to // transfer files into and out of the sandbox. - let input_sender = spawn_pipe_exec( + let (input_sender, exec_group) = spawn_pipe_exec( &self.policy, &self.workspace, Some("/usr/lib/openssh/sftp-server".to_string()), @@ -865,6 +883,7 @@ impl russh::server::Handler for SshHandler { anyhow::anyhow!("subsystem_request on unknown channel {channel:?}") })?; state.input_sender = Some(InputSender::Process(input_sender)); + state.exec_group = Some(exec_group); } else { ocsf_emit!( SshActivityBuilder::new(openshell_ocsf::ctx::ctx()) @@ -1074,7 +1093,7 @@ impl SshHandler { if let Some(pty) = state.pty_request.take() { // PTY was requested — allocate a real PTY (interactive shell or // exec that explicitly asked for a terminal). - let (pty_master, input_sender) = spawn_pty_shell( + let (pty_master, input_sender, exec_group) = spawn_pty_shell( &self.policy, &self.workspace, command, @@ -1092,11 +1111,12 @@ impl SshHandler { )?; state.pty_master = Some(pty_master); state.input_sender = Some(InputSender::Process(input_sender)); + state.exec_group = Some(exec_group); } else { // No PTY requested — use plain pipes so stdout/stderr are // separate and output has clean LF line endings. This is the // path VSCode Remote-SSH exec commands take. - let input_sender = spawn_pipe_exec( + let (input_sender, exec_group) = spawn_pipe_exec( &self.policy, &self.workspace, command, @@ -1112,6 +1132,7 @@ impl SshHandler { self.enforcement_mode, )?; state.input_sender = Some(InputSender::Process(input_sender)); + state.exec_group = Some(exec_group); } Ok(()) } @@ -1285,7 +1306,7 @@ fn spawn_pty_shell( user_environment: &HashMap, resolved_identity: ResolvedProcessIdentity, enforcement_mode: ProcessEnforcementMode, -) -> anyhow::Result<(std::fs::File, mpsc::Sender>)> { +) -> anyhow::Result<(std::fs::File, mpsc::Sender>, Arc)> { let winsize = Winsize { ws_row: to_u16(pty.row_height.max(1)), ws_col: to_u16(pty.col_width.max(1)), @@ -1369,6 +1390,8 @@ fn spawn_pty_shell( let child_pid = child.id(); #[cfg(target_os = "linux")] managed_children::register(child_pid); + // The pre_exec hook's setsid made the child a process group leader. + let exec_group = ExecProcessGroup::new(&child); let master_file = master; let (sender, receiver) = mpsc::channel::>(); @@ -1411,8 +1434,9 @@ fn spawn_pty_shell( let handle_exit = handle; let runtime_exit = runtime; + let exit_group = Arc::clone(&exec_group); std::thread::spawn(move || { - let status = child.wait().ok(); + let status = exit_group.wait(&mut child).ok(); #[cfg(target_os = "linux")] managed_children::unregister(child_pid); let code = status.and_then(|s| s.code()).unwrap_or(1).unsigned_abs(); @@ -1431,7 +1455,7 @@ fn spawn_pty_shell( })); }); - Ok((master_file, sender)) + Ok((master_file, sender, exec_group)) } /// Spawn a command using plain pipes (no PTY). @@ -1454,7 +1478,7 @@ fn spawn_pipe_exec( user_environment: &HashMap, resolved_identity: ResolvedProcessIdentity, enforcement_mode: ProcessEnforcementMode, -) -> anyhow::Result>> { +) -> anyhow::Result<(mpsc::Sender>, Arc)> { // Resolve a shell present in the sandbox image; minimal images (e.g. Alpine) // don't ship bash, only `/bin/sh`. Runs in the supervisor, so it inspects // the sandbox filesystem. No command → read from stdin with no `-i`: @@ -1479,6 +1503,15 @@ fn spawn_pipe_exec( cmd.stdin(Stdio::piped()) .stdout(Stdio::piped()) .stderr(Stdio::piped()); + // Lead a new process group so closing the channel can signal the whole + // command tree (e.g. `bash -c 'pip install ...'`). std calls setpgid in + // the child before the pre_exec hook drops privileges, and spawn() + // returns only after exec, so the group exists before anything can + // signal it. + { + use std::os::unix::process::CommandExt; + cmd.process_group(0); + } if let Some(dir) = workspace.root() { cmd.current_dir(dir); @@ -1518,6 +1551,7 @@ fn spawn_pipe_exec( let child_pid = child.id(); #[cfg(target_os = "linux")] managed_children::register(child_pid); + let exec_group = ExecProcessGroup::new(&child); let child_stdin = child.stdin.take(); let child_stdout = child.stdout.take().expect("stdout must be piped"); @@ -1586,8 +1620,9 @@ fn spawn_pipe_exec( // Exit waiter thread let handle_exit = handle; let runtime_exit = runtime; + let exit_group = Arc::clone(&exec_group); std::thread::spawn(move || { - let status = child.wait().ok(); + let status = exit_group.wait(&mut child).ok(); #[cfg(target_os = "linux")] managed_children::unregister(child_pid); let code = status.and_then(|s| s.code()).unwrap_or(1).unsigned_abs(); @@ -1601,7 +1636,7 @@ fn spawn_pipe_exec( })); }); - Ok(sender) + Ok((sender, exec_group)) } mod unsafe_pty { @@ -1677,7 +1712,8 @@ mod unsafe_pty { /// Pre-exec hook for pipe-based (non-PTY) exec. /// - /// Skips `setsid` and `TIOCSCTTY` since there is no controlling terminal. + /// Skips `setsid` and `TIOCSCTTY` since there is no controlling terminal; + /// the caller places the child in its own process group instead. #[allow(unsafe_code)] #[cfg_attr( not(target_os = "linux"),