From 643c795bbfdae52ad0ca604ab1ba8f11cb92484f Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sun, 8 Nov 2026 10:23:14 +0100 Subject: [PATCH] Footage cache (#3271): a decode thread lets go of its flight before handing the planes over The decode thread's job held the flight until it ended, and the flight keeps the outcome for its waiters, so on a busy machine the planes just decoded still looked in use and the store dropped another file's instead (the eviction test failed on CI). The job now drops the flight before it sends the outcome. A test holds the decode thread after it sent; it fails with the old order. 300 runs of both eviction tests pass. --- crates/media/src/exr_planes.rs | 16 ++++++++++--- crates/media/src/exr_planes/tests.rs | 34 ++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/crates/media/src/exr_planes.rs b/crates/media/src/exr_planes.rs index bed9a38e4..c8c41befd 100644 --- a/crates/media/src/exr_planes.rs +++ b/crates/media/src/exr_planes.rs @@ -371,12 +371,15 @@ pub(crate) fn decodes_of(path: &str) -> Vec { DECODES.lock().unwrap_or_else(PoisonError::into_inner).iter().filter(|(p, _)| p == path).map(|(_, d)| d.clone()).collect() } -/// Run before a decode of a path is queued on the decode threads (`QUEUING`) and when the -/// queued job begins (`STARTING`), keyed by path (tests: holding a decode queued). +/// Run before a decode of a path is queued on the decode threads (`QUEUING`), when the +/// queued job begins (`STARTING`) and after it sent its outcome (`SENT`), keyed by path (tests: +/// holding a decode queued, or its decode thread busy after it). #[cfg(test)] pub(crate) static QUEUING: Mutex> = Mutex::new(Vec::new()); #[cfg(test)] pub(crate) static STARTING: Mutex> = Mutex::new(Vec::new()); +#[cfg(test)] +pub(crate) static SENT: Mutex> = Mutex::new(Vec::new()); #[cfg(test)] fn run_hook(hooks: &Mutex>, path: &str) { @@ -761,7 +764,14 @@ fn run(path: &str, source: &ExrSource, flight: &Arc, base: Option = write(vec![part(None, &RGBA, (32, 32), Compression::ZIP1)], (32, 32)).into(); + let one = 4 * (32 * 32 * 4 + 1); + let store = PlaneStore::new(2 * one); + let all = Want { all: true, ids: Default::default() }; + let get = |p: &str| { + let source = ExrSource::Bytes(bytes.clone()); + let index = crate::exr_index::get(p, &source).unwrap(); + store.get(p, &source, &index, &all, Use::Other).map(|_| ()) + }; + let (a, b, c) = ("/3271-late-a.exr", "/3271-late-b.exr", "/3271-late-c.exr"); + // b's decode thread stays in its job (at most 10 s) after it sent the planes. + let (go, wait) = std::sync::mpsc::channel::<()>(); + let wait = std::sync::Mutex::new(wait); + SENT.lock().unwrap().push(( + b.to_string(), + Arc::new(move |_: &str| { + let _ = wait.lock().unwrap().recv_timeout(std::time::Duration::from_secs(10)); + }), + )); + get(a).unwrap(); + get(b).unwrap(); + get(a).unwrap(); + get(c).unwrap(); + let cached = store.cached(); + let _ = go.send(()); + SENT.lock().unwrap().retain(|(p, _)| p != b); + assert_eq!(cached, [a, c], "b was least recently used"); +} + /// #474 / #3191: a file forgotten (Reload Footage) while it decodes keeps nothing of that decode, /// and the request asks again: no planes made before the reload are kept or served. #[test]