ec-av1: bound every piped test child (1800s, EC_AV1_CHILD_TIMEOUT_SECS) -- a wedged aomenc dies loud instead of hanging the suite
Browse files at this commit38b691941a1c5a3f3bc00d517af271ae301779baParent: 2b75a461 changed file +66 -4
- crates/ec-av1/src/probe.rs +66 -4
crates/ec-av1/src/probe.rsmodified+66 -4
| @@ -124,22 +124,84 @@ pub fn source( | |||
| 124 | /// carry the real diagnosis. | 124 | /// carry the real diagnosis. |
| 125 | /// | 125 | /// |
| 126 | /// # Panics | 126 | /// # Panics |
| 127 | -/// When the child cannot be spawned or reaped, or the writer thread panics. | 127 | +/// When the child cannot be spawned or reaped, when the writer thread |
| 128 | +/// panics, or when the child is still running after the deadline (below): | ||
| 129 | +/// it is killed and the test fails naming the command, the timeout and the | ||
| 130 | +/// kill -- the CFL suite lost 11h41m to a wedged aomenc child because the | ||
| 131 | +/// pre-pipedrain spawn had no bound (class: aomenc-stdin-pipe-deadlock). | ||
| 132 | +/// | ||
| 133 | +/// The deadline is 1800 s per child, overridable with | ||
| 134 | +/// `EC_AV1_CHILD_TIMEOUT_SECS`. Every legitimate child here is an | ||
| 135 | +/// aomenc/ffmpeg over an at-most-a-few-MB fixture: seconds, not minutes. | ||
| 128 | #[cfg(test)] | 136 | #[cfg(test)] |
| 129 | pub(crate) fn run_with_stdin(cmd: &mut Command, input: &[u8]) -> std::process::Output { | 137 | pub(crate) fn run_with_stdin(cmd: &mut Command, input: &[u8]) -> std::process::Output { |
| 130 | - use std::io::Write; | 138 | + use std::io::{Read, Write}; |
| 131 | use std::process::Stdio; | 139 | use std::process::Stdio; |
| 140 | + use std::time::{Duration, Instant}; | ||
| 141 | + | ||
| 142 | + let timeout = std::env::var("EC_AV1_CHILD_TIMEOUT_SECS") | ||
| 143 | + .ok() | ||
| 144 | + .and_then(|s| s.parse::<u64>().ok()) | ||
| 145 | + .map_or(Duration::from_secs(1800), Duration::from_secs); | ||
| 132 | 146 | ||
| 133 | cmd.stdin(Stdio::piped()) | 147 | cmd.stdin(Stdio::piped()) |
| 134 | .stdout(Stdio::piped()) | 148 | .stdout(Stdio::piped()) |
| 135 | .stderr(Stdio::piped()); | 149 | .stderr(Stdio::piped()); |
| 136 | let mut child = cmd.spawn().expect("encoder failed to start"); | 150 | let mut child = cmd.spawn().expect("encoder failed to start"); |
| 137 | let mut stdin = child.stdin.take().expect("encoder stdin"); | 151 | let mut stdin = child.stdin.take().expect("encoder stdin"); |
| 152 | + let mut stdout = child.stdout.take().expect("encoder stdout"); | ||
| 153 | + let mut stderr = child.stderr.take().expect("encoder stderr"); | ||
| 154 | + let program = cmd.get_program().to_string_lossy().into_owned(); | ||
| 155 | + | ||
| 138 | let payload = input.to_vec(); | 156 | let payload = input.to_vec(); |
| 139 | let writer = std::thread::spawn(move || { | 157 | let writer = std::thread::spawn(move || { |
| 140 | let _ = stdin.write_all(&payload); | 158 | let _ = stdin.write_all(&payload); |
| 141 | }); | 159 | }); |
| 142 | - let out = child.wait_with_output().expect("encoder failed to run"); | 160 | + // Both output pipes drained from the first millisecond: the child can |
| 161 | + // never wedge on a full stdout/stderr pipe while we are still writing | ||
| 162 | + // stdin (the pre-pipedrain inline `write_all` + `wait_with_output` | ||
| 163 | + // pattern that deadlocked the CFL suite's census gate). | ||
| 164 | + let out_reader = std::thread::spawn(move || { | ||
| 165 | + let mut buf = Vec::new(); | ||
| 166 | + let _ = stdout.read_to_end(&mut buf); | ||
| 167 | + buf | ||
| 168 | + }); | ||
| 169 | + let err_reader = std::thread::spawn(move || { | ||
| 170 | + let mut buf = Vec::new(); | ||
| 171 | + let _ = stderr.read_to_end(&mut buf); | ||
| 172 | + buf | ||
| 173 | + }); | ||
| 174 | + | ||
| 175 | + let deadline = Instant::now() + timeout; | ||
| 176 | + let status = loop { | ||
| 177 | + match child.try_wait().expect("encoder status") { | ||
| 178 | + Some(status) => break status, | ||
| 179 | + None if Instant::now() >= deadline => { | ||
| 180 | + let _ = child.kill(); | ||
| 181 | + let killed = child.wait().expect("encoder reap"); | ||
| 182 | + // The dead child closed every pipe, so all three threads | ||
| 183 | + // are out of their syscalls: the joins below are bounded. | ||
| 184 | + let _ = writer.join(); | ||
| 185 | + let _ = out_reader.join(); | ||
| 186 | + let _ = err_reader.join(); | ||
| 187 | + panic!( | ||
| 188 | + "{program}: still running {secs}s after spawn -- killed ({killed}). \ | ||
| 189 | + A live child here hangs the whole suite, which is how the CFL run lost \ | ||
| 190 | + 11h41m to aomenc wedged in anon_pipe_write \ | ||
| 191 | + (class: aomenc-stdin-pipe-deadlock). Check its wchan/fds before blaming \ | ||
| 192 | + the decoder.", | ||
| 193 | + secs = timeout.as_secs(), | ||
| 194 | + ); | ||
| 195 | + } | ||
| 196 | + None => std::thread::sleep(Duration::from_millis(50)), | ||
| 197 | + } | ||
| 198 | + }; | ||
| 199 | + let out = out_reader.join().expect("encoder stdout reader thread"); | ||
| 200 | + let err = err_reader.join().expect("encoder stderr reader thread"); | ||
| 143 | writer.join().expect("encoder stdin writer thread"); | 201 | writer.join().expect("encoder stdin writer thread"); |
| 144 | - out | 202 | + std::process::Output { |
| 203 | + status, | ||
| 204 | + stdout: out, | ||
| 205 | + stderr: err, | ||
| 206 | + } | ||
| 145 | } | 207 | } |