diff --git a/packages/process-daemon/SPEC.md b/packages/process-daemon/SPEC.md index bb1d679a6b8d..665c9ce42f3f 100644 --- a/packages/process-daemon/SPEC.md +++ b/packages/process-daemon/SPEC.md @@ -181,10 +181,14 @@ starts a pipe process: any command without a terminal. Both return `"SIGTERM"`, when a signal ended the process (on Windows, when the daemon signalled it); `exit_code` is then null. `signal` is left out when there is none. -- On Unix an exited pipe process stays listed, as a zombie, until `terminate`. - The zombie keeps its PID and process group ID reserved, so killing the group - can never reach an unrelated process. On Windows the process object and job - handle stay open until `terminate`, so the PID is not reused either. +- An exited pipe process stays listed until `terminate`. On macOS the daemon + reaps the child on exit if it is the last member of its process group, and + rechecks on `list` and `get` if descendants outlive the child. On + Linux with group signalling through pidfds (6.9+), it reaps the child and + keeps a pidfd only if the group may still have members. Where the group + cannot safely be released or addressed by pidfd, the zombie reserves its + PID and group ID until `terminate`. On Windows the process object and job + handle stay open until `terminate`. `terminate` kills the whole tree (SIGKILL on Unix; the job on Windows), reaps it and removes the process; it never waits on a descendant that left the process group or job and still holds the pipe. The daemon never deletes diff --git a/packages/process-daemon/src/service.rs b/packages/process-daemon/src/service.rs index be54f6f84fee..51ce0dca8c3a 100644 --- a/packages/process-daemon/src/service.rs +++ b/packages/process-daemon/src/service.rs @@ -630,7 +630,7 @@ struct TerminalHandle { /// Dropped at shutdown, before the joins, to cancel a pipe reader that a /// surviving descendant would otherwise keep blocked. cancel: Mutex>>, - /// Runs after the joins; reaps a pipe process kept as a zombie until now. + /// Runs after the joins; releases any remaining pipe process resources. reap: Mutex>>, /// Pipe only: the child, for `signal`. process: Option>, @@ -894,7 +894,15 @@ impl TerminalHandle { if self.mode.is_pty() { self.request(|reply| ActorMessage::RefreshForegroundProcess { reply })?; } - Ok(self.info()) + let info = self.info(); + #[cfg(target_os = "macos")] + if matches!(info.lifecycle, TerminalLifecycle::Exited { .. }) + && let Some(process) = &self.process + { + // A descendant that still needed the group at child exit may now be gone. + process.release(); + } + Ok(info) } fn request(&self, make: impl FnOnce(Reply>) -> ActorMessage) -> Result { diff --git a/packages/process-daemon/src/service/pipe.rs b/packages/process-daemon/src/service/pipe.rs index f2a39135c580..131d11c7ab66 100644 --- a/packages/process-daemon/src/service/pipe.rs +++ b/packages/process-daemon/src/service/pipe.rs @@ -2,7 +2,7 @@ //! stdout and stderr sharing one pipe so their bytes keep their order. //! //! The platform modules own the child, its process tree and the output pipe: -//! Unix keeps a zombie so the PID and process group stay reserved, and Windows +//! Unix guards against signalling a reused process group, and Windows //! keeps the process and its kill-on-close job. Everything the two must do the //! same way lives here: //! @@ -157,8 +157,7 @@ struct Waiter { } /// Registers a spawned pipe process with its actor, then runs the platform's -/// reader and waiter. Both are started in that order, and the waiter last, so -/// it is joined before the process is reaped. +/// reader and waiter. Shutdown joins both before releasing the process. fn start_pipe( id: TerminalId, order: u64, diff --git a/packages/process-daemon/src/service/pipe/unix.rs b/packages/process-daemon/src/service/pipe/unix.rs index 5b7016257604..be1e7e64cece 100644 --- a/packages/process-daemon/src/service/pipe/unix.rs +++ b/packages/process-daemon/src/service/pipe/unix.rs @@ -3,10 +3,9 @@ //! The child runs in its own process group with stdin on `/dev/null`. Three //! choices keep the daemon safe from what the child leaves behind: //! -//! - The waiter observes the exit with `WNOWAIT` and leaves the child as a -//! zombie until shutdown. While the zombie exists, its PID and process group -//! ID cannot be reused, so killing the group can never hit an unrelated -//! process, even long after the exit. +//! - The waiter observes the exit with `WNOWAIT`. Before reaping, it retires +//! numeric signals or switches to an identity-bound group handle. A group +//! that still needs its numeric ID keeps the child reserved until shutdown. //! - The reader polls a cancel pipe as well as the output pipe, so shutdown //! never waits on a pipe that a descendant which left the process group //! still holds open. @@ -16,13 +15,15 @@ use std::fs::OpenOptions; use std::io::{self, PipeReader, PipeWriter, Read}; use std::os::fd::AsFd; +#[cfg(target_os = "linux")] +use std::os::fd::{AsRawFd, FromRawFd, OwnedFd}; use std::os::unix::fs::OpenOptionsExt; use std::os::unix::process::CommandExt; use std::process::{Command, Stdio}; use std::sync::{Arc, Mutex, PoisonError, RwLock}; use std::time::Instant; -use anyhow::{Context, Result, bail}; +use anyhow::{Context, Result}; use nix::errno::Errno; use nix::poll::{PollFd, PollFlags, PollTimeout, poll}; @@ -121,12 +122,11 @@ impl TerminalHandle { drop(command); let process = Arc::new(Process { pid: child.id(), - reaped: Mutex::new(false), + target: Mutex::new(Target::Child), }); // Dropping `Child` neither waits nor kills; `Process` reaps it. drop(child); - let pid = process.pid; start_pipe( id, order, @@ -135,7 +135,7 @@ impl TerminalHandle { Box::new(cancel_writer), move |actor| read_output(output, OutputSink::new(sink, actor), cancel, exited), move || super::Waiter { - exit: wait_exit(pid), + exit: wait_exit(process.pid).inspect(|_| process.release()), // Starts the reader's drain deadline, after the exit message. after: Box::new(exited_writer), }, @@ -170,11 +170,18 @@ fn reset_signals() -> io::Result<()> { Ok(()) } -/// A pipe child that stays a zombie after it exits, until `reap`. +#[derive(Debug)] +enum Target { + Child, + #[cfg(target_os = "linux")] + Group(OwnedFd), + Gone, +} + #[derive(Debug)] pub(in crate::service) struct Process { pid: u32, - reaped: Mutex, + target: Mutex, } impl Process { @@ -182,47 +189,96 @@ impl Process { self.pid } - /// Kills the whole process group. Never signals after `reap`, when the - /// group ID may belong to someone else. pub(super) fn kill(&self) { - let reaped = self.reaped.lock().unwrap_or_else(PoisonError::into_inner); - if !*reaped && let Ok(pid) = i32::try_from(self.pid) { - // SAFETY: the unreaped child keeps its PID and group ID reserved. - // Signal the child directly too, in case it moved to another group. - unsafe { - libc::killpg(pid, libc::SIGKILL); - libc::kill(pid, libc::SIGKILL); - } - } + let _ = self.signal(ProcessSignal::Kill); } /// Sends `signal` to the process group and the child. Allowed after the - /// exit, to reach what is left of the group; refused after `reap`. + /// exit, to reach what is left of the group. pub(in crate::service) fn signal(&self, signal: ProcessSignal) -> Result<()> { let signal = match signal { ProcessSignal::Term => libc::SIGTERM, ProcessSignal::Int => libc::SIGINT, ProcessSignal::Kill => libc::SIGKILL, }; - let reaped = self.reaped.lock().unwrap_or_else(PoisonError::into_inner); - if *reaped { - bail!("process has been reaped"); + let target = self.target.lock().unwrap_or_else(PoisonError::into_inner); + match &*target { + Target::Gone => return Ok(()), + #[cfg(target_os = "linux")] + Target::Group(fd) => { + return match signal_group(fd, signal) { + Err(Errno::ESRCH) | Ok(()) => Ok(()), + Err(error) => Err(error).context("failed to signal process group"), + }; + } + Target::Child => (), } let pid = i32::try_from(self.pid).context("invalid pid")?; // SAFETY: the unreaped child keeps its PID and group ID reserved. - check_signal(unsafe { libc::killpg(pid, signal) }, self.pid)?; + let group = check_signal(unsafe { libc::killpg(pid, signal) }, self.pid); // Signal the child directly too, in case it moved to another group. // SAFETY: as above. - check_signal(unsafe { libc::kill(pid, signal) }, self.pid) + group.and(check_signal(unsafe { libc::kill(pid, signal) }, self.pid)) + } + + /// Releases an exited child once numeric group signals are no longer needed. + pub(in crate::service) fn release(&self) { + #[cfg(any(target_os = "macos", target_os = "linux"))] + let mut target = self.target.lock().unwrap_or_else(PoisonError::into_inner); + #[cfg(target_os = "linux")] + { + // Linux 6.9+ can address the original group even after its leader + // has been reaped and the numeric PID reused. Probe before reaping. + // SAFETY: pidfd_open returns a new owned descriptor on success. + let fd = unsafe { libc::syscall(libc::SYS_pidfd_open, self.pid, 0) }; + if fd < 0 { + return; + } + // SAFETY: pidfd_open returned this fresh descriptor. + let fd = unsafe { OwnedFd::from_raw_fd(fd as i32) }; + if signal_group(&fd, 0).is_err() { + return; + } + self.waitpid(); + *target = if signal_group(&fd, 0) == Err(Errno::ESRCH) { + Target::Gone + } else { + Target::Group(fd) + }; + } + #[cfg(target_os = "macos")] + { + if !matches!(*target, Target::Child) { + return; + } + // libproc snapshots the group under the process-list lock. Two + // slots distinguish the zombie alone from any remaining members, + // including descendants that already closed their output pipes. + let mut members = [0i32; 2]; + // SAFETY: members has room for the specified number of bytes. + let count = unsafe { + libc::proc_listpgrppids( + self.pid as i32, + members.as_mut_ptr().cast(), + std::mem::size_of_val(&members) as i32, + ) + }; + if count == 1 && members[0] == self.pid as i32 { + self.waitpid(); + *target = Target::Gone; + } + } } - /// Call only after the waiter has returned: kill the group first, so the - /// child has exited and this does not block. pub(super) fn reap(&self) { - let mut reaped = self.reaped.lock().unwrap_or_else(PoisonError::into_inner); - if *reaped { - return; + let mut target = self.target.lock().unwrap_or_else(PoisonError::into_inner); + if matches!(*target, Target::Child) { + self.waitpid(); } + *target = Target::Gone; + } + + fn waitpid(&self) { let Ok(pid) = i32::try_from(self.pid) else { return; }; @@ -233,10 +289,25 @@ impl Process { break; } } - *reaped = true; } } +#[cfg(target_os = "linux")] +fn signal_group(fd: &OwnedFd, signal: i32) -> nix::Result<()> { + // SAFETY: the kernel takes the group identity from the pidfd; a null + // siginfo requests ordinary kill semantics. + Errno::result(unsafe { + libc::syscall( + libc::SYS_pidfd_send_signal, + fd.as_raw_fd(), + signal, + std::ptr::null::(), + libc::PIDFD_SIGNAL_PROCESS_GROUP, + ) + }) + .map(|_| ()) +} + /// Reads errno right after a `kill`/`killpg` result. A zombie group with no /// live members gives ESRCH on macOS, which is not an error here. /// Signalling an exited child whose group is empty fails with `ESRCH`, or diff --git a/packages/process-daemon/tests/pipe.rs b/packages/process-daemon/tests/pipe.rs index ee10d1ce1b82..d08315340839 100644 --- a/packages/process-daemon/tests/pipe.rs +++ b/packages/process-daemon/tests/pipe.rs @@ -218,16 +218,45 @@ fn terminate_does_not_wait_for_a_descendant_that_left_the_group() { } #[test] -fn an_exited_process_is_kept_until_terminate() { +fn finished_records_do_not_keep_unreaped_children() { + // Linux validates the signal scope before looking up the fd. Older kernels + // reject the scope and must keep the numeric group ID reserved instead. + #[cfg(target_os = "linux")] + let early_reap = unsafe { + libc::syscall( + libc::SYS_pidfd_send_signal, + -1, + 0, + std::ptr::null::(), + libc::PIDFD_SIGNAL_PROCESS_GROUP, + ); + nix::errno::Errno::last() == nix::errno::Errno::EBADF + }; + #[cfg(not(target_os = "linux"))] + let early_reap = cfg!(target_os = "macos"); let service = TerminalService::default(); - let (info, file) = start(&service, "exit 0"); - run(&service, info.id, &file); - let pid = info.pid.unwrap() as i32; - // The zombie keeps the PID and process group reserved. - assert!(alive(pid)); - assert_eq!(service.list(ProcessMode::Pipe).unwrap().len(), 1); - service.terminate(info.id).unwrap(); - assert!(!alive(pid)); + let mut finished = Vec::new(); + for _ in 0..8 { + let (info, file) = start(&service, "exit 7"); + assert_eq!(run(&service, info.id, &file).exit.exit_code, Some(7)); + finished.push(info); + } + for info in &finished { + assert_eq!(alive(info.pid.unwrap() as i32), !early_reap); + } + assert_eq!( + service.list(ProcessMode::Pipe).unwrap().len(), + finished.len() + ); + for info in finished { + let pid = info.pid.unwrap() as i32; + assert_eq!( + service.wait(info.id).unwrap().recv().unwrap().exit_code, + Some(7) + ); + service.terminate(info.id).unwrap(); + assert!(!alive(pid)); + } assert!(service.list(ProcessMode::Pipe).unwrap().is_empty()); }