Skip to content

Commit

Permalink
Don't consider Zombies to be alive
Browse files Browse the repository at this point in the history
This fixes a regression that was introduced in #171

Closes #197

(cherry picked from commit 20b1182)
  • Loading branch information
eminence committed Aug 21, 2022
1 parent 75e11f9 commit f8356f0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/process/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -850,8 +850,14 @@ impl Process {
}

/// Is this process still alive?
///
/// Processes in the Zombie or Dead state are not considered alive.
pub fn is_alive(&self) -> bool {
rustix::fs::statat(&self.fd, "stat", AtFlags::empty()).is_ok()
if let Ok(stat) = self.stat() {
stat.state != 'Z' && stat.state != 'X'
} else {
false
}
}

/// What user owns this process?
Expand Down
19 changes: 19 additions & 0 deletions src/process/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,25 @@ fn test_smaps() {
fn test_proc_alive() {
let myself = Process::myself().unwrap();
assert!(myself.is_alive());

// zombies should not be considered alive
let mut command = std::process::Command::new("sleep");
command
.arg("0")
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null());
let mut child = command.spawn().unwrap();
let child_pid = child.id() as i32;

// sleep very briefly to allow the child to start and then exit
std::thread::sleep(std::time::Duration::from_millis(20));

let child_proc = Process::new(child_pid).unwrap();
assert!(!child_proc.is_alive(), "Child state is: {:?}", child_proc.stat());
assert!(child_proc.stat().unwrap().state().unwrap() == ProcState::Zombie);
child.wait().unwrap();
assert!(Process::new(child_pid).is_err());
assert!(!child_proc.is_alive(), "Child state is: {:?}", child_proc.stat());
}

#[test]
Expand Down

0 comments on commit f8356f0

Please sign in to comment.