Fix false positive alerts from a run-pass test on Command.

Reported as a part of rust-lang/rust#19120

The logic of rust-lang/rust@74fb798a20 was
flawed because when a CI tool run the test parallely with other tasks,
they all belong to a single session family and the test may pick up
irrelevant zombie processes before they are reaped by the CI tool
depending on timing.

Also, panic! inside a loop over all children makes the logic simpler.

By not destructing the return values of Command::spawn() until
find_zombies() finishes, I believe we can conduct a slightly stricter
test.

Signed-off-by: NODA, Kai <nodakai@gmail.com>
This commit is contained in:
NODA, Kai 2014-12-07 07:05:13 +08:00
parent 358db12a89
commit 87424c6a32

View File

@ -8,8 +8,6 @@
// option. This file may not be copied, modified, or distributed // option. This file may not be copied, modified, or distributed
// except according to those terms. // except according to those terms.
// ignore-test
extern crate libc; extern crate libc;
use std::io::process::Command; use std::io::process::Command;
@ -18,51 +16,38 @@ use std::iter::IteratorExt;
use libc::funcs::posix88::unistd; use libc::funcs::posix88::unistd;
// "ps -A -o pid,sid,command" with GNU ps should output something like this: // The output from "ps -A -o pid,ppid,args" should look like this:
// PID SID COMMAND // PID PPID COMMAND
// 1 1 /sbin/init // 1 0 /sbin/init
// 2 0 [kthreadd] // 2 0 [kthreadd]
// 3 0 [ksoftirqd/0]
// ... // ...
// 12562 9237 ./spawn-failure // 6076 9064 /bin/zsh
// 12563 9237 [spawn-failure] <defunct>
// 12564 9237 [spawn-failure] <defunct>
// ... // ...
// 12592 9237 [spawn-failure] <defunct> // 7164 6076 ./spawn-failure
// 12593 9237 ps -A -o pid,sid,command // 7165 7164 [spawn-failure] <defunct>
// 12884 12884 /bin/zsh // 7166 7164 [spawn-failure] <defunct>
// 12922 12922 /bin/zsh // ...
// 7197 7164 [spawn-failure] <defunct>
// 7198 7164 ps -A -o pid,ppid,command
// ... // ...
#[cfg(unix)] #[cfg(unix)]
fn find_zombies() { fn find_zombies() {
// http://man.freebsd.org/ps(1) let my_pid = unsafe { unistd::getpid() };
// http://man7.org/linux/man-pages/man1/ps.1.html
#[cfg(not(target_os = "macos"))]
const FIELDS: &'static str = "pid,sid,command";
// https://developer.apple.com/library/mac/documentation/Darwin/ // http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ps.html
// Reference/ManPages/man1/ps.1.html let ps_cmd_output = Command::new("ps").args(&["-A", "-o", "pid,ppid,args"]).output().unwrap();
#[cfg(target_os = "macos")]
const FIELDS: &'static str = "pid,sess,command";
let my_sid = unsafe { unistd::getsid(0) };
let ps_cmd_output = Command::new("ps").args(&["-A", "-o", FIELDS]).output().unwrap();
let ps_output = String::from_utf8_lossy(ps_cmd_output.output.as_slice()); let ps_output = String::from_utf8_lossy(ps_cmd_output.output.as_slice());
let found = ps_output.split('\n').enumerate().any(|(line_no, line)| for (line_no, line) in ps_output.split('\n').enumerate() {
0 < line_no && 0 < line.len() && if 0 < line_no && 0 < line.len() &&
my_sid == from_str(line.split(' ').filter(|w| 0 < w.len()).nth(1) my_pid == from_str(line.split(' ').filter(|w| 0 < w.len()).nth(1)
.expect("1st column should be Session ID") .expect("1st column should be PPID")
).expect("Session ID string into integer") && ).expect("PPID string into integer") &&
line.contains("defunct") && { line.contains("defunct") {
println!("Zombie child {}", line); panic!("Zombie child {}", line);
true }
} }
);
assert!( ! found, "Found at least one zombie child");
} }
#[cfg(windows)] #[cfg(windows)]
@ -71,10 +56,13 @@ fn find_zombies() { }
fn main() { fn main() {
let too_long = format!("/NoSuchCommand{:0300}", 0u8); let too_long = format!("/NoSuchCommand{:0300}", 0u8);
for _ in range(0u32, 100) { let _failures = Vec::from_fn(100, |_i| {
let invalid = Command::new(too_long.as_slice()).spawn(); let cmd = Command::new(too_long.as_slice());
assert!(invalid.is_err()); let failed = cmd.spawn();
} assert!(failed.is_err(), "Make sure the command fails to spawn(): {}", cmd);
failed
});
find_zombies(); find_zombies();
// then _failures goes out of scope
} }