From 2da516ed84e8f1eb2bda3a9ffa3ce093ec9344c3 Mon Sep 17 00:00:00 2001 From: arnav-hiwarkar Date: Thu, 12 Nov 2026 19:05:59 +0530 Subject: [PATCH] #2042: a decode in flight when its file is forgotten keeps nothing The media pool counts forgets and frame clears. A read-ahead (sequential playback's next frame, on its own thread) carries the count it was queued at and decodes nothing once a forget came after it; any decode keeps its frame only if no forget came while it ran. A viewer playing a movie at Close, or any reader decoding through it, can no longer bring the closed project's frames back after Session::replace_project forgot them. --- crates/media/src/pool.rs | 58 +++++++++++++++++++++++++++++++++++----- 1 file changed, 51 insertions(+), 7 deletions(-) diff --git a/crates/media/src/pool.rs b/crates/media/src/pool.rs index 872e4fdfa..6e2e8faa4 100644 --- a/crates/media/src/pool.rs +++ b/crates/media/src/pool.rs @@ -208,6 +208,10 @@ struct CacheState { last: HashMap, i64>, /// Movies whose frames can't be kept as 8-bit levels (more bits, or shown turned). wide: HashSet>, + /// Counts [`MediaPool::forget`]s and frame clears: a decode keeps its frame only when none + /// came while it ran, so one in flight when a file is forgotten (a closed project's, or one + /// changed on disk) can't bring the frame back. + forgets: u64, } /// A movie/audio source by path ([`Opened`]), opened by the first caller while the others wait @@ -406,6 +410,7 @@ impl MediaPool { let mut c = lock(&self.inner.cache); c.lru = Lru::default(); c.last.clear(); + c.forgets = c.forgets.wrapping_add(1); drop(c); // Decoded GIFs sit outside the frame budget; Purge frees them too. lock(&self.inner.gifs).clear(); @@ -431,6 +436,7 @@ impl MediaPool { } c.last.remove(path); c.wide.remove(path); + c.forgets = c.forgets.wrapping_add(1); drop(c); crate::exr_channels::forget(path); } @@ -455,7 +461,7 @@ impl MediaPool { if lock(&self.inner.cache).wide.contains(footage.path.as_str()) { return None; } - match Inner::frame_px(&self.inner, footage, t, true, None, float, true) { + match Inner::frame_px(&self.inner, footage, t, true, None, float, true, None) { Ok(Pixels::Q8(f)) => Some(f), _ => None, } @@ -850,7 +856,7 @@ impl Inner { /// The frame of `footage` at `t`, resampled to `size` (see [`FootageSource::frame_at_size`]): /// a movie's made at that size directly, a still's resampled once and kept at that size. fn frame_at(self: &Arc, footage: &Footage, t: Tick, playback: bool, size: Option<(u32, u32)>, float: FloatCurve) -> Result> { - match Self::frame_px(self, footage, t, playback, size, float, false)? { + match Self::frame_px(self, footage, t, playback, size, float, false, None)? { Pixels::F32(img) => Ok(img), // (a float request's key never holds 8-bit levels) Pixels::Q8(_) => Err(MediaError::Decode(format!("{}: 8-bit frame for a float request", footage.path))), @@ -859,8 +865,20 @@ impl Inner { /// [`Self::frame_at`], kept as 8-bit levels when `q8` is set and the frame has 8 bits (a /// float frame under the float key otherwise, and the movie is remembered as `wide`). + /// `since`: the [`CacheState::forgets`] count the read was asked at (a read-ahead's, when it + /// was queued); a read that missed is not decoded once a forget came after it, and a frame + /// decoded while one came is not kept. #[allow(clippy::too_many_arguments)] - fn frame_px(self: &Arc, footage: &Footage, t: Tick, playback: bool, size: Option<(u32, u32)>, float: FloatCurve, q8: bool) -> Result { + fn frame_px( + self: &Arc, + footage: &Footage, + t: Tick, + playback: bool, + size: Option<(u32, u32)>, + float: FloatCurve, + q8: bool, + since: Option, + ) -> Result { if !footage.has_video { return Err(MediaError::Unsupported(format!("{}: no video", footage.path))); } @@ -909,6 +927,10 @@ impl Inner { } c = self.done.wait(c).unwrap_or_else(std::sync::PoisonError::into_inner); } + let since = since.unwrap_or(c.forgets); + if c.forgets != since { + return Err(MediaError::Cancelled); + } self.misses.fetch_add(1, Ordering::Relaxed); c.inflight.insert(loc.key.clone()); drop(c); @@ -917,6 +939,9 @@ impl Inner { reads::note_all(&files); let img = img?; let mut c = lock(&self.cache); + if c.forgets != since { + return Ok(img); + } if q8 && let Pixels::F32(_) = img { // not 8-bit: kept as the float frame it is, which the renderer asks for next c.wide.insert(loc.key.path.clone()); @@ -935,23 +960,23 @@ impl Inner { next.key.size = loc.key.size; next.key.q8 = loc.key.q8; if next.key != loc.key && !c.lru.map.contains_key(&next.key) && !c.inflight.contains(&next.key) { - self.prefetch(footage, next_t, size, float, loc.key.q8); + self.prefetch(footage, next_t, size, float, loc.key.q8, c.forgets); } } } #[cfg(not(target_arch = "wasm32"))] - fn prefetch(self: &Arc, footage: &Footage, t: Tick, size: Option<(u32, u32)>, float: FloatCurve, q8: bool) { + fn prefetch(self: &Arc, footage: &Footage, t: Tick, size: Option<(u32, u32)>, float: FloatCurve, q8: bool, since: u64) { let (me, f) = (self.clone(), footage.clone()); self.prefetched.fetch_add(1, Ordering::Relaxed); // A plain thread, not a rayon job: the decode below parallelises with rayon itself. std::thread::spawn(move || { - let _ = me.frame_px(&f, t, false, size, float, q8); + let _ = me.frame_px(&f, t, false, size, float, q8, Some(since)); }); } #[cfg(target_arch = "wasm32")] - fn prefetch(self: &Arc, _footage: &Footage, _t: Tick, _size: Option<(u32, u32)>, _float: FloatCurve, _q8: bool) {} + fn prefetch(self: &Arc, _footage: &Footage, _t: Tick, _size: Option<(u32, u32)>, _float: FloatCurve, _q8: bool, _since: u64) {} /// A still (image file or sequence frame) at `loc.key.size` when one is set: resampled as the /// renderer would ([`FootageSource::frame_at_size`]), so the pool keeps the reduced frame and a @@ -1203,6 +1228,25 @@ fn lock(m: &Mutex) -> std::sync::MutexGuard<'_, T> { mod tests { use super::*; + /// #2042: a read queued before its file was forgotten (a read-ahead of a closed project's + /// movie, started from the frame before) decodes nothing and keeps nothing; a read asked + /// after the forget decodes and keeps its frame. + #[test] + fn reads_queued_before_a_forget_keep_nothing() { + let mut png = std::io::Cursor::new(Vec::new()); + image::RgbaImage::new(2, 2).write_to(&mut png, image::ImageFormat::Png).unwrap(); + let pool = MediaPool::new(); + pool.add_bytes("/a.png", png.into_inner().into()); + let f = Footage { path: "/a.png".into(), kind: FootageKind::Still, width: 2, height: 2, has_video: true, ..Default::default() }; + let queued = lock(&pool.inner.cache).forgets; + pool.forget("/a.png"); + let read = Inner::frame_px(&pool.inner, &f, Tick::ZERO, false, None, FloatCurve::Srgb, false, Some(queued)); + assert!(matches!(read, Err(MediaError::Cancelled)), "{:?}", read.err()); + assert_eq!(pool.stats().frames, 0, "the forgotten file's frame came back"); + pool.frame_at(&f, Tick::ZERO).unwrap(); + assert_eq!(pool.stats().frames, 1); + } + /// Conforming a long file's audio reads every chunk at its own time: the sample index times /// ticks per second overflowed `i64` past 12:36 at 48 kHz, and later chunks came out silent /// (#172).