| @@ -53,13 +53,14 @@ fn pidfile(name: &str) -> PathBuf { |
| 53 | 53 | dir.join("pid") |
| 54 | 54 | } |
| 55 | 55 | |
| 56 | | −/// Whether the process whose pid `file` holds is gone (not even a zombie) |
| 57 | | −/// within a second. |
| 56 | +/// Whether the process whose pid `file` holds has died within a second. |
| 57 | +/// A zombie counts as dead: once the fixture has exited, its orphans |
| 58 | +/// belong to PID 1, and in a CI container PID 1 may never reap them. |
| 58 | 59 | fn gone(file: &Path) -> bool { |
| 59 | 60 | let pid = std::fs::read_to_string(file).unwrap(); |
| 60 | | − let proc = PathBuf::from(format!("/proc/{}", pid.trim())); |
| 61 | + let stat = PathBuf::from(format!("/proc/{}/stat", pid.trim())); |
| 61 | 62 | let started = Instant::now(); |
| 62 | | − while proc.exists() { |
| 63 | + while alive(&stat) { |
| 63 | 64 | if started.elapsed() > Duration::from_secs(1) { |
| 64 | 65 | return false; |
| 65 | 66 | } |
| @@ -68,6 +69,19 @@ fn gone(file: &Path) -> bool { |
| 68 | 69 | true |
| 69 | 70 | } |
| 70 | 71 | |
| 72 | +/// Whether `/proc/<pid>/stat` names a process that still runs: present, |
| 73 | +/// and its state (after the command name's closing parenthesis) neither |
| 74 | +/// `Z` (zombie) nor `X` (dead). |
| 75 | +fn alive(stat: &Path) -> bool { |
| 76 | + let Ok(text) = std::fs::read_to_string(stat) else { |
| 77 | + return false; |
| 78 | + }; |
| 79 | + let state = text |
| 80 | + .rfind(')') |
| 81 | + .and_then(|end| text[end + 1..].trim_start().chars().next()); |
| 82 | + !matches!(state, None | Some('Z' | 'X')) |
| 83 | +} |
| 84 | + |
| 71 | 85 | #[test] |
| 72 | 86 | fn a_killed_actor_leaves_no_child_behind() { |
| 73 | 87 | let Some(exe) = fixture() else { return }; |