From 858dde47a1d16ab4e50fcb14ceec7e549aa2adb6 Mon Sep 17 00:00:00 2001 From: SteamSmith <341182916+SteamSmith@users.noreply.github.com> Date: Thu, 5 Nov 2026 06:57:43 -0500 Subject: [PATCH] fix(cook): drop a stale background cook that a newer sync cook superseded A background cook of version A could land after session.inspect had synchronously cooked version B and cleared dirty, overwriting B's results with no recook. Sessions now count synchronous cooks (cook_epoch) and the cooker discards a result whose start epoch is out of date. Co-Authored-By: Claude Sonnet 5.5 --- crates/engine/src/session.rs | 9 +++++++ crates/engine/src/session.test.rs | 13 +++++++++++ crates/ui-egui/src/cook.rs | 14 ++++++++++- crates/ui-egui/src/cook.test.rs | 39 +++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 crates/engine/src/session.test.rs create mode 100644 crates/ui-egui/src/cook.test.rs diff --git a/crates/engine/src/session.rs b/crates/engine/src/session.rs index 7dbdeae..1158134 100644 --- a/crates/engine/src/session.rs +++ b/crates/engine/src/session.rs @@ -150,6 +150,9 @@ pub struct Session { pub cooked: Cooked, /// Set when the graph changed and needs cooking. pub dirty: bool, + /// Bumped by every synchronous [`Session::cook`]; a background cook that started before the + /// bump holds stale results and must not replace `cooked`. + pub cook_epoch: u64, /// Cooked node outputs, shared with background cooks. pub cache: Arc>, /// A mesh loaded for the 3D view (`scene: "custom"`): its file name and geometry. @@ -178,6 +181,7 @@ impl Session { clipboard: None, cooked: Cooked::default(), dirty: true, + cook_epoch: 0, cache: Arc::new(Mutex::new(Cache::default())), custom_mesh: None, undo: Vec::new(), @@ -323,6 +327,7 @@ impl Session { }; self.cooked = cooked; self.dirty = false; + self.cook_epoch = self.cook_epoch.wrapping_add(1); &self.cooked } @@ -402,3 +407,7 @@ impl Session { } } } + +#[cfg(test)] +#[path = "session.test.rs"] +mod tests; diff --git a/crates/engine/src/session.test.rs b/crates/engine/src/session.test.rs new file mode 100644 index 0000000..de09274 --- /dev/null +++ b/crates/engine/src/session.test.rs @@ -0,0 +1,13 @@ +#[cfg(test)] +mod session_tests { + use crate::Session; + use materialcraft_core::Package; + + #[test] + fn sync_cook_bumps_the_cook_epoch() { + let mut s = Session::new(Package::new("t")); + let before = s.cook_epoch; + s.cook(); + assert_ne!(s.cook_epoch, before); + } +} diff --git a/crates/ui-egui/src/cook.rs b/crates/ui-egui/src/cook.rs index 9b6e410..ca28863 100644 --- a/crates/ui-egui/src/cook.rs +++ b/crates/ui-egui/src/cook.rs @@ -12,6 +12,8 @@ pub struct Cooker { rx: Option>, /// When the running cook started (app seconds). pub started: Option, + /// `Session::cook_epoch` when the running cook started. + epoch: u64, } impl Cooker { @@ -25,7 +27,11 @@ pub fn drive(app: &mut MaterialCraftApp, ctx: &egui::Context) { if let Some(rx) = &app.cooker.rx { match rx.try_recv() { Ok(c) => { - app.session.cooked = c; + // A synchronous cook (e.g. `session.inspect`) since this one started is newer: + // its results stand and this stale snapshot is dropped. + if app.cooker.epoch == app.session.cook_epoch { + app.session.cooked = c; + } app.cooker.rx = None; app.cooker.started = None; ctx.request_repaint(); @@ -53,6 +59,7 @@ fn start(app: &mut MaterialCraftApp, now: f64) { let pkg = s.pkg.clone(); let (gi, max) = (s.graph, s.preview_size); let cache = s.cache.clone(); + let epoch = s.cook_epoch; let dec = s.resource_decoder(); let (tx, rx) = std::sync::mpsc::channel(); use materialcraft_engine::eval::Options; @@ -69,6 +76,7 @@ fn start(app: &mut MaterialCraftApp, now: f64) { match spawned { Ok(_) => { app.cooker.rx = Some(rx); + app.cooker.epoch = epoch; app.cooker.started = Some(now); } Err(e) => { @@ -83,3 +91,7 @@ fn start(app: &mut MaterialCraftApp, _now: f64) { // shortcut: no threads on the web; the cook runs on the UI thread (the page pauses). app.session.cook(); } + +#[cfg(test)] +#[path = "cook.test.rs"] +mod tests; diff --git a/crates/ui-egui/src/cook.test.rs b/crates/ui-egui/src/cook.test.rs new file mode 100644 index 0000000..7f08553 --- /dev/null +++ b/crates/ui-egui/src/cook.test.rs @@ -0,0 +1,39 @@ +#[cfg(test)] +mod cook_tests { + use crate::MaterialCraftApp; + use crate::cook::{drive, start}; + use crate::state::UiState; + use materialcraft_engine::{execute, sample_session}; + use serde_json::json; + + fn wait_and_drive(app: &mut MaterialCraftApp, ctx: &egui::Context) { + for _ in 0..2000 { + drive(app, ctx); + if !app.cooker.busy() { + return; + } + std::thread::sleep(std::time::Duration::from_millis(5)); + } + panic!("background cook never finished"); + } + + /// A background cook of version A lands after a synchronous cook of version B (as in + /// `session.inspect`): B's results must survive. + #[test] + fn stale_background_cook_does_not_overwrite_newer_sync_cook() { + let mut session = sample_session(); + session.preview_size = Some(64); + let mut app = MaterialCraftApp::headless(session, UiState::default()); + let ctx = egui::Context::default(); + + start(&mut app, 0.0); // cook of version A runs in the background + let added = execute(&mut app.session, "graph.node.add", &json!({"def": "clouds_1", "at": [0, 0]})).unwrap(); + let id = materialcraft_engine::core::NodeId(added["id"].as_u64().unwrap()); + assert!(app.session.dirty); + app.session.cook_if_dirty(); // version B cooked synchronously, dirty cleared + assert!(app.session.cooked.nodes.contains_key(&id)); + + wait_and_drive(&mut app, &ctx); + assert!(app.session.cooked.nodes.contains_key(&id), "stale cook replaced the newer results"); + } +}