#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().
This commit is contained in:
arnav-hiwarkar committed 2026-11-12 19:02:57 +05:30
1 parent 418a55bc4f
commit 365c472828
4 files changed
+80 -15

No files matched your search

+8
View File
@@ -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<LayerCache>,
/// 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<std::sync::atomic::AtomicBool>,
/// 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(),
+9 -7
View File
@@ -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<String>) {
// 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.
+48 -8
View File
@@ -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<ThumbnailJob> {
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<String>
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<Item = &effectcraft_project::Footage> {
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))
})
}
@@ -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)]