Closing: stop the Home picture builds instead of waiting for every cold recent file (#8398) (#9416)

* claim: #8398

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Closing: stop the Home picture builds instead of waiting for them (#8398)

Dropping SolveApp waited up to 10 s for the Home picture threads to end
(#6063). The thumbnails thread imports and rebuilds every cold recent
file (STEP and mesh imports, designs without a stored picture) one after
another, and its channel only closes once all are done, so closing the
window froze until every cold recent was built or the 10 s ran out.

The picture threads now build under a shared progress watch that
finish_pictures cancels first: the design being built stops at its next
feature (a STEP import at its next step), the remaining cold files are
skipped, and a build stopped part way draws and caches no picture.
Pictures already on the GPU are still waited for, so the driver crash
#6063 fixed stays fixed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
PaddleStroke
2026-10-30 15:46:06 +00:00
committed by GitHub
parent f1c2f67d63
commit f0d5666a74
+77 -5
View File
@@ -54,6 +54,10 @@ pub struct HomeState {
sample_rx: Option<std::sync::mpsc::Receiver<Made>>,
#[cfg(not(target_arch = "wasm32"))]
remembering: Option<std::sync::mpsc::Receiver<Made>>,
/// Watches the designs the picture threads open and build, and stops them when the app
/// closes ([`finish_pictures`]): closing must not wait for every cold recent file (#8398).
#[cfg(not(target_arch = "wasm32"))]
stop: std::sync::Arc<solidcraft_engine::doc::progress::Progress>,
/// A sample picked before its design was ready.
wanted: Option<usize>,
/// Whether each recent file is there, found off the window thread (a stat of a slow or
@@ -214,8 +218,12 @@ fn remember_at(s: &Session, key: &str, #[cfg(not(target_arch = "wasm32"))] ident
/// Waits up to `within` for the threads drawing Home pictures, and drops what they made: the
/// window is going, and a picture still on the GPU as the process exits can crash the driver.
/// They are told to stop first: the design being built stops at its next feature and the cold
/// recent files after it are left alone, so closing waits for one picture at most, not for every
/// recent design to be imported and rebuilt (#8398).
#[cfg(not(target_arch = "wasm32"))]
pub(crate) fn finish_pictures(h: &mut HomeState, within: std::time::Duration) {
h.stop.cancel();
let end = std::time::Instant::now() + within;
for rx in [h.rx.take(), h.sample_rx.take(), h.remembering.take()].into_iter().flatten() {
// Each thread sends what it made and ends; its channel closes when it has.
@@ -296,8 +304,28 @@ fn sample_picture(i: usize) -> Option<egui::ColorImage> {
decode_thumbnail(SAMPLE_PICTURES.get(i)?.to_vec())
}
/// Build what a key stands for (runs off the UI thread where threads exist).
/// Build what a key stands for (on the web, which has no threads; elsewhere [`make_watched`]).
#[cfg(any(test, target_arch = "wasm32"))]
fn make(key: &str) -> Made {
make_unless(key, &|| false)
}
/// [`make`] on a picture thread watched by `stop` ([`HomeState::stop`]): once stopped, the
/// design is built no further and no picture is drawn or cached.
#[cfg(not(target_arch = "wasm32"))]
fn make_watched(key: &str, stop: &std::sync::Arc<solidcraft_engine::doc::progress::Progress>) -> Made {
if stop.is_cancelled() {
return (key.to_string(), None, None);
}
let made = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
solidcraft_engine::doc::progress::watch(stop.clone(), || make_unless(key, &|| stop.is_cancelled()))
}));
made.unwrap_or_else(|_| (key.to_string(), None, None))
}
/// [`make`], with no picture once `stopped` says so: a build stopped part way is not the design,
/// and its picture must not be cached as the design's.
fn make_unless(key: &str, stopped: &dyn Fn() -> bool) -> Made {
let sample = key.strip_prefix("sample:").and_then(|i| i.parse::<usize>().ok());
if sample.is_none()
&& let Some(img) = picture(key)
@@ -315,7 +343,7 @@ fn make(key: &str) -> Made {
}),
None => s.execute("doc.open", &serde_json::json!({ "path": key })).is_ok(),
};
if !built {
if !built || stopped() {
return (key.to_string(), None, None);
}
if sample.is_some() {
@@ -354,12 +382,16 @@ fn start_jobs(h: &mut HomeState) {
}
let mut keys = std::mem::take(&mut h.queued);
let (tx, rx) = std::sync::mpsc::channel();
let stop = h.stop.clone();
let spawned = std::thread::Builder::new().name("thumbnails".into()).spawn(move || {
// Prefer local PNGs; even the metadata checks belong off the window thread.
keys.sort_by_key(|k| !cache_path(k).is_some_and(|p| p.exists()));
let Some(cold) = read_pictures(keys, &tx) else { return };
for k in cold {
let made = std::panic::catch_unwind(|| make(&k)).unwrap_or((k, None, None));
if stop.is_cancelled() {
return;
}
let made = make_watched(&k, &stop);
if tx.send(made).is_err() {
return;
}
@@ -377,11 +409,11 @@ fn start_sample(h: &mut HomeState) {
return;
}
let (tx, rx) = std::sync::mpsc::channel();
let stop = h.stop.clone();
if std::thread::Builder::new()
.name("selected-sample".into())
.spawn(move || {
let key = format!("sample:{i}");
let made = std::panic::catch_unwind(|| make(&key)).unwrap_or((key, None, None));
let made = make_watched(&format!("sample:{i}"), &stop);
let _ = tx.send(made);
})
.is_ok()
@@ -1273,6 +1305,46 @@ mod tests {
std::fs::remove_file(path).unwrap();
}
/// Closing the app with cold recent files still being imported for their pictures stops
/// them: it does not wait for each to be built, and nothing built part way is cached (#8398).
#[cfg(not(target_arch = "wasm32"))]
#[test]
fn closing_stops_the_cold_recent_pictures_instead_of_waiting_for_them() {
let nonce = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos();
let mut part = Session::default();
part.execute("solid.box", &serde_json::json!({"length":10,"width":20,"height":30})).unwrap();
let keys: Vec<String> = (0..4)
.map(|n| {
let path = std::env::temp_dir().join(format!("solidcraft-closing-cold-{}-{nonce}-{n}.step", std::process::id()));
let key = path.to_string_lossy().into_owned();
part.execute("file.export", &serde_json::json!({"path":key})).unwrap();
key
})
.collect();
let mut h = HomeState::default();
// The first cold design waits before its first feature until stopped: still being
// built whatever the machine's speed when the app closes.
h.stop.hold_at(0);
h.queued = keys.clone();
start_jobs(&mut h);
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(60);
while h.stop.features().1 == 0 && std::time::Instant::now() < deadline {
std::thread::sleep(std::time::Duration::from_millis(5));
}
assert!(h.stop.features().1 > 0, "the first cold recent is being built");
let rx = h.rx.take().unwrap();
assert!(matches!(rx.try_recv(), Err(std::sync::mpsc::TryRecvError::Empty)), "it is still being built");
h.rx = Some(rx);
let began = std::time::Instant::now();
finish_pictures(&mut h, std::time::Duration::from_secs(60));
assert!(began.elapsed() < std::time::Duration::from_secs(30), "closing waited {:?}", began.elapsed());
assert!(h.rx.is_none());
for key in keys {
assert!(!cache_path(&key).unwrap().exists(), "no picture of a stopped build is cached");
std::fs::remove_file(key).unwrap();
}
}
#[test]
fn first_home_frame_shows_placeholders_and_the_sample_pictures_arrive_without_building_any_model() {
let mut h = crate::scenario::Harness::new();