forge
Explore
Log in

Don't you dare go Hollow

Key ffmpeg/x264 scratch files per call, not per process

Browse files at this commit

3c9d2439c38cdd5f95cdfac93cc6c3b95384469dAhmet Kaan GümüşParent: f9d5fb9
  • fix
    • key ffmpeg/x264 scratch files per call, not per process
Show full message
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.rsmodified+16 -3
@@ -437,9 +437,17 @@ fn have_ffmpeg() -> bool {
437 437
438 /// Decode an Annex B stream with ffmpeg, returning one I420 triple per picture.438 /// Decode an Annex B stream with ffmpeg, returning one I420 triple per picture.
439 fn ffmpeg_decode(stream: &[u8], w: usize, h: usize, extra: &[&str]) -> Option<Vec<Planes>> {439 fn ffmpeg_decode(stream: &[u8], w: usize, h: usize, extra: &[&str]) -> Option<Vec<Planes>> {
440+ // Distinct per call, not per process: several #[test]s in this binary
441+ // call this on parallel threads, and a path keyed only on stream length
442+ // lets one call's cleanup delete another's still-being-read file.
443+ static SEQ: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
440 let dir = std::env::temp_dir().join(format!("ec-h264-enc-{}", std::process::id()));444 let dir = std::env::temp_dir().join(format!("ec-h264-enc-{}", std::process::id()));
441 std::fs::create_dir_all(&dir).ok()?;445 std::fs::create_dir_all(&dir).ok()?;
442- let path = dir.join(format!("s{}.264", stream.len()));446+ let path = dir.join(format!(
447+ "s{}-{}.264",
448+ stream.len(),
449+ SEQ.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
450+ ));
443 std::fs::write(&path, stream).ok()?;451 std::fs::write(&path, stream).ok()?;
444 let mut cmd = std::process::Command::new("ffmpeg");452 let mut cmd = std::process::Command::new("ffmpeg");
445 cmd.args(["-v", "error"]);453 cmd.args(["-v", "error"]);
@@ -1010,10 +1018,15 @@ fn have_x264() -> bool {
1010 /// off so both encoders spend bits by the same rule. Returns the Annex B1018 /// off so both encoders spend bits by the same rule. Returns the Annex B
1011 /// stream.1019 /// stream.
1012 fn x264_encode_qp(sources: &[Planes], w: usize, h: usize, qp: i32, gop: u32) -> Option<Vec<u8>> {1020 fn x264_encode_qp(sources: &[Planes], w: usize, h: usize, qp: i32, gop: u32) -> Option<Vec<u8>> {
1021+ // Distinct per call, not per process: two #[test]s in this binary call
1022+ // this on parallel threads with overlapping qp values, and a path keyed
1023+ // only on qp lets one call's cleanup delete another's still-in-use file.
1024+ static SEQ: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
1025+ let seq = SEQ.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
1013 let dir = std::env::temp_dir().join(format!("ec-h264-x264-{}", std::process::id()));1026 let dir = std::env::temp_dir().join(format!("ec-h264-x264-{}", std::process::id()));
1014 std::fs::create_dir_all(&dir).ok()?;1027 std::fs::create_dir_all(&dir).ok()?;
1015- let raw = dir.join(format!("in-q{qp}.yuv"));1028+ let raw = dir.join(format!("in-q{qp}-{seq}.yuv"));
1016- let out = dir.join(format!("out-q{qp}.264"));1029+ let out = dir.join(format!("out-q{qp}-{seq}.264"));
1017 let mut buf = Vec::with_capacity(sources.len() * w * h * 3 / 2);1030 let mut buf = Vec::with_capacity(sources.len() * w * h * 3 / 2);
1018 for (y, u, v) in sources {1031 for (y, u, v) in sources {
1019 buf.extend_from_slice(y);1032 buf.extend_from_slice(y);