forge
Explore
Log in

Don't you dare go Hollow

The seat a test cycles is one no other test is sitting in

Browse files at this commit

88ce23e611e1737e09c317bd940fcb1da36a9346Ahmet Kaan GümüşParent: 3e11643
  • fix
    • the seat a test cycles is one no other test is sitting in
Show full message
🐛 fix(engine): the seat a test cycles is one no other test is sitting in

The stand-in encoder seat was armed by a process-wide variable and read by
`hw_seat`, so a software test that had already opened its own export picked
it up mid-flight, met the hang injection, and stalled: `cargo test -p engine
--test export` was green or five-failed depending on the interleaving. The
progress reset the fallback does made it worse -- one cancel test hot-spins
on progress reaching 1.0 and a regression there never ends.

The seat is process state, so it is borrowed rather than assumed: the whole
software suite shares a read guard, exactly as parallel as it always was, and
the encoder-death family takes it exclusively, lifting the software encode
pin only for its own exports and putting it back before it lets go. Which
lets `hw_seat` go back to being production policy with no test flag in it.

Also: a child that does not answer `vh_enc_dma_geometry` is now reaped there
instead of costing the next picture a second whole timeout; the plugin's own
note no longer claims the editor never loads the encode half, when what it
never does is *call* it -- the availability check is a dlopen and opens no
VA-API display; and the guard that refuses to write an export again in
software when a person picked the GPU by name now has the test it was
shipped without.
crates/engine-hw/src/lib.rsmodified+7 -5
@@ -10,11 +10,13 @@
1010 //! reach whatever the panic strategy is: Mesa runs its own C worker threads and
1111 //! calls libc `abort()` from them when the video ring resets (measured
1212 //! 2026-08-13, `vcn_unified_0`, mesa 26.1.6). No `catch_unwind` can see a
13-//! foreign thread's `abort`. So the *encode* half of this plugin is no longer
14-//! loaded by the editor at all: it is `dlopen`ed by the `hw-encode-child`
15-//! helper, one process per session, and a driver that aborts now costs an export
16-//! its seat rather than costing a person their session
17-//! ([`engine::hwproc`](../engine/hwproc/index.html)).
13+//! foreign thread's `abort`. So no *encode* entry point of this plugin is
14+//! called in the editor's process any more: the editor still `dlopen`s the
15+//! library to see whether the `vh_enc_*` symbols are there at all, which opens
16+//! no VA-API display and starts no driver thread, and every call through them is
17+//! made by the `hw-encode-child` helper, one process per session. A driver that
18+//! aborts now costs an export its seat rather than costing a person their
19+//! session ([`engine::hwproc`](../engine/hwproc/index.html)).
1820 //!
1921 //! corner-cut: *decode* is still in the editor's own address space, where the
2022 //! same abort would still be fatal -- and the same ring hangs it (mmhub page
crates/engine/src/export.rsmodified+1 -8
@@ -609,14 +609,7 @@ fn audio_kbps_of(settings: &ExportSettings) -> u32 {
609609 /// ([`Enc::open`] runs ahead of the muxer), which is what makes the refusal
610610 /// free.
611611 fn hw_seat(meta: &VideoMeta, settings: &ExportSettings) -> crate::Result<Option<HwEncoder>> {
612- // `VE_HW_TEST_FAKE` is the containment tests' stand-in seat -- a child that
613- // opens no driver at all -- and it is not what a software pin is pinning
614- // away: the pin says "do not use this machine's GPU", and that seat is not
615- // this machine's GPU. Without this the suite could only prove the fallback
616- // on a run with no other export test in it.
617- if settings.seat == EncoderSeat::Software
618- || (forced("VE_SW_ENC") && !crate::hwproc::faked())
619- {
612+ if settings.seat == EncoderSeat::Software || forced("VE_SW_ENC") {
620613 return Ok(None);
621614 }
622615 let opened = open_hw_seat(meta, settings);
crates/engine/src/hwproc.rsmodified+22 -9
@@ -462,18 +462,31 @@ impl Remote {
462462
463463 /// What the child's encoder codes in, or `None` where it has no zero-copy
464464 /// door.
465+ ///
466+ /// The two `None`s are not the same thing and only one of them is an
467+ /// answer. A child that *says* it has no door costs the caller a read-back
468+ /// and nothing else; a child that does not answer at all is gone, and is
469+ /// reaped right here -- otherwise the next picture would spend a whole
470+ /// second timeout discovering what this call already knows.
465471 pub fn dma_geometry(&mut self) -> Option<(u32, u32)> {
466472 self.request(&[T_GEOM], &[]).ok()?;
467- let (tag, _) = recv_tag(&self.sock).ok()?;
468- if tag != R_AU {
469- return None;
470- }
471473 let mut buf = [0u8; 8];
472- self.sock.read_exact(&mut buf).ok()?;
473- Some((
474- u32::from_le_bytes(buf[..4].try_into().ok()?),
475- u32::from_le_bytes(buf[4..].try_into().ok()?),
476- ))
474+ let answered = match recv_tag(&self.sock) {
475+ Ok((R_AU, _)) => self.sock.read_exact(&mut buf).map(|()| true),
476+ Ok(_) => Ok(false),
477+ Err(e) => Err(e),
478+ };
479+ match answered {
480+ Ok(true) => Some((
481+ u32::from_le_bytes(buf[..4].try_into().ok()?),
482+ u32::from_le_bytes(buf[4..].try_into().ok()?),
483+ )),
484+ Ok(false) => None,
485+ Err(e) => {
486+ self.bury(&e.to_string());
487+ None
488+ }
489+ }
477490 }
478491
479492 /// Feeds one packed I420 picture through the shared mapping.
crates/engine/tests/export.rsmodified+104 -30
@@ -53,23 +53,48 @@ fn part_path(out: &Path) -> PathBuf {
5353 PathBuf::from(format!("{}.part", out.display()))
5454 }
5555
56+/// Which seat an export takes is decided by *process-wide* environment
57+/// variables, so it is a shared resource and every test in this binary borrows
58+/// it rather than assuming it.
59+///
60+/// Readers share: the whole software suite wants the same pin and may run at
61+/// once, exactly as it always did. A test that has to *change* the pin --
62+/// there is one family, the encoder-death ones at the end of this file -- takes
63+/// it exclusively, so no export is ever in flight while the seat is being
64+/// cycled underneath it. A borrow held only for the seat's opening would not do:
65+/// [`engine::export::start`] falls back and re-opens mid-run, so the pin has to
66+/// hold for the whole export.
67+static SEAT: std::sync::RwLock<()> = std::sync::RwLock::new(());
68+
69+type Shared = std::sync::RwLockReadGuard<'static, ()>;
70+
5671 /// Pins both paths to software for the whole test binary, so the default suite
57-/// proves the fallback that every machine has.
58-fn pin_software() {
72+/// proves the fallback that every machine has. Bind the guard: it is the borrow
73+/// above, and dropping it early puts the export back in the race this exists to
74+/// end.
75+#[must_use = "the seat pin holds only while its guard is alive"]
76+fn pin_software() -> Shared {
77+ let borrowed = SEAT.read().unwrap_or_else(|poisoned| poisoned.into_inner());
5978 static PIN: Once = Once::new();
6079 PIN.call_once(|| unsafe {
6180 std::env::set_var("VE_SW", "1");
6281 std::env::set_var("VE_SW_ENC", "1");
6382 });
83+ borrowed
6484 }
6585
66-/// Removes both pins, for the hardware twins.
67-fn pin_hardware() {
86+/// Removes both pins, for the hardware twins -- which run in their own
87+/// `--ignored --test-threads=1` pass, so they never share this binary with the
88+/// software ones.
89+#[must_use = "the seat pin holds only while its guard is alive"]
90+fn pin_hardware() -> Shared {
91+ let borrowed = SEAT.read().unwrap_or_else(|poisoned| poisoned.into_inner());
6892 static PIN: Once = Once::new();
6993 PIN.call_once(|| unsafe {
7094 std::env::remove_var("VE_SW");
7195 std::env::remove_var("VE_SW_ENC");
7296 });
97+ borrowed
7398 }
7499
75100 /// `[0,30) + [60,150)` of the source: two cuts and a delete, so the export has
@@ -180,7 +205,7 @@ fn round_trip(name: &str, limit: Duration) {
180205
181206 #[test]
182207 fn exports_an_edited_timeline_in_software() {
183- pin_software();
208+ let _seat = pin_software();
184209 round_trip("sw", Duration::from_secs(120));
185210 }
186211
@@ -189,7 +214,7 @@ fn exports_an_edited_timeline_in_software() {
189214 #[test]
190215 #[ignore]
191216 fn exports_an_edited_timeline_in_hardware() {
192- pin_hardware();
217+ let _seat = pin_hardware();
193218 round_trip("hw", Duration::from_secs(60));
194219 }
195220
@@ -202,7 +227,7 @@ fn exports_an_edited_timeline_in_hardware() {
202227 /// (`playback.rs: a_fitted_clip_is_letterboxed_and_a_filled_one_is_not`).
203228 #[test]
204229 fn exports_at_the_project_resolution_with_the_watched_geometry() {
205- pin_software();
230+ let _seat = pin_software();
206231 let clip = |source: usize, start: u32| engine::Clip {
207232 start,
208233 in_frame: 0,
@@ -282,7 +307,7 @@ fn exports_at_the_project_resolution_with_the_watched_geometry() {
282307 /// header. Software only: the picture is beside the point here.
283308 #[test]
284309 fn exports_the_audio_stream_the_timeline_plays() {
285- pin_software();
310+ let _seat = pin_software();
286311 let multi = asset("test_multiaudio.mp4");
287312 let (meta, _) = engine::demux::Demuxer::open(&multi).expect("open the fixture");
288313 let whole = engine::Clip {
@@ -456,14 +481,14 @@ fn multi_source_round_trip(name: &str, limit: Duration) {
456481
457482 #[test]
458483 fn exports_two_sources_as_one_timeline_in_software() {
459- pin_software();
484+ let _seat = pin_software();
460485 multi_source_round_trip("multi_sw", Duration::from_secs(180));
461486 }
462487
463488 #[test]
464489 #[ignore]
465490 fn exports_two_sources_as_one_timeline_in_hardware() {
466- pin_hardware();
491+ let _seat = pin_hardware();
467492 multi_source_round_trip("multi_hw", Duration::from_secs(60));
468493 }
469494
@@ -473,7 +498,7 @@ fn exports_two_sources_as_one_timeline_in_hardware() {
473498 /// whose rate control is the one every machine has.
474499 #[test]
475500 fn a_higher_bitrate_writes_a_bigger_file() {
476- pin_software();
501+ let _seat = pin_software();
477502 let mut sizes = Vec::new();
478503 for bitrate in [1_000_000u64, 4_000_000, 8_000_000] {
479504 let mut session = edited(&asset("test_baseline.mp4"));
@@ -503,7 +528,7 @@ fn a_higher_bitrate_writes_a_bigger_file() {
503528 /// (the audio pass never touches the second file).
504529 #[test]
505530 fn a_vanished_source_fails_the_export() {
506- pin_software();
531+ let _seat = pin_software();
507532 let baseline = asset("test_baseline.mp4");
508533 let doomed = Scratch::file("ve_export_vanished_source", "mp4");
509534 std::fs::copy(&baseline, &doomed).expect("copy a second source");
@@ -537,7 +562,7 @@ fn a_vanished_source_fails_the_export() {
537562 /// thing a user would otherwise try to play.
538563 #[test]
539564 fn cancelling_leaves_no_file() {
540- pin_software();
565+ let _seat = pin_software();
541566 let mut session = edited(&asset("test_baseline.mp4"));
542567 session.pause();
543568 let out = out_path("cancel");
@@ -572,7 +597,7 @@ fn cancelling_leaves_no_file() {
572597 /// file, never a `.part`.
573598 #[test]
574599 fn cancelling_after_the_last_frame_leaves_no_file() {
575- pin_software();
600+ let _seat = pin_software();
576601 let out = out_path("late");
577602 let part = part_path(&out);
578603 for attempt in 1..=5 {
@@ -615,7 +640,7 @@ fn cancelling_after_the_last_frame_leaves_no_file() {
615640 #[test]
616641 #[ignore]
617642 fn exports_audio_alongside_the_video() {
618- pin_hardware();
643+ let _seat = pin_hardware();
619644 let source = asset("test_av.mp4");
620645 let mut session = PlaybackSession::open(&source).expect("open source");
621646 // One cut: the timeline is unchanged but the audio copy now runs as two
@@ -749,7 +774,7 @@ fn synthetic(index: u32, width: usize, height: usize) -> (Vec<u8>, Vec<u8>, Vec<
749774 /// early, which is a lip sync error for the whole export.
750775 #[test]
751776 fn a_leading_audio_gap_keeps_the_sound_after_it_in_place() {
752- pin_software();
777+ let _seat = pin_software();
753778 let source = asset("test_av.mp4");
754779 let mut session = PlaybackSession::open(&source).expect("open source");
755780 assert!(session.cut_at(1.0), "cut at 1 s");
@@ -797,7 +822,7 @@ fn a_leading_audio_gap_keeps_the_sound_after_it_in_place() {
797822 /// a second early.
798823 #[test]
799824 fn exports_a_gapped_timeline_as_black_and_silence() {
800- pin_software();
825+ let _seat = pin_software();
801826 let source = asset("test_av.mp4");
802827 let mut session = PlaybackSession::open(&source).expect("open source");
803828 assert!(session.cut_at(1.0), "split at 1 s");
@@ -887,7 +912,7 @@ fn exports_a_gapped_timeline_as_black_and_silence() {
887912 /// hardware one buys is speed, and that is the ignored test below.
888913 #[test]
889914 fn a_proxy_is_picture_only_every_frame_a_starting_point_and_cached() {
890- pin_software();
915+ let _seat = pin_software();
891916 let source = asset("test_av.mp4");
892917 let out = engine::proxy::path_for(&source).expect("a cache directory");
893918 // A proxy left by an earlier run would make this test assert about a file
@@ -1021,7 +1046,7 @@ fn a_proxy_is_picture_only_every_frame_a_starting_point_and_cached() {
10211046 #[test]
10221047 #[ignore = "needs the VA-API plugin"]
10231048 fn a_hardware_proxy_is_every_frame_a_starting_point_too() {
1024- pin_hardware();
1049+ let _seat = pin_hardware();
10251050 let source = asset("test_av.mp4");
10261051 let out = engine::proxy::path_for(&source).expect("a cache directory");
10271052 let _ = std::fs::remove_file(&out);
@@ -1094,7 +1119,7 @@ fn a_hardware_proxy_is_every_frame_a_starting_point_too() {
10941119 #[test]
10951120 #[ignore = "needs the VA-API plugin and a real library"]
10961121 fn a_proxy_of_real_4k_film_is_made_faster_than_it_plays() {
1097- pin_hardware();
1122+ let _seat = pin_hardware();
10981123 let Some(film) = engine::real_library::film("hevc_4k_hdr") else {
10991124 return;
11001125 };
@@ -1174,14 +1199,23 @@ fn a_proxy_of_real_4k_film_is_made_faster_than_it_plays() {
11741199 // opens a stand-in seat in the child that touches no driver;
11751200 // `VE_HW_TEST_ABORT` / `VE_HW_TEST_HANG` say where it dies or stops.
11761201
1177-/// The stand-in-seat variables are process-wide, so the tests below take turns
1178-/// with them; nothing else in this binary reads them, and every one is removed
1179-/// again before the turn is given up.
1202+/// The one place in this binary that *changes* the seat rather than reading it,
1203+/// so it takes [`SEAT`] exclusively: while it holds, no other export in this
1204+/// process is in flight, and every variable it set is gone again before it lets
1205+/// go. Which is the whole of the bug this shape exists to make impossible --
1206+/// a stand-in encoder seat cycled underneath a software test that had already
1207+/// opened its own is a hang, not a failure, and it does not reproduce.
1208+///
1209+/// It also lifts the software *encode* pin for its own exports, because the
1210+/// question here is what an export does when its hardware seat dies. Decode
1211+/// stays pinned to software: it is not what is under test and the fixture must
1212+/// decode the same way it does everywhere else in this file.
11801213 fn injected<T>(abort: Option<&str>, hang: Option<&str>, body: impl FnOnce() -> T) -> T {
1181- use std::sync::Mutex;
1182- static TURN: Mutex<()> = Mutex::new(());
1183- let _turn = TURN.lock().unwrap_or_else(|poisoned| poisoned.into_inner());
1214+ let _exclusive = SEAT.write().unwrap_or_else(|poisoned| poisoned.into_inner());
1215+ let restore = std::env::var_os("VE_SW_ENC");
11841216 unsafe {
1217+ std::env::set_var("VE_SW", "1");
1218+ std::env::remove_var("VE_SW_ENC");
11851219 std::env::set_var("VE_HW_TEST_FAKE", "1");
11861220 std::env::set_var("VE_HW_CHILD_BIN", env!("CARGO_BIN_EXE_hw-encode-child"));
11871221 // Short, because one of these is waiting out a child that has stopped
@@ -1207,6 +1241,12 @@ fn injected<T>(abort: Option<&str>, hang: Option<&str>, body: impl FnOnce() -> T
12071241 ] {
12081242 std::env::remove_var(var);
12091243 }
1244+ // Put the software suite's pin back exactly as it was found -- it is
1245+ // set once for the whole binary and this is the only thing that ever
1246+ // takes it away.
1247+ if let Some(pinned) = restore {
1248+ std::env::set_var("VE_SW_ENC", pinned);
1249+ }
12101250 }
12111251 match out {
12121252 Ok(value) => value,
@@ -1234,7 +1274,6 @@ fn short_edit(source: &Path, frames: u32) -> (Project, engine::VideoMeta) {
12341274 /// presses, and this is that same call with a driver dying under it.
12351275 #[test]
12361276 fn a_hardware_encoder_killed_mid_export_costs_the_file_nothing() {
1237- pin_software();
12381277 let source = asset("test_baseline.mp4");
12391278 let (project, meta) = short_edit(&source, 20);
12401279 let out = out_path("hw_child_abort");
@@ -1264,7 +1303,6 @@ fn a_hardware_encoder_killed_mid_export_costs_the_file_nothing() {
12641303 /// the first frame. A refusal, not a crash, and the file is whole.
12651304 #[test]
12661305 fn a_hardware_encoder_killed_at_its_open_is_simply_no_seat() {
1267- pin_software();
12681306 let source = asset("test_baseline.mp4");
12691307 let (project, meta) = short_edit(&source, 12);
12701308 let out = out_path("hw_child_abort_init");
@@ -1283,7 +1321,6 @@ fn a_hardware_encoder_killed_at_its_open_is_simply_no_seat() {
12831321 /// picture is fed and the encoder is being flushed when it dies.
12841322 #[test]
12851323 fn a_hardware_encoder_killed_at_the_drain_costs_the_file_nothing() {
1286- pin_software();
12871324 let source = asset("test_baseline.mp4");
12881325 let (project, meta) = short_edit(&source, 12);
12891326 let out = out_path("hw_child_abort_drain");
@@ -1306,7 +1343,6 @@ fn a_hardware_encoder_killed_at_the_drain_costs_the_file_nothing() {
13061343 /// that sat on a stuck child forever would be the hang this refuses.
13071344 #[test]
13081345 fn a_hardware_encoder_that_stops_answering_is_killed_and_replaced() {
1309- pin_software();
13101346 let source = asset("test_baseline.mp4");
13111347 let (project, meta) = short_edit(&source, 12);
13121348 let out = out_path("hw_child_hang");
@@ -1327,6 +1363,44 @@ fn a_hardware_encoder_that_stops_answering_is_killed_and_replaced() {
13271363 std::fs::remove_file(&out).unwrap();
13281364 }
13291365
1366+/// ...and the export that does **not** get written again: a person who picked
1367+/// the GPU on the export card asked for it by name, so a dead hardware seat is
1368+/// refused in words rather than answered with an hour in an encoder nobody
1369+/// chose. The same posture [`engine::export::start`] already takes for a machine
1370+/// with no hardware seat at all -- and the guard that separates the two is the
1371+/// one every test above depends on being narrow.
1372+///
1373+/// The process is alive either way, which is the invariant; what differs is who
1374+/// is told.
1375+#[test]
1376+fn an_export_that_asked_for_the_gpu_by_name_refuses_instead_of_falling_back() {
1377+ let source = asset("test_baseline.mp4");
1378+ let (project, meta) = short_edit(&source, 12);
1379+ let out = out_path("hw_child_named_seat");
1380+
1381+ let error = injected(Some("2"), None, || {
1382+ let settings = ExportSettings {
1383+ seat: engine::export::EncoderSeat::Hardware,
1384+ ..Default::default()
1385+ };
1386+ let handle = engine::export::start(project, meta, &out, &settings);
1387+ wait(&handle, Duration::from_secs(180))
1388+ .expect_err("a named hardware seat that died is not written again in software")
1389+ });
1390+
1391+ println!("named hardware seat: {error}");
1392+ assert!(
1393+ engine::hwproc::is_lost(&error),
1394+ "the refusal has to name the hardware failure: {error}"
1395+ );
1396+ assert!(
1397+ !out.exists(),
1398+ "a refused export still wrote {}",
1399+ out.display()
1400+ );
1401+ assert!(!part_path(&out).exists(), "the .part outlived the refusal");
1402+}
1403+
13301404 /// The seat's own contract, under the same injection and with no export around
13311405 /// it: a killed child comes back as an error carrying
13321406 /// [`engine::hwproc::HW_LOST`], every later call gives the same answer instead