From 61e4f00138545b092624c8a06adde0c75f0e3ef6 Mon Sep 17 00:00:00 2001 From: MeanTemperature <44195109+MeanTemperature@users.noreply.github.com> Date: Sat, 28 Nov 2026 07:43:24 +0000 Subject: [PATCH] CLI renders keep at most 1 GB of decoded footage frames (#4157) (#4167) * CLI: a headless session's footage frames keep at most 1 GB (#4157) Since #3301 the footage frames may fill the caches' whole common budget, and since #3941 the CLI's budget is that of the installed RAM. A one-pass render reads each frame about once, so it kept gigabytes of frames it never read again and was killed under a cgroup memory limit. Session::footage_cap caps the footage frames (and decoded planes) below the common budget; apply_cache_budgets honours it, so a new memory reading or a settings change keeps it. headless_session() sets it to HEADLESS_MEDIA_CACHE (1 GB, the footage cache's default in 0.24). The desktop app keeps the whole budget. Co-Authored-By: Claude Opus 5.5 * test(host): a headless session's footage cap is exactly HEADLESS_MEDIA_CACHE (#4157) Assert the cap itself (or the budget, if smaller) rather than only that it is below the budget, which a wrong cap above 1 GB would also pass. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- crates/engine/src/lib.rs | 6 ++++++ crates/engine/src/settings/prefs.rs | 4 ++-- crates/engine/src/settings/prefs/queries.rs | 5 +++++ crates/engine/src/settings/session_settings.rs | 5 +++-- crates/host/src/lib.rs | 5 +++++ crates/host/tests/it/memory_budget.rs | 17 ++++++++++++++++- crates/media/src/exr_planes.rs | 3 ++- crates/render/src/memory.rs | 3 ++- 8 files changed, 41 insertions(+), 7 deletions(-) diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index d4e8820e2..67808e628 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -231,6 +231,11 @@ pub struct Session { /// The system is in a low-memory episode (the cache budgets stay reduced until it ends, see /// [`sysinfo::SysMemory::still_low`] and [`Session::memory_budget`]). pub memory_low: bool, + /// The most the footage frames (and decoded planes) hold by themselves, below the common + /// budget ([`Session::apply_cache_budgets`]); `None`: the whole budget, as in the desktop app. + /// Headless sessions (the CLI, its MCP server) set it: their renders read each footage frame + /// about once, so frames kept up to the whole budget only cost memory (#4157). + pub footage_cap: Option, /// The keyframe drag in progress (`keys.move` with a merge key). pub key_move: Option, /// Reads the system's memory in the background for [`Session::memory_tick`]. @@ -394,6 +399,7 @@ impl Default for Session { script_ui: scriptui::ScriptUi::default(), sys_memory: None, memory_low: false, + footage_cap: None, key_move: None, memory_watch: Default::default(), disk_watch: Default::default(), diff --git a/crates/engine/src/settings/prefs.rs b/crates/engine/src/settings/prefs.rs index fd50835ce..b3595ad98 100644 --- a/crates/engine/src/settings/prefs.rs +++ b/crates/engine/src/settings/prefs.rs @@ -28,8 +28,8 @@ pub use choices::{APPEARANCE_MODES, DARK_THEMES, LANGUAGES, LIGHT_THEMES, THEMES pub use dialog::{Item, Kind, Page, pages, pages_json, todo_keys}; pub use document::{migrate, page_id, section_key}; pub use queries::{ - CacheCaps, FALLBACK_MEMORY_BUDGET, MEMORY_HEADROOM_PERCENT, MIN_MEMORY_BUDGET, MemoryBudget, PROCESS_PURGE_PERCENT, WEB_LAYER_CACHE, WEB_MEDIA_CACHE, - WEB_MEMORY_BUDGET, WEB_PREVIEW_CACHE, mask_color, + CacheCaps, FALLBACK_MEMORY_BUDGET, HEADLESS_MEDIA_CACHE, MEMORY_HEADROOM_PERCENT, MIN_MEMORY_BUDGET, MemoryBudget, PROCESS_PURGE_PERCENT, WEB_LAYER_CACHE, + WEB_MEDIA_CACHE, WEB_MEMORY_BUDGET, WEB_PREVIEW_CACHE, mask_color, }; pub use recent::{MAX_RECENT_SCRIPTS, RecentScript}; pub use sections::{ diff --git a/crates/engine/src/settings/prefs/queries.rs b/crates/engine/src/settings/prefs/queries.rs index c15d48269..5d8622ec7 100644 --- a/crates/engine/src/settings/prefs/queries.rs +++ b/crates/engine/src/settings/prefs/queries.rs @@ -172,6 +172,11 @@ pub const WEB_LAYER_CACHE: u64 = 512 << 20; /// The most the footage frame cache holds in the browser (see [`WEB_LAYER_CACHE`]). pub const WEB_MEDIA_CACHE: u64 = 256 << 20; +/// The most the footage frame cache holds in a headless session ([`crate::Session::footage_cap`], +/// #4157): an export reads each frame about once, so this keeps only what a frame's layers share +/// and what time effects read again (the footage cache's default size before the common budget). +pub const HEADLESS_MEDIA_CACHE: u64 = 1 << 30; + /// Above this part (percent) of the installed RAM left after **RAM Reserved for Other /// Applications**, the process's measured memory makes the caches give way even within their /// budget ([`Prefs::process_limits`]): above the caches' budget plus most of the room left for diff --git a/crates/engine/src/settings/session_settings.rs b/crates/engine/src/settings/session_settings.rs index 7b7b16ca1..f38504d8f 100644 --- a/crates/engine/src/settings/session_settings.rs +++ b/crates/engine/src/settings/session_settings.rs @@ -112,12 +112,13 @@ impl Session { /// (the layer cache, the footage frames and decoded planes, the preview frames), kept by the /// memory accountant, which frees the least valuable data first whichever cache holds it. No /// cache has a limit of its own below that budget, except the browser's caps - /// ([`crate::prefs::MemoryBudget::caps`]; frontends apply the preview frames' cap). + /// ([`crate::prefs::MemoryBudget::caps`]; frontends apply the preview frames' cap) and a + /// headless session's footage cap ([`Session::footage_cap`]). pub fn apply_cache_budgets(&mut self) { let b = self.memory_budget(); let caps = b.caps(cfg!(target_arch = "wasm32")); self.layer_cache.set_budget(caps.layer); - self.footage.set_cache_budget(caps.footage); + self.footage.set_cache_budget(self.footage_cap.map_or(caps.footage, |cap| cap.min(caps.footage))); let accountant = effectcraft_render::memory::Accountant::global(); let layers: std::sync::Arc = self.layer_cache.clone(); accountant.register(&layers); diff --git a/crates/host/src/lib.rs b/crates/host/src/lib.rs index 14b2e3bd8..64f512b8a 100644 --- a/crates/host/src/lib.rs +++ b/crates/host/src/lib.rs @@ -130,8 +130,13 @@ pub fn session() -> Session { /// as in the desktop app instead of the budget for unknown RAM. The desktop app reads it in the /// background every few seconds instead (`Session::memory_tick`); this waits for the reading /// (about a second on Windows). A session that can't read it keeps the budget for unknown RAM. +/// Its footage frames keep at most [`effectcraft_engine::prefs::HEADLESS_MEDIA_CACHE`] (#4157): a +/// render reads each frame about once, and frames kept up to the whole budget ran a render under +/// a container's memory limit out of memory. pub fn headless_session() -> Session { let mut s = session(); + s.footage_cap = Some(usize::try_from(effectcraft_engine::prefs::HEADLESS_MEDIA_CACHE).unwrap_or(usize::MAX)); + s.apply_cache_budgets(); s.take_memory_reading(effectcraft_engine::sysinfo::memory()); s } diff --git a/crates/host/tests/it/memory_budget.rs b/crates/host/tests/it/memory_budget.rs index 153932137..c3560cf7b 100644 --- a/crates/host/tests/it/memory_budget.rs +++ b/crates/host/tests/it/memory_budget.rs @@ -4,6 +4,9 @@ //! //! #3941: a headless session (the CLI, its MCP server) reads the system's memory once, so its //! caches share the budget of the installed RAM, as in the desktop app. +//! +//! #4157: but a headless session's footage frames keep less than that budget: its renders read +//! each frame about once, and frames kept up to the budget ran a render out of memory. use effectcraft_engine::render::memory::Accountant; @@ -22,10 +25,22 @@ fn a_host_session_bounds_the_caches() { eprintln!("the system's memory can't be read here: the headless reading is not checked"); return; }; - let headless = effectcraft_host::headless_session(); + let mut headless = effectcraft_host::headless_session(); let b = headless.memory_budget(); assert_eq!(b.installed, Some(memory.total), "the budget of the installed RAM, not of unknown RAM"); assert_eq!(b, effectcraft_engine::prefs::Prefs::default().memory_budget(Some(memory), b.reduced)); assert_eq!(headless.layer_cache.budget(), b.bytes, "the caches follow it"); assert_eq!(headless.export_cache_budget(), b.bytes, "exports and Render Queue runs follow it"); + + // #4157: the footage frames keep at most HEADLESS_MEDIA_CACHE (or the budget, if smaller), + // also once the budget changes (a new memory reading, then the settings) + let footage_capped = |s: &effectcraft_engine::Session, when: &str| { + let budget = s.memory_budget().bytes; + let cap = usize::try_from(effectcraft_engine::prefs::HEADLESS_MEDIA_CACHE).unwrap(); + assert_eq!(s.footage.cache_budget(), Some(cap.min(budget)), "{when}: the footage frames' cap under a {budget}-byte budget"); + }; + headless.take_memory_reading(Some(effectcraft_engine::sysinfo::SysMemory { total: 64 << 30, available: 48 << 30 })); + footage_capped(&headless, "a new memory reading"); + headless.execute("prefs.set", serde_json::json!({"key": "memory.ramReservedGb", "value": 8})).unwrap(); + footage_capped(&headless, "a settings change"); } diff --git a/crates/media/src/exr_planes.rs b/crates/media/src/exr_planes.rs index 2791295a9..d464ec10a 100644 --- a/crates/media/src/exr_planes.rs +++ b/crates/media/src/exr_planes.rs @@ -18,7 +18,8 @@ //! - Kept by the memory accountant (#3298): least recently used offered first, each entry worth //! what decoding it took. Also bounded by bytes by itself ([`CACHE_BUDGET`] until the caches' //! budget is set, then the footage frames' cap, [`set_budget`]: the whole budget on the -//! desktop, at most [`crate::exr_channels::WEB_CACHE_CAP`] in the browser); there the newest +//! desktop, less in a headless session (the CLI, #4157), at most +//! [`crate::exr_channels::WEB_CACHE_CAP`] in the browser); there the newest //! entry stays even when it alone is over. Entries in use (planes an effect holds, a decode in //! progress, the last pictures' planes kept for their effects, [`HELD_FILES`]) are never //! dropped, by either. diff --git a/crates/render/src/memory.rs b/crates/render/src/memory.rs index 8412401b7..f30311a1f 100644 --- a/crates/render/src/memory.rs +++ b/crates/render/src/memory.rs @@ -2,7 +2,8 @@ //! frames, the decoded footage planes, the preview frames and the disk cache's write queue //! register here as [`MemoryHolder`]s and share one budget ([`Accountant::set_budget`], from //! Settings ▸ Memory & CPU ▸ RAM Reserved for Other Applications). No holder has a share of its -//! own: when they hold more than the budget together, the accountant frees the least valuable +//! own (some have a cap below it: the browser's caches, a headless session's footage frames, +//! #4157): when they hold more than the budget together, the accountant frees the least valuable //! item first, whichever holder keeps it, in large steps: down to [`TRIM_TO`] of the budget, so //! a cache doesn't fill up and trim again with every insert. //!