From 3c9d2439c38cdd5f95cdfac93cc6c3b95384469d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 27 Aug 2026 04:19:29 +0300 Subject: [PATCH] fix(h264): key ffmpeg/x264 scratch files per call, not per process ffmpeg_decode and x264_encode_qp named their temp files by process id plus stream length / qp alone. Both helpers are called from several #[test] functions that cargo test runs on parallel threads, so two concurrent calls with the same stream length or qp shared a path -- one call's cleanup (remove_file) could delete a file another call's ffmpeg child was still reading. An atomic per-call sequence number now makes every path distinct, matching the ec-av1 fix in 9b60697. Audited every other process::id()-keyed temp path in the repo (ec-flac, ec-opus, ec-ogg, ec-mp3, ec-vorbis, ec-alac, ec-h265, ec-probe, ec-truehd): each is either called from a single #[test] in its binary or discriminated by a unique literal/tag per caller, so none of those share a path across concurrent tests. --- crates/ec-h264/tests/encode.rs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/crates/ec-h264/tests/encode.rs b/crates/ec-h264/tests/encode.rs index 686a69d0..6e82eae0 100644 --- a/crates/ec-h264/tests/encode.rs +++ b/crates/ec-h264/tests/encode.rs @@ -437,9 +437,17 @@ fn have_ffmpeg() -> bool { /// Decode an Annex B stream with ffmpeg, returning one I420 triple per picture. fn ffmpeg_decode(stream: &[u8], w: usize, h: usize, extra: &[&str]) -> Option> { + // Distinct per call, not per process: several #[test]s in this binary + // call this on parallel threads, and a path keyed only on stream length + // lets one call's cleanup delete another's still-being-read file. + static SEQ: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); let dir = std::env::temp_dir().join(format!("ec-h264-enc-{}", std::process::id())); std::fs::create_dir_all(&dir).ok()?; - let path = dir.join(format!("s{}.264", stream.len())); + let path = dir.join(format!( + "s{}-{}.264", + stream.len(), + SEQ.fetch_add(1, std::sync::atomic::Ordering::Relaxed) + )); std::fs::write(&path, stream).ok()?; let mut cmd = std::process::Command::new("ffmpeg"); cmd.args(["-v", "error"]); @@ -1010,10 +1018,15 @@ fn have_x264() -> bool { /// off so both encoders spend bits by the same rule. Returns the Annex B /// stream. fn x264_encode_qp(sources: &[Planes], w: usize, h: usize, qp: i32, gop: u32) -> Option> { + // Distinct per call, not per process: two #[test]s in this binary call + // this on parallel threads with overlapping qp values, and a path keyed + // only on qp lets one call's cleanup delete another's still-in-use file. + static SEQ: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); + let seq = SEQ.fetch_add(1, std::sync::atomic::Ordering::Relaxed); let dir = std::env::temp_dir().join(format!("ec-h264-x264-{}", std::process::id())); std::fs::create_dir_all(&dir).ok()?; - let raw = dir.join(format!("in-q{qp}.yuv")); - let out = dir.join(format!("out-q{qp}.264")); + let raw = dir.join(format!("in-q{qp}-{seq}.yuv")); + let out = dir.join(format!("out-q{qp}-{seq}.264")); let mut buf = Vec::with_capacity(sources.len() * w * h * 3 / 2); for (y, u, v) in sources { buf.extend_from_slice(y);