Skip to content

Commit

Permalink
Merge #7
Browse files Browse the repository at this point in the history
7: Wait for all processes in the process group r=passcod a=malyn

This avoids zombie processes that would otherwise be created if the child process exits after spawning its own children, but without waiting for those children to exit. This change ensures that `wait` reaps all of those zombie processes, while still returning the exit code from the original process that was spawned (Command Group's direct child).

One note: I wasn't entirely sure if this should be in Command Group or in my application code, but the documentation of the [`AsyncGroupChild::wait`](https://docs.rs/command-group/1.0.8/command_group/struct.AsyncGroupChild.html#method.wait) function suggested that cleaning up all of the children in the process *group* might have been what was intended:

> Waits for the child group to exit completely, returning the status that the process leader exited with.

The Tokio code does not perform this reaping though, and I realize that Command Group is ultimately based on the Tokio API, so, as I said, not entirely sure if this is the right place for this logic.

Co-authored-by: Michael Alyn Miller <malyn@strangeGizmo.com>
  • Loading branch information
bors[bot] and malyn committed Nov 19, 2022
2 parents 675e38d + 13cb9e0 commit 3d0ebcd
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 17 deletions.
52 changes: 43 additions & 9 deletions src/stdlib/child/unix.rs
Expand Up @@ -67,15 +67,49 @@ impl ChildImp {
fn wait_imp(&mut self, flag: WaitPidFlag) -> Result<Option<ExitStatus>> {
let negpid = Pid::from_raw(-self.pgid.as_raw());

// we can't use the safe wrapper directly because it doesn't return the raw status, and we
// need it to convert to the std's ExitStatus.
let mut status: i32 = 0;
match unsafe { libc::waitpid(negpid.into(), &mut status as *mut libc::c_int, flag.bits()) }
{
0 => Ok(None),
res => Errno::result(res)
.map_err(Error::from)
.map(|_| Some(ExitStatus::from_raw(status))),
// Wait for processes in a loop until every process in this
// process group has exited (this ensures that we reap any
// zombies that may have been created if the parent exited after
// spawning children, but didn't wait for those children to
// exit).
let mut parent_exit_status: Option<ExitStatus> = None;
loop {
// we can't use the safe wrapper directly because it doesn't
// return the raw status, and we need it to convert to the
// std's ExitStatus.
let mut status: i32 = 0;
match unsafe {
libc::waitpid(negpid.into(), &mut status as *mut libc::c_int, flag.bits())
} {
0 => {
// Zero should only happen if WNOHANG was passed in,
// and means that no processes have yet to exit.
return Ok(None);
}
-1 => {
match Errno::last() {
Errno::ECHILD => {
// No more children to reap; this is a
// graceful exit.
return Ok(parent_exit_status);
}
errno => {
return Err(Error::from(errno));
}
}
}
pid => {
// *A* process exited. Was it the parent process
// that we started? If so, collect the exit signal,
// otherwise we reaped a zombie process and should
// continue in the loop.
if self.pgid.as_raw() == pid {
parent_exit_status = Some(ExitStatus::from_raw(status));
} else {
// Reaped a zombie child; keep looping.
}
}
};
}
}

Expand Down
49 changes: 41 additions & 8 deletions src/tokio/child/unix.rs
Expand Up @@ -70,14 +70,47 @@ impl ChildImp {
}

fn wait_imp(pgid: i32, flag: WaitPidFlag) -> Result<Option<ExitStatus>> {
// we can't use the safe wrapper directly because it doesn't return the raw status, and we
// need it to convert to the std's ExitStatus.
let mut status: i32 = 0;
match unsafe { libc::waitpid(-pgid, &mut status as *mut libc::c_int, flag.bits()) } {
0 => Ok(None),
res => Errno::result(res)
.map_err(Error::from)
.map(|_| Some(ExitStatus::from_raw(status))),
// Wait for processes in a loop until every process in this
// process group has exited (this ensures that we reap any
// zombies that may have been created if the parent exited after
// spawning children, but didn't wait for those children to
// exit).
let mut parent_exit_status: Option<ExitStatus> = None;
loop {
// we can't use the safe wrapper directly because it doesn't
// return the raw status, and we need it to convert to the
// std's ExitStatus.
let mut status: i32 = 0;
match unsafe { libc::waitpid(-pgid, &mut status as *mut libc::c_int, flag.bits()) } {
0 => {
// Zero should only happen if WNOHANG was passed in,
// and means that no processes have yet to exit.
return Ok(None);
}
-1 => {
match Errno::last() {
Errno::ECHILD => {
// No more children to reap; this is a
// graceful exit.
return Ok(parent_exit_status);
}
errno => {
return Err(Error::from(errno));
}
}
}
pid => {
// *A* process exited. Was it the parent process
// that we started? If so, collect the exit signal,
// otherwise we reaped a zombie process and should
// continue in the loop.
if pgid == pid {
parent_exit_status = Some(ExitStatus::from_raw(status));
} else {
// Reaped a zombie child; keep looping.
}
}
};
}
}

Expand Down

0 comments on commit 3d0ebcd

Please sign in to comment.