Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions packages/process-daemon/SPEC.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions packages/process-daemon/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<Box<dyn Send>>>,
/// 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<Option<Box<dyn FnOnce() + Send>>>,
/// Pipe only: the child, for `signal`.
process: Option<Arc<pipe::Process>>,
Expand Down Expand Up @@ -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<T>(&self, make: impl FnOnce(Reply<Result<T>>) -> ActorMessage) -> Result<T> {
Expand Down
5 changes: 2 additions & 3 deletions packages/process-daemon/src/service/pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
//!
Expand Down Expand Up @@ -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,
Expand Down
137 changes: 104 additions & 33 deletions packages/process-daemon/src/service/pipe/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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};

Expand Down Expand Up @@ -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,
Expand All @@ -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),
},
Expand Down Expand Up @@ -170,59 +170,115 @@ 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<bool>,
target: Mutex<Target>,
}

impl Process {
pub(super) fn pid(&self) -> u32 {
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 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On kernels without PIDFD_SIGNAL_PROCESS_GROUP (before 6.9) both early returns keep the child as a zombie, so the per-Location pile-up stays there. That covers Ubuntu 24.04 LTS (6.8), Debian 12 (6.1) and RHEL 9 (5.14), and a commenter on #63817 hits it on 6.8. Could Linux fall back to the macOS approach when the pidfd path isn't available? That would mean reaping if no other process in /proc has this pgrp, and otherwise rechecking on get/list. It has the same window as proc_listpgrppids, and would leave only the cmd & case reserved on old kernels.

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;
};
Expand All @@ -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::siginfo_t>(),
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
Expand Down
47 changes: 38 additions & 9 deletions packages/process-daemon/tests/pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::siginfo_t>(),
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());
}

Expand Down
Loading