A run that never unwound has its directory taken by the next one
Browse files at this commit267d45bd7b2e3e4e8662db24aa10bf4c0a6b8f72Parent: f6f8156- test
- a run that never unwound has its directory taken by the next one
1 changed file +62 -2
- crates/engine/src/scratch.rs +62 -2
crates/engine/src/scratch.rsmodified+62 -2
| @@ -7,7 +7,8 @@ | ||
| 7 | 7 | //! what cleans up, which is the whole point: unwinding runs it, so a *failing* |
| 8 | 8 | //! test leaves nothing behind either. The suites used to litter `/tmp` with a |
| 9 | 9 | //! fresh `ve_*` directory per run and never take one back; on this machine that |
| 10 | -//! reached 2907 of them. | |
| 10 | +//! reached 2907 of them. A test the kernel *aborts* never unwinds and so never | |
| 11 | +//! drops anything; [`reap_orphans`] takes those on the next run instead. | |
| 11 | 12 | //! |
| 12 | 13 | //! Nothing outside a test has any use for this. It lives in the library rather |
| 13 | 14 | //! than in a test file because the integration suites, the unit tests here and |
| @@ -35,6 +36,8 @@ pub struct Scratch { | ||
| 35 | 36 | /// handed. |
| 36 | 37 | fn fresh(name: &str) -> PathBuf { |
| 37 | 38 | static NTH: AtomicU64 = AtomicU64::new(0); |
| 39 | + static REAP: std::sync::Once = std::sync::Once::new(); | |
| 40 | + REAP.call_once(reap_orphans); | |
| 38 | 41 | let dir = std::env::temp_dir().join(format!( |
| 39 | 42 | "{name}_{}_{}", |
| 40 | 43 | std::process::id(), |
| @@ -45,6 +48,39 @@ fn fresh(name: &str) -> PathBuf { | ||
| 45 | 48 | std::fs::canonicalize(&dir).expect("canonical scratch dir") |
| 46 | 49 | } |
| 47 | 50 | |
| 51 | +/// The one leak `Drop` cannot reach: a test the kernel *aborts* -- the AV1 | |
| 52 | +/// hardware encoder does it whenever the driver's ring hangs -- never unwinds, | |
| 53 | +/// so its directory outlives it. The name carries the process id that made it, | |
| 54 | +/// so a later run can tell an orphan from a directory a live sibling suite is | |
| 55 | +/// still writing into, and takes only the orphans. Once per process, before the | |
| 56 | +/// first scratch directory of it: the suites are what fills `/tmp`, and they all | |
| 57 | +/// come through here. | |
| 58 | +fn reap_orphans() { | |
| 59 | + let Ok(entries) = std::fs::read_dir(std::env::temp_dir()) else { | |
| 60 | + return; | |
| 61 | + }; | |
| 62 | + if !Path::new("/proc").is_dir() { | |
| 63 | + return; // No way to ask whether the owner is still running: take nothing. | |
| 64 | + } | |
| 65 | + for entry in entries.flatten() { | |
| 66 | + let name = entry.file_name(); | |
| 67 | + let Some(name) = name.to_str() else { continue }; | |
| 68 | + // `ve_<what>_<pid>_<nth>`, and nothing else, ever. | |
| 69 | + let mut tail = name.rsplitn(3, '_'); | |
| 70 | + let (Some(nth), Some(pid), Some(head)) = (tail.next(), tail.next(), tail.next()) else { | |
| 71 | + continue; | |
| 72 | + }; | |
| 73 | + if !head.starts_with("ve_") || nth.parse::<u64>().is_err() { | |
| 74 | + continue; | |
| 75 | + } | |
| 76 | + let Ok(pid) = pid.parse::<u64>() else { continue }; | |
| 77 | + if Path::new("/proc").join(pid.to_string()).exists() { | |
| 78 | + continue; // Still running: its own `Drop` will do this. | |
| 79 | + } | |
| 80 | + let _ = std::fs::remove_dir_all(entry.path()); | |
| 81 | + } | |
| 82 | +} | |
| 83 | + | |
| 48 | 84 | impl Scratch { |
| 49 | 85 | /// A directory to litter: the value *is* the directory. |
| 50 | 86 | pub fn dir(name: &str) -> Self { |
| @@ -95,7 +131,31 @@ impl Drop for Scratch { | ||
| 95 | 131 | |
| 96 | 132 | #[cfg(test)] |
| 97 | 133 | mod tests { |
| 98 | - use super::Scratch; | |
| 134 | + use super::{Scratch, reap_orphans}; | |
| 135 | + | |
| 136 | + /// What an aborted test leaves goes on the next run's account: its | |
| 137 | + /// directory is taken, a live process's is not, and nothing that is not | |
| 138 | + /// ours is touched whatever it is named. | |
| 139 | + #[test] | |
| 140 | + fn the_next_run_takes_what_an_aborted_one_left() { | |
| 141 | + let tmp = std::env::temp_dir(); | |
| 142 | + // No process has this id: `pid_max` is an exclusive bound everywhere. | |
| 143 | + let orphan = tmp.join("ve_scratch_orphan_4294967295_0"); | |
| 144 | + let mine = tmp.join(format!("ve_scratch_live_{}_9999", std::process::id())); | |
| 145 | + let theirs = tmp.join("someone_elses_4294967295_0"); | |
| 146 | + for dir in [&orphan, &mine, &theirs] { | |
| 147 | + std::fs::create_dir_all(dir).expect("a directory to reap"); | |
| 148 | + } | |
| 149 | + | |
| 150 | + reap_orphans(); | |
| 151 | + | |
| 152 | + assert!(!orphan.exists(), "the aborted run's directory is gone"); | |
| 153 | + assert!(mine.is_dir(), "a running suite's own directory is left alone"); | |
| 154 | + assert!(theirs.is_dir(), "and so is everything that is not ours"); | |
| 155 | + for dir in [&mine, &theirs] { | |
| 156 | + std::fs::remove_dir_all(dir).expect("cleanup"); | |
| 157 | + } | |
| 158 | + } | |
| 99 | 159 | |
| 100 | 160 | /// The guard's whole job, both shapes: what it hands back is usable, and |
| 101 | 161 | /// what it made is gone once it is -- including when a test panicked, which |