From 7c5b0da99b0ea6c3a3202925d22061ea1bdea997 Mon Sep 17 00:00:00 2001 From: arnav-hiwarkar Date: Wed, 21 Oct 2026 21:37:31 +0530 Subject: [PATCH 1/7] Close, New and Open Project free the closed project's memory `Session::replace_project` (`crates/engine/src/lib.rs`), shared by Close, New, Open and the demo project: - forgets the decoded frames and decoders of the closed project's footage, as well as the new project's (the existing #1070 behaviour); - emits `Event::PurgeCaches` after the swap, so the viewers drop the RAM preview of the closed project. Its comp ids can also collide with the new project's. Cache budgets and LRU order are unchanged; only the release path changes. --- crates/engine/src/lib.rs | 10 +++-- crates/engine/src/tests_menu_cmds.rs | 58 ++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 3 deletions(-) diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index cdfc7468e..4d8706bff 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -1160,9 +1160,10 @@ impl Session { self.script_ui.project_epoch = self.script_ui.project_epoch.wrapping_add(1); p.fix_next_id(); upgrade_effects(&mut p); - // The media layer keys decoded frames by path: files of the new project may have changed - // since an earlier project decoded them (#1070). - for it in p.items.values() { + // Let go of what the closed project decoded (frames, decoders, models), and of what the + // new project's files decoded under an earlier project: the media layer keys them by + // path, and the files may have changed since (#1070). + for it in self.project.items.values().chain(p.items.values()) { if let effectcraft_project::ItemKind::Footage(f) = &it.kind { self.forget_decoded(f); } @@ -1174,6 +1175,9 @@ impl Session { self.path = path; self.bump(); self.mark_saved(); + // The viewers' RAM preview shows the closed project's frames, whose comp ids can collide + // with the new project's. + self.events.push(Event::PurgeCaches); let first = self.project.comps().next().map(|(id, _)| *id); if let Some(c) = first { self.open_comp(c); diff --git a/crates/engine/src/tests_menu_cmds.rs b/crates/engine/src/tests_menu_cmds.rs index 6da07dd40..83f3b609c 100644 --- a/crates/engine/src/tests_menu_cmds.rs +++ b/crates/engine/src/tests_menu_cmds.rs @@ -1767,3 +1767,61 @@ fn old_guides_load_as_pixel_guides_and_percent_round_trips() { // A zero-sized comp never divides by zero. assert!(g.px(0, 0).is_finite()); } + +#[test] +fn replacing_project_releases_old_footage() { + // A footage source that records the paths it is told to forget. + struct Recording(std::sync::Mutex>); + impl crate::render::FootageSource for Recording { + fn frame( + &self, + _: effectcraft_project::ItemId, + _: &effectcraft_project::Footage, + _: effectcraft_time::Tick, + ) -> Option> { + None + } + fn forget(&self, path: &str) { + self.0.lock().unwrap().push(path.to_string()); + } + } + let mut s = comp(); + let src = std::sync::Arc::new(Recording(Default::default())); + s.footage = src.clone(); + let recorded = || src.0.lock().unwrap().clone(); + let with_old = |s: &Session| { + let mut p = (*s.project).clone(); + p.add_item("old", Default::default(), None, ItemKind::Footage(effectcraft_project::Footage { path: "/old.mp4".into(), ..Default::default() })); + p + }; + let old = with_old(&s); + s.replace_project(old, None); + s.drain_events(); + src.0.lock().unwrap().clear(); + + // Close Project lets go of the closed project's footage and clears the RAM preview. + s.execute("file.closeProject", json!({})).unwrap(); + assert!(recorded().contains(&"/old.mp4".to_string()), "{:?}", recorded()); + assert!(s.drain_events().contains(&Event::PurgeCaches)); + + // Opening a project forgets the old project's files and the new project's. + let old = with_old(&s); + s.replace_project(old, None); + s.drain_events(); + src.0.lock().unwrap().clear(); + let mut new = effectcraft_project::Project::default(); + new.add_item("new", Default::default(), None, ItemKind::Footage(effectcraft_project::Footage { path: "/new.mp4".into(), ..Default::default() })); + s.replace_project(new, None); + let paths = recorded(); + assert!(paths.contains(&"/old.mp4".to_string()) && paths.contains(&"/new.mp4".to_string()), "{paths:?}"); + assert!(s.drain_events().contains(&Event::PurgeCaches)); + + // New Project does the same. + let old = with_old(&s); + s.replace_project(old, None); + s.drain_events(); + src.0.lock().unwrap().clear(); + s.execute("file.newProject", json!({})).unwrap(); + assert!(recorded().contains(&"/old.mp4".to_string()), "{:?}", recorded()); + assert!(s.drain_events().contains(&Event::PurgeCaches)); +} From 418a55bc4f39e21db863de2e878f69d3ec9969b2 Mon Sep 17 00:00:00 2001 From: arnav-hiwarkar Date: Thu, 12 Nov 2026 19:00:15 +0530 Subject: [PATCH 2/7] #2042: test that thumbnail jobs can't refill a closed project's caches A Project panel thumbnail job made while a project is open, run after Close / New Project (held), or decoding through Close (gated), must leave nothing of the closed project's footage in the footage source or the layer cache. Both fail on the current code: the job decodes the old still again and keeps it. --- crates/engine/src/tests/project_release.rs | 80 ++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/crates/engine/src/tests/project_release.rs b/crates/engine/src/tests/project_release.rs index 490bb6576..c8ba5f9ea 100644 --- a/crates/engine/src/tests/project_release.rs +++ b/crates/engine/src/tests/project_release.rs @@ -61,3 +61,83 @@ fn replacing_project_releases_old_footage() { assert!(recorded().contains(&"/old.mp4".to_string()), "{:?}", recorded()); assert!(s.drain_events().contains(&Event::PurgeCaches)); } + +/// A footage source with a memory cache, like the media pool: a frame read keeps its file's +/// entry until the file is forgotten. With a gate, the first read waits for the test. +#[derive(Default)] +struct Caching { + cached: Mutex>, + gate: Mutex, std::sync::mpsc::Receiver<()>)>>, +} + +impl FootageSource for Caching { + fn frame(&self, _: ItemId, footage: &Footage, _: Tick) -> Option> { + let gate = self.gate.lock().unwrap().take(); + if let Some((entered, go)) = gate { + entered.send(()).unwrap(); + go.recv().unwrap(); + } + self.cached.lock().unwrap().insert(footage.path.clone()); + Some(Arc::new(Image::new(footage.width, footage.height))) + } + fn forget(&self, path: &str) { + self.cached.lock().unwrap().remove(path); + } +} + +/// A session showing `/old.png` (a footage item, and a comp with a layer of it) through a +/// [`Caching`] source: (session, source, footage item, comp). +fn old_project() -> (Session, Arc, ItemId, ItemId) { + let mut s = Session::default(); + let src = Arc::new(Caching::default()); + s.footage = src.clone(); + let still = Footage { path: "/old.png".into(), has_video: true, width: 8, height: 8, ..Default::default() }; + let mut p = Project::default(); + let item = p.add_item("old", Default::default(), None, ItemKind::Footage(still)); + s.replace_project(p, None); + s.execute("comp.new", json!({"name": "Main", "width": 8, "height": 8, "frameRate": 30, "duration": 1})).unwrap(); + s.execute("layer.addItem", json!({"item": item.0})).unwrap(); + let comp = s.active_comp_id().unwrap(); + (s, src, item, comp) +} + +#[test] +fn held_thumbnail_jobs_cannot_refill_the_closed_projects_caches() { + for close in ["file.closeProject", "file.newProject"] { + let (mut s, src, item, comp) = old_project(); + // Project panel thumbnails, made while the project is open and run after it closed (a + // thread that started late). + let jobs = [s.thumbnail_job(item, Tick(0), 16).unwrap(), s.thumbnail_job(comp, Tick(0), 16).unwrap()]; + s.execute(close, json!({})).unwrap(); + let shown: Vec<_> = jobs.into_iter().map(|job| job()).collect(); + assert!(src.cached.lock().unwrap().is_empty(), "{close}: the closed project's frames came back"); + assert_eq!(s.layer_cache.stats().entries, 0, "{close}: the closed project's layers came back"); + assert!(shown.iter().all(Option::is_none), "{close}: a closed project's thumbnail isn't shown"); + } +} + +#[test] +fn a_thumbnail_reading_through_close_lets_go_of_what_it_read() { + let (mut s, src, item, _) = old_project(); + let (entered, entered_rx) = std::sync::mpsc::channel(); + let (go, go_rx) = std::sync::mpsc::channel(); + *src.gate.lock().unwrap() = Some((entered, go_rx)); + let job = s.thumbnail_job(item, Tick(0), 16).unwrap(); + let running = std::thread::spawn(job); + // The job is decoding the closed project's still when the project closes. + entered_rx.recv().unwrap(); + s.execute("file.closeProject", json!({})).unwrap(); + go.send(()).unwrap(); + let shown = running.join().unwrap(); + assert!(src.cached.lock().unwrap().is_empty(), "the frame decoded through Close was kept"); + assert!(shown.is_none(), "a closed project's thumbnail isn't shown"); +} + +#[test] +fn an_open_projects_thumbnails_still_render() { + let (s, src, item, comp) = old_project(); + let (w, h, px) = s.thumbnail_job(item, Tick(0), 16).unwrap()().unwrap(); + assert_eq!((w, h, px.len()), (8, 8, 8 * 8 * 4)); + assert!(s.thumbnail_job(comp, Tick(0), 16).unwrap()().is_some()); + assert!(src.cached.lock().unwrap().contains("/old.png")); +} From 365c472828cff8971d647302c41a7ea3311d9c27 Mon Sep 17 00:00:00 2001 From: arnav-hiwarkar Date: Thu, 12 Nov 2026 19:02:57 +0530 Subject: [PATCH 3/7] #2042: closing a project calls off its thumbnail jobs Session::replace_project sets the closed project's flag (Session::project_closed) before it forgets the project's footage, and gives the next project a fresh one. A Project panel thumbnail job reads through it (CancellableFootage, #329): started after Close it reads nothing, a movie decode in flight stops early, and a job that read through Close forgets its files (and a comp thumbnail clears the layer cache) before answering None, so it can't refill the shared caches with the closed project's frames. replace_project also forgets the decoded proxies of the closed and new items, and drops the Cryptomatte probe for projects without footage. The thumbnail's pixel copy uses get(). --- crates/engine/src/lib.rs | 8 ++++ crates/engine/src/session/edit.rs | 16 ++++--- crates/engine/src/session/render.rs | 56 ++++++++++++++++++---- crates/engine/src/tests/project_release.rs | 15 ++++++ 4 files changed, 80 insertions(+), 15 deletions(-) diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 0b7608756..380358529 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -167,6 +167,13 @@ pub struct Session { pub journal: Vec<(String, Value)>, /// Processed-layer pixels reused across frames and edits (content-keyed, never stale). pub layer_cache: Arc, + /// Set when this project closes ([`Session::replace_project`]), then replaced by a fresh flag + /// for the next one: work started for the project off the UI thread (Project panel + /// thumbnails) stops reading its footage and lets go of what it read, so it can't fill the + /// shared caches with a closed project's frames. Public only so that other crates can build a + /// `Session { .. }`. + #[doc(hidden)] + pub project_closed: Arc, /// The last Cryptomatte pick probe (see [`Session::crypto_probe_job`]). Public only so that /// other crates can build a `Session { .. }`; its type is crate-private. #[doc(hidden)] @@ -351,6 +358,7 @@ impl Default for Session { events: vec![], journal: vec![], layer_cache: Arc::new(LayerCache::default()), + project_closed: Default::default(), crypto_probe: Default::default(), disk_cache: None, pending_disk_drops: Default::default(), diff --git a/crates/engine/src/session/edit.rs b/crates/engine/src/session/edit.rs index 9235dc41c..a1d994f45 100644 --- a/crates/engine/src/session/edit.rs +++ b/crates/engine/src/session/edit.rs @@ -196,6 +196,9 @@ impl Session { /// Replace the whole project (open/new), resetting history and state. pub fn replace_project(&mut self, mut p: Project, path: Option) { + // Work still running for the closed project stops reading it (see `project_closed`). + self.project_closed.store(true, std::sync::atomic::Ordering::SeqCst); + self.project_closed = Arc::default(); if self.stop_render() && let Some(mut job) = self.render_job.take() { @@ -221,14 +224,13 @@ impl Session { self.script_ui.project_epoch = self.script_ui.project_epoch.wrapping_add(1); p.fix_next_id(); upgrade_effects(&mut p); - // Let go of what the closed project decoded (frames, decoders, models), and of what the - // new project's files decoded under an earlier project: the media layer keys them by - // path, and the files may have changed since (#1070). - for it in self.project.items.values().chain(p.items.values()) { - if let effectcraft_project::ItemKind::Footage(f) = &it.kind { - self.forget_decoded(f); - } + // Let go of what the closed project's footage and proxies decoded (frames, decoders, + // models), and of what the new project's files decoded under an earlier project: the + // media layer keys them by path, and the files may have changed since (#1070). + for f in super::render::project_footage(&self.project).chain(super::render::project_footage(&p)) { + self.forget_decoded(f); } + self.drop_crypto_probe(); self.layer_cache.clear(); self.project = Arc::new(p); // The set of footage effects read the channels of is sent again, even when it is empty. diff --git a/crates/engine/src/session/render.rs b/crates/engine/src/session/render.rs index c3740196c..7e95a7bfe 100644 --- a/crates/engine/src/session/render.rs +++ b/crates/engine/src/session/render.rs @@ -1,3 +1,7 @@ +use std::sync::atomic::Ordering; + +use effectcraft_render::{CancellableFootage, FootageSource}; + use super::{EngineError, Image, ItemId, RenderOpts, Renderer, Result, Session, ThumbnailJob, Tick, Value, commands}; impl Session { @@ -77,9 +81,12 @@ impl Session { /// A Project panel thumbnail of an item (a comp at `t`, footage at its first frame), longest /// side at most `max_side`, as a job that owns everything it reads: frontends run it off the /// UI thread (a big comp takes far longer than a frame). `None` for items without pixels. + /// Once the project closes ([`Session::replace_project`]) the job reads no more and answers + /// `None`, after letting go of what it read meanwhile: a thread that started late or decoded + /// through Close can't fill the shared caches with the closed project's frames. pub fn thumbnail_job(&self, item: ItemId, t: Tick, max_side: u32) -> Option { let it = self.project.item(item)?; - let footage = self.footage.clone(); + let (footage, closed) = (self.footage.clone(), self.project_closed.clone()); match &it.kind { effectcraft_project::ItemKind::Comp(c) => { let (project, expr, cache) = (self.project.clone(), self.expr.clone(), self.layer_cache.clone()); @@ -93,21 +100,38 @@ impl Session { }; let bg = c.background; Some(Box::new(move || { - let mut r = Renderer::new(&project, footage.as_ref(), opts); + if closed.load(Ordering::SeqCst) { + return None; + } + let src = CancellableFootage { inner: footage.as_ref(), cancel: closed.clone() }; + let mut r = Renderer::new(&project, &src, opts); r.expr = expr.as_deref(); r.cache = Some(&cache); let img = r.comp_frame(item, t); + if closed.load(Ordering::SeqCst) { + project_footage(&project).for_each(|f| forget_files(footage.as_ref(), f)); + cache.clear(); + return None; + } Some((img.width, img.height, img.to_rgba8_over(bg))) })) } effectcraft_project::ItemKind::Footage(f) if f.has_video => { let f = f.clone(); Some(Box::new(move || { + if closed.load(Ordering::SeqCst) { + return None; + } // Vector footage is drawn at the thumbnail's size: its declared size can be a // 16384² canvas, gigabytes of pixels to point-sample down to a few dozen. let scale = (max_side.max(1) as f64 / f.width.max(f.height).max(1) as f64).min(1.0); - let src = footage.vector_frame(item, &f, scale).or_else(|| footage.frame(item, &f, Tick(0)))?; - let img = effectcraft_render::cineon_frame(src, &f); + let read = CancellableFootage { inner: footage.as_ref(), cancel: closed.clone() }; + let src = read.vector_frame(item, &f, scale).or_else(|| read.frame(item, &f, Tick(0))); + if closed.load(Ordering::SeqCst) { + forget_files(footage.as_ref(), &f); + return None; + } + let img = effectcraft_render::cineon_frame(src?, &f); // Point-sample down to at most `max_side` on the long side (a drawn size one // pixel over it, from rounding, is shrunk by that pixel, not halved). let (long, max) = (u64::from(img.width.max(img.height)), u64::from(max_side.max(1))); @@ -119,7 +143,7 @@ impl Session { for x in 0..w { let (sx, sy) = (u64::from(x) * u64::from(img.width) / u64::from(w), u64::from(y) * u64::from(img.height) / u64::from(h)); let i = ((sy * u64::from(img.width) + sx) * 4) as usize; - px.extend_from_slice(&full[i..i + 4]); + px.extend_from_slice(full.get(i..i + 4)?); } } Some((w, h, px)) @@ -145,9 +169,7 @@ impl Session { /// Make the media layer and the layer cache let go of what they decoded from `footage`'s /// files (it keys frames by path, so an overwritten file would keep showing its old pixels). pub(crate) fn forget_decoded(&self, footage: &effectcraft_project::Footage) { - for p in std::iter::once(&footage.path).chain(&footage.sequence) { - self.footage.forget(p); - } + forget_files(self.footage.as_ref(), footage); self.layer_cache.clear(); self.drop_crypto_probe(); } @@ -191,3 +213,21 @@ pub(crate) fn aux_footage(project: &effectcraft_project::Project) -> Vec paths.dedup(); paths } + +/// Make `src` let go of what it decoded from `footage`'s files (its file, or a sequence's). +fn forget_files(src: &dyn FootageSource, footage: &effectcraft_project::Footage) { + for p in std::iter::once(&footage.path).chain(&footage.sequence) { + src.forget(p); + } +} + +/// Every footage `project` shows: its footage items and the proxies of its items. +pub(crate) fn project_footage(project: &effectcraft_project::Project) -> impl Iterator { + project.items.values().flat_map(|it| { + let own = match &it.kind { + effectcraft_project::ItemKind::Footage(f) => Some(f), + _ => None, + }; + own.into_iter().chain(it.proxy.as_deref().map(|px| &px.footage)) + }) +} diff --git a/crates/engine/src/tests/project_release.rs b/crates/engine/src/tests/project_release.rs index c8ba5f9ea..f556b34be 100644 --- a/crates/engine/src/tests/project_release.rs +++ b/crates/engine/src/tests/project_release.rs @@ -62,6 +62,21 @@ fn replacing_project_releases_old_footage() { assert!(s.drain_events().contains(&Event::PurgeCaches)); } +#[test] +fn closing_a_project_releases_its_proxies() { + let mut s = Session::default(); + let src = Arc::new(Recording::default()); + s.footage = src.clone(); + let mut p = with_footage(&Project::default(), "old", "/old.mp4"); + let item = p.items.values_mut().next().unwrap(); + let footage = Footage { path: "/old-proxy.mov".into(), ..Default::default() }; + item.proxy = Some(Box::new(effectcraft_project::Proxy { footage, enabled: true })); + s.replace_project(p, None); + src.0.lock().unwrap().clear(); + s.execute("file.closeProject", json!({})).unwrap(); + assert!(src.0.lock().unwrap().contains(&"/old-proxy.mov".to_string()), "{:?}", src.0.lock().unwrap()); +} + /// A footage source with a memory cache, like the media pool: a frame read keeps its file's /// entry until the file is forgotten. With a gate, the first read waits for the test. #[derive(Default)] From 2da516ed84e8f1eb2bda3a9ffa3ce093ec9344c3 Mon Sep 17 00:00:00 2001 From: arnav-hiwarkar Date: Thu, 12 Nov 2026 19:05:59 +0530 Subject: [PATCH 4/7] #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). From 66337085711dcc32d1279188fddda77fac9e53b6 Mon Sep 17 00:00:00 2001 From: arnav-hiwarkar Date: Thu, 12 Nov 2026 19:12:26 +0530 Subject: [PATCH 5/7] #2042: a cache purge drops the Project panel's thumbnails Event::PurgeCaches (Close / New / Open Project, Edit > Purge, footage reloads) now also removes the Project panel's thumbnail textures and the renders awaited for them. They were keyed by item id only: the closed project's textures stayed in memory, and an item of the next project with a closed one's id showed the closed project's picture until its own render landed. --- crates/ui-egui/src/app/frame.rs | 1 + crates/ui-egui/src/panels/project.rs | 47 +++++++++++++++++++++++----- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/crates/ui-egui/src/app/frame.rs b/crates/ui-egui/src/app/frame.rs index b78c38c73..63101350d 100644 --- a/crates/ui-egui/src/app/frame.rs +++ b/crates/ui-egui/src/app/frame.rs @@ -153,6 +153,7 @@ impl EffectcraftApp { } effectcraft_engine::Event::PurgeCaches => { self.frames.clear(); + crate::panels::project::forget_thumbnails(ctx); // (frames made after it get keys of their own: see `Self::purge_stamp`) self.purges = self.purges.wrapping_add(1); } diff --git a/crates/ui-egui/src/panels/project.rs b/crates/ui-egui/src/panels/project.rs index 8d67fd9ee..bc03d282f 100644 --- a/crates/ui-egui/src/panels/project.rs +++ b/crates/ui-egui/src/panels/project.rs @@ -119,8 +119,22 @@ pub fn drop_folder(project: &effectcraft_engine::project::Project, row: Option)>>>>); +#[derive(Clone)] +struct PendingThumb(u64, Arc)>>>>); + +/// Let go of every Project panel thumbnail and the renders in flight for them (a cache purge, or +/// a project closed: the next project's item ids can be the closed one's). +pub fn forget_thumbnails(ctx: &egui::Context) { + ctx.data_mut(|d| { + d.remove_by_type::(); + d.remove_by_type::(); + }); +} /// Thumbnail of a comp (its current time) or footage (first frame), cached per item/revision. /// Rendered on a background thread (inline on wasm32): until the new one lands the previous @@ -129,19 +143,19 @@ fn thumbnail(app: &EffectcraftApp, ctx: &egui::Context, it: &Item) -> Option = ctx.data(|d| d.get_temp(key)); - let mut tex = cached.as_ref().map(|(_, t)| t.clone()); - let have = cached.as_ref().map(|(r, _)| *r); + let cached: Option = ctx.data(|d| d.get_temp(key)); + let mut tex = cached.as_ref().map(|Thumb(_, t)| t.clone()); + let have = cached.as_ref().map(|Thumb(r, _)| *r); // A finished render: upload it. let pending: Option = ctx.data(|d| d.get_temp(pkey)); - if let Some((prev, slot)) = &pending { + if let Some(PendingThumb(prev, slot)) = &pending { let done = slot.lock().ok().and_then(|mut s| s.take()); match done { Some(Some((w, h, px))) => { let ci = egui::ColorImage::from_rgba_unmultiplied([w as usize, h as usize], &px); let t = ctx.load_texture(format!("proj-thumb-{}", it.id.0), ci, egui::TextureOptions::LINEAR); ctx.data_mut(|d| { - d.insert_temp(key, (*prev, t.clone())); + d.insert_temp(key, Thumb(*prev, t.clone())); d.remove::(pkey); }); tex = Some(t); @@ -179,7 +193,7 @@ fn thumbnail(app: &EffectcraftApp, ctx: &egui::Context, it: &Item) -> Option(pkey, (rev, slot))); + ctx.data_mut(|d| d.insert_temp(pkey, PendingThumb(rev, slot))); ctx.request_repaint(); tex } @@ -1504,6 +1518,25 @@ mod tests { } } + /// #2042: closing the project drops the Project panel's thumbnails (and renders in flight), + /// so their textures go with it and an item of the next project with a closed one's id never + /// shows the closed project's picture. + #[test] + fn closing_the_project_forgets_its_thumbnails() { + let mut app = EffectcraftApp::new(effectcraft_engine::Session::default()); + let ctx = egui::Context::default(); + let (key, pkey) = (egui::Id::unique(("proj-thumb", 1u64)), egui::Id::unique(("proj-thumb-pending", 1u64))); + let tex = ctx.load_texture("t", egui::ColorImage::from_rgba_unmultiplied([1, 1], &[255; 4]), egui::TextureOptions::LINEAR); + ctx.data_mut(|d| { + d.insert_temp(key, Thumb(app.session.revision, tex)); + d.insert_temp(pkey, PendingThumb(app.session.revision, Arc::default())); + }); + app.session.execute("file.closeProject", serde_json::json!({})).unwrap(); + app.handle_events(&ctx); + assert!(ctx.data(|d| d.get_temp::(key)).is_none(), "the closed project's thumbnail is kept"); + assert!(ctx.data(|d| d.get_temp::(pkey)).is_none(), "a closed project's render is awaited"); + } + /// Long names show in full: the Name column fits them (it stopped at 260 points with "…") /// up to the panel's width less the Label column and, while optional columns show, room for /// them; a width the user dragged wins. From 0635f2496d9da71332671f3dd8522266058bd5a8 Mon Sep 17 00:00:00 2001 From: arnav-hiwarkar Date: Thu, 12 Nov 2026 19:32:06 +0530 Subject: [PATCH 6/7] #2042: review fixes for closing a project's thumbnails - CancellableFootage calls off vector_frame too: a closed project's SVG / PDF thumbnail stops before it rasterises, like a viewer frame nobody waits for. - replace_project forgets every footage file once and clears the layer cache and the Cryptomatte probe once, instead of once per footage item. - The through-Close test covers comp thumbnails (layer cache and footage) as well as footage ones, and fails with a timeout instead of hanging. - Docs: forget is also how a closed project's files go. --- crates/engine/src/session/edit.rs | 2 +- crates/engine/src/session/render.rs | 2 +- crates/engine/src/tests/project_release.rs | 31 ++++++++++++---------- crates/media/src/pool.rs | 10 ++++--- crates/render/src/footage.rs | 8 +++--- 5 files changed, 29 insertions(+), 24 deletions(-) diff --git a/crates/engine/src/session/edit.rs b/crates/engine/src/session/edit.rs index a1d994f45..7d2a20949 100644 --- a/crates/engine/src/session/edit.rs +++ b/crates/engine/src/session/edit.rs @@ -228,7 +228,7 @@ impl Session { // models), and of what the new project's files decoded under an earlier project: the // media layer keys them by path, and the files may have changed since (#1070). for f in super::render::project_footage(&self.project).chain(super::render::project_footage(&p)) { - self.forget_decoded(f); + super::render::forget_files(self.footage.as_ref(), f); } self.drop_crypto_probe(); self.layer_cache.clear(); diff --git a/crates/engine/src/session/render.rs b/crates/engine/src/session/render.rs index 7e95a7bfe..fe1634945 100644 --- a/crates/engine/src/session/render.rs +++ b/crates/engine/src/session/render.rs @@ -215,7 +215,7 @@ pub(crate) fn aux_footage(project: &effectcraft_project::Project) -> Vec } /// Make `src` let go of what it decoded from `footage`'s files (its file, or a sequence's). -fn forget_files(src: &dyn FootageSource, footage: &effectcraft_project::Footage) { +pub(crate) fn forget_files(src: &dyn FootageSource, footage: &effectcraft_project::Footage) { for p in std::iter::once(&footage.path).chain(&footage.sequence) { src.forget(p); } diff --git a/crates/engine/src/tests/project_release.rs b/crates/engine/src/tests/project_release.rs index f556b34be..f7fb91b99 100644 --- a/crates/engine/src/tests/project_release.rs +++ b/crates/engine/src/tests/project_release.rs @@ -132,20 +132,23 @@ fn held_thumbnail_jobs_cannot_refill_the_closed_projects_caches() { } #[test] -fn a_thumbnail_reading_through_close_lets_go_of_what_it_read() { - let (mut s, src, item, _) = old_project(); - let (entered, entered_rx) = std::sync::mpsc::channel(); - let (go, go_rx) = std::sync::mpsc::channel(); - *src.gate.lock().unwrap() = Some((entered, go_rx)); - let job = s.thumbnail_job(item, Tick(0), 16).unwrap(); - let running = std::thread::spawn(job); - // The job is decoding the closed project's still when the project closes. - entered_rx.recv().unwrap(); - s.execute("file.closeProject", json!({})).unwrap(); - go.send(()).unwrap(); - let shown = running.join().unwrap(); - assert!(src.cached.lock().unwrap().is_empty(), "the frame decoded through Close was kept"); - assert!(shown.is_none(), "a closed project's thumbnail isn't shown"); +fn thumbnails_reading_through_close_let_go_of_what_they_read() { + for which in ["footage", "comp"] { + let (mut s, src, item, comp) = old_project(); + let (entered, entered_rx) = std::sync::mpsc::channel(); + let (go, go_rx) = std::sync::mpsc::channel(); + *src.gate.lock().unwrap() = Some((entered, go_rx)); + let job = s.thumbnail_job(if which == "comp" { comp } else { item }, Tick(0), 16).unwrap(); + let running = std::thread::spawn(job); + // The job is decoding the closed project's still when the project closes. + entered_rx.recv_timeout(std::time::Duration::from_secs(30)).unwrap(); + s.execute("file.closeProject", json!({})).unwrap(); + go.send(()).unwrap(); + let shown = running.join().unwrap(); + assert!(src.cached.lock().unwrap().is_empty(), "{which}: the frame decoded through Close was kept"); + assert_eq!(s.layer_cache.stats().entries, 0, "{which}: the closed project's layers were kept"); + assert!(shown.is_none(), "{which}: a closed project's thumbnail isn't shown"); + } } #[test] diff --git a/crates/media/src/pool.rs b/crates/media/src/pool.rs index 6e2e8faa4..9e87c1575 100644 --- a/crates/media/src/pool.rs +++ b/crates/media/src/pool.rs @@ -423,7 +423,9 @@ impl MediaPool { lock(&self.inner.problems).clear(); } - /// Forget a file (after it changed on disk, or to retry a file that failed to open). + /// Forget a file (after it changed on disk or its project closed, or to retry a file that + /// failed to open). A decode of any file in flight meanwhile keeps nothing (see + /// [`CacheState::forgets`]). pub fn forget(&self, path: &str) { lock(&self.inner.sources).remove(path); lock(&self.inner.models).remove(path); @@ -1228,9 +1230,9 @@ 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. + /// #2042: a read queued before its file was forgotten (as a read-ahead of a closed project's + /// movie is, by the read of the frame before) decodes nothing and keeps nothing; a read asked + /// after the forget decodes and keeps its frame. (A still stands in for the movie.) #[test] fn reads_queued_before_a_forget_keep_nothing() { let mut png = std::io::Cursor::new(Vec::new()); diff --git a/crates/render/src/footage.rs b/crates/render/src/footage.rs index c14961d0f..9c4b2527c 100644 --- a/crates/render/src/footage.rs +++ b/crates/render/src/footage.rs @@ -73,9 +73,9 @@ pub trait FootageSource: Send + Sync { /// Drop the decoded frames it keeps (Edit ▸ Purge ▸ All Memory / Image Cache Memory): the /// next reads decode again. Sources without a cache ignore it. fn purge(&self) {} - /// The file at `path` changed on disk (Reload Footage): drop what it keeps of it (decoded - /// frames, open decoders, parsed models), so the next reads see the new contents. Sources - /// without a cache ignore it. + /// The file at `path` changed on disk (Reload Footage), or the project showing it closed: + /// drop what it keeps of it (decoded frames, open decoders, parsed models), so the next reads + /// see the new contents. Sources without a cache ignore it. fn forget(&self, _path: &str) {} /// Settings ▸ Disk ▸ Conformed Audio Folder: where decoded audio is kept between reads /// (`None` = off); sources that don't decode audio ignore it. @@ -205,7 +205,7 @@ impl FootageSource for CancellableFootage<'_> { self.inner.cache_budget() } fn vector_frame(&self, item: ItemId, footage: &Footage, scale: f64) -> Option> { - self.inner.vector_frame(item, footage, scale) + self.read(|| self.inner.vector_frame(item, footage, scale)) } fn cancellable(&self, cancel: &Arc, f: &mut dyn FnMut()) { self.inner.cancellable(cancel, f) From 9046c8d39a96102bd1c8839cb109aa0e22ff71d6 Mon Sep 17 00:00:00 2001 From: arnav-hiwarkar Date: Thu, 12 Nov 2026 20:03:35 +0530 Subject: [PATCH 7/7] #2042: read the Project panel thumbnail through panels::project::cached_thumbnail ui_large_project::comp_thumbnail_renders_off_the_ui_thread read the thumbnail cache by its old storage type, (u64, TextureHandle), which the purge change replaced with a private newtype, so it never saw the texture land. It now asks cached_thumbnail. --- crates/ui-egui/src/panels/project.rs | 5 +++++ crates/ui-egui/tests/it/ui_large_project.rs | 4 +--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/crates/ui-egui/src/panels/project.rs b/crates/ui-egui/src/panels/project.rs index bc03d282f..0d8074934 100644 --- a/crates/ui-egui/src/panels/project.rs +++ b/crates/ui-egui/src/panels/project.rs @@ -127,6 +127,11 @@ struct Thumb(u64, egui::TextureHandle); #[derive(Clone)] struct PendingThumb(u64, Arc)>>>>); +/// The Project panel's thumbnail texture of `item`, once its render has landed. +pub fn cached_thumbnail(ctx: &egui::Context, item: ItemId) -> Option { + ctx.data(|d| d.get_temp::(egui::Id::unique(("proj-thumb", item.0)))).map(|Thumb(_, t)| t) +} + /// Let go of every Project panel thumbnail and the renders in flight for them (a cache purge, or /// a project closed: the next project's item ids can be the closed one's). pub fn forget_thumbnails(ctx: &egui::Context) { diff --git a/crates/ui-egui/tests/it/ui_large_project.rs b/crates/ui-egui/tests/it/ui_large_project.rs index 4e2476269..2310087e3 100644 --- a/crates/ui-egui/tests/it/ui_large_project.rs +++ b/crates/ui-egui/tests/it/ui_large_project.rs @@ -64,9 +64,7 @@ fn comp_thumbnail_renders_off_the_ui_thread() { let mut landed = false; for _ in 0..400 { h.step(); - if h.state().auto.find("project.thumbnail").is_some() - && h.ctx.data(|d| d.get_temp::<(u64, egui::TextureHandle)>(egui::Id::unique(("proj-thumb", main.0)))).is_some() - { + if h.state().auto.find("project.thumbnail").is_some() && effectcraft_ui_egui::panels::project::cached_thumbnail(&h.ctx, main).is_some() { landed = true; break; }