Skip to content

Commit 84c6c24

Browse files
committed
Don't consider Zombies to be alive
This fixes a regression that was introduced in #171 Closes #197 (cherry picked from commit 20b1182)
1 parent ef81112 commit 84c6c24

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/process/mod.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -833,8 +833,14 @@ impl Process {
833833
}
834834

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

840846
/// What user owns this process?

src/process/tests.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,25 @@ fn test_smaps() {
204204
fn test_proc_alive() {
205205
let myself = Process::myself().unwrap();
206206
assert!(myself.is_alive());
207+
208+
// zombies should not be considered alive
209+
let mut command = std::process::Command::new("sleep");
210+
command
211+
.arg("0")
212+
.stdout(std::process::Stdio::null())
213+
.stderr(std::process::Stdio::null());
214+
let mut child = command.spawn().unwrap();
215+
let child_pid = child.id() as i32;
216+
217+
// sleep very briefly to allow the child to start and then exit
218+
std::thread::sleep(std::time::Duration::from_millis(20));
219+
220+
let child_proc = Process::new(child_pid).unwrap();
221+
assert!(!child_proc.is_alive(), "Child state is: {:?}", child_proc.stat());
222+
assert!(child_proc.stat().unwrap().state().unwrap() == ProcState::Zombie);
223+
child.wait().unwrap();
224+
assert!(Process::new(child_pid).is_err());
225+
assert!(!child_proc.is_alive(), "Child state is: {:?}", child_proc.stat());
207226
}
208227

209228
#[test]

0 commit comments

Comments
 (0)