diff --git a/src/servuo.rs b/src/servuo.rs index 4859e7e..0c76c0a 100644 --- a/src/servuo.rs +++ b/src/servuo.rs @@ -383,6 +383,16 @@ mod tests { use super::*; use crate::util::TempDir; + /// Held by every test that scans the real process table. The live-launch test's child names + /// `ServUO.exe`; caught mid-exit by a parallel scan (cmdline read, cwd already gone) it is an + /// `Unknown` match, and the empty-tree test would report it as a shard. + static PROCESS_TABLE: std::sync::Mutex<()> = std::sync::Mutex::new(()); + + fn process_table() -> std::sync::MutexGuard<'static, ()> { + // A failed test poisons the lock; the next one still needs it. + PROCESS_TABLE.lock().unwrap_or_else(|e| e.into_inner()) + } + fn fake_root(dir: &Path) { fs::create_dir_all(dir.join("Scripts")).unwrap(); fs::create_dir_all(dir.join("Config")).unwrap(); @@ -607,7 +617,9 @@ mod tests { /// installer#47 end to end on a real `/proc`: a process started *inside* the root naming a /// relative `ServUO.exe` — the shape of `cd root && mono ServUO.exe` — is found, under its own /// pid (a thread id would not equal `child.id()`, installer#45 item 5). `sh -c '…' ServUO.exe` - /// puts `ServUO.exe` in argv as `$0`; the trailing `:` stops the shell exec'ing into `sleep`. + /// puts `ServUO.exe` in argv as `$0`. The shell waits in the `read` builtin on a stdin pipe the + /// test holds open, so it never forks: a forked child is a copy of `sh` — same argv, same cwd — + /// until it execs, a genuine second match the scan could report first (release run 3309). #[cfg(target_os = "linux")] #[test] fn a_live_relative_launch_from_the_root_is_found_by_its_pid() { @@ -616,9 +628,11 @@ mod tests { fake_root(&root); let root = fs::canonicalize(&root).unwrap(); + let _table = process_table(); let mut child = std::process::Command::new("sh") - .args(["-c", "sleep 30; :", "ServUO.exe"]) + .args(["-c", "read x", "ServUO.exe"]) .current_dir(&root) + .stdin(std::process::Stdio::piped()) .spawn() .unwrap(); // The process is listed as soon as it exists, but give /proc a moment to fill cmdline. @@ -651,6 +665,7 @@ mod tests { let tmp = TempDir::new("rg-test-running").unwrap(); let root = tmp.path().join("srv"); fake_root(&root); + let _table = process_table(); assert!(find_running(&root).is_none()); } } diff --git a/src/util.rs b/src/util.rs index abc3ccc..3be2777 100644 --- a/src/util.rs +++ b/src/util.rs @@ -119,13 +119,22 @@ pub struct TempDir { } impl TempDir { + /// The name is pid + a per-process counter + the clock. The counter is what makes it unique: + /// two threads can read the same nanosecond (parallel tests did, and deleted each other's + /// files). `create_dir`, not `create_dir_all`, so a name that somehow exists is an error rather + /// than a directory silently shared with its owner. pub fn new(prefix: &str) -> Result { + use std::sync::atomic::{AtomicU64, Ordering}; + static NEXT: AtomicU64 = AtomicU64::new(0); + + let n = NEXT.fetch_add(1, Ordering::Relaxed); let nanos = SystemTime::now() .duration_since(UNIX_EPOCH) .map(|d| d.as_nanos()) .unwrap_or(0); - let path = std::env::temp_dir().join(format!("{prefix}-{}-{nanos}", std::process::id())); - fs::create_dir_all(&path) + let path = + std::env::temp_dir().join(format!("{prefix}-{}-{n}-{nanos}", std::process::id())); + fs::create_dir(&path) .with_context(|| format!("cannot create scratch directory {}", path.display()))?; Ok(Self { path }) } @@ -307,6 +316,22 @@ mod tests { assert!(!path.exists()); } + #[test] + fn temp_dirs_made_at_once_on_many_threads_never_share_a_path() { + // Parallel tests with one prefix used to land in one directory when they read the same + // nanosecond, and the first to finish deleted the other's files. + let dirs: Vec = std::thread::scope(|s| { + let handles: Vec<_> = (0..16) + .map(|_| s.spawn(|| TempDir::new("rg-test-unique").unwrap())) + .collect(); + handles.into_iter().map(|h| h.join().unwrap()).collect() + }); + let mut paths: Vec<_> = dirs.iter().map(|d| d.path().to_path_buf()).collect(); + paths.sort(); + paths.dedup(); + assert_eq!(paths.len(), dirs.len()); + } + #[test] fn a_failed_command_is_reported_with_what_it_printed() { // Both halves matter: the command to retype, and the reason it failed. stderr wins over