Skip to content

Commit

Permalink
Run waitpid on childs to ensure no zombies are left behind
Browse files Browse the repository at this point in the history
Fixes containers/crun#1482

Signed-off-by: Sascha Grunert <[email protected]>
  • Loading branch information
saschagrunert committed Sep 10, 2024
1 parent 06f131d commit 9e944a7
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion conmon-rs/server/src/child_reaper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use nix::{
errno::Errno,
sys::{
signal::{kill, Signal},
wait::{waitpid, WaitStatus},
wait::{waitpid, WaitPidFlag, WaitStatus},
},
unistd::{getpgid, Pid},
};
Expand Down Expand Up @@ -137,6 +137,9 @@ impl ChildReaper {
// meaning there is no other entity that could cancel the read_loops.
token.cancel();

// Wait to ensure that all children do not become zombies.
Self::check_child_processes();

bail!(err_str)
}

Expand All @@ -149,6 +152,34 @@ impl ChildReaper {
Ok((grandchild_pid, token))
}

fn check_child_processes() {
debug!("Checking child processes");
let pid = Pid::from_raw(-1);
loop {
match waitpid(pid, WaitPidFlag::WNOHANG.into()) {
Ok(WaitStatus::Exited(p, code)) => {
debug!("PID {p} exited with status: {code}");
break;
}
Ok(WaitStatus::StillAlive) => {
debug!("PID {pid} is still in same state");
break;
}
Ok(_) => {
continue;
}
Err(Errno::EINTR) => {
debug!("Retrying on EINTR for PID {pid}");
continue;
}
Err(err) => {
error!("Unable to waitpid on {:#}", err);
break;
}
};
}
}

pub fn watch_grandchild(
&self,
child: Child,
Expand Down

0 comments on commit 9e944a7

Please sign in to comment.