mirror of
https://github.com/storytold/materialcraft.git
synced 2026-11-20 22:40:59 +01:00
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 <noreply@anthropic.com>
This commit is contained in:
1 parent
91bb53f07a
commit
858dde47a1
4 files changed
+74
-1
No files matched your search
@@ -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<Mutex<Cache>>,
|
||||
/// 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;
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,8 @@ pub struct Cooker {
|
||||
rx: Option<Receiver<Cooked>>,
|
||||
/// When the running cook started (app seconds).
|
||||
pub started: Option<f64>,
|
||||
/// `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;
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user