From e0582668bb8e234a2d9f5fadd9c2f68c4f374007 Mon Sep 17 00:00:00 2001 From: kaltenbrunnermarkus <153593707+kaltenbrunnermarkus@users.noreply.github.com> Date: Sun, 1 Nov 2026 00:10:20 +0000 Subject: [PATCH] Web app: an opened design is drawn when its meshes cannot be made on a thread (#10362) The web build cannot start the open-meshing thread. Its fallback counted every body done but listed none ready, so whatever the file load opened on the web drew no bodies and kept "Loading parts n/n" up for good. The fallback now lists every body ready and the viewport meshes as it draws, as the fallback meant to. Found while opening a design through the file load in the browser (#10198). Co-authored-by: Claude Opus 5.5 --- crates/ui-egui/src/file_load_ui_tests.rs | 31 +++++++++ crates/ui-egui/src/open_meshing.rs | 86 ++++++++++++++++-------- 2 files changed, 90 insertions(+), 27 deletions(-) diff --git a/crates/ui-egui/src/file_load_ui_tests.rs b/crates/ui-egui/src/file_load_ui_tests.rs index ad301a050..ef714ab1a 100644 --- a/crates/ui-egui/src/file_load_ui_tests.rs +++ b/crates/ui-egui/src/file_load_ui_tests.rs @@ -460,6 +460,37 @@ fn a_control_import_passes_its_matrix_and_name_on() { let _ = std::fs::remove_dir_all(&dir); } +/// #10198: the web build cannot start the meshing thread. An opened design is still drawn whole +/// and its "Loading parts" note goes, instead of an empty view with "Loading parts n/n" for good. +#[test] +fn an_opened_design_is_drawn_whole_when_meshing_has_no_thread() { + let dir = scratch("progressive-no-thread"); + let path = large_design(&dir, 6); + let mut w = Win::new(); + let (tx, rx) = std::sync::mpsc::channel(); + w.app.control_rx = Some(rx); + let (open, open_rx) = crate::control::ControlRequest::new("engine.execute", json!({"command": "doc.open", "params": {"path": path}})); + tx.send(open).unwrap(); + crate::open_meshing::without_threads(|| { + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(120); + while open_rx.try_recv().is_err() { + w.frame(vec![]); + std::thread::sleep(std::time::Duration::from_millis(1)); + assert!(std::time::Instant::now() < deadline, "the open answers"); + } + for _ in 0..100 { + if !crate::open_meshing::active(&w.app) { + break; + } + w.frame(vec![]); + } + }); + assert!(!crate::open_meshing::active(&w.app), "the parts are in: {:?}", crate::open_meshing::progress(&w.app)); + let world = w.app.session.world_state(); + assert!(!world.bodies.is_empty()); + assert_eq!(crate::open_meshing::shown_world(&w.app).bodies.len(), world.bodies.len(), "the whole design is drawn"); +} + /// An opened design is in the window before its bodies are meshed: the view draws the bodies /// meshed so far (never meshing one itself), framed from their outlines, until all are. #[test] diff --git a/crates/ui-egui/src/open_meshing.rs b/crates/ui-egui/src/open_meshing.rs index 9f96b9192..65056ddfa 100644 --- a/crates/ui-egui/src/open_meshing.rs +++ b/crates/ui-egui/src/open_meshing.rs @@ -66,33 +66,41 @@ impl Meshing { } first.extend(rest); let job = (world.clone(), ready.clone(), done.clone(), stop.clone()); - let spawned = std::thread::Builder::new().name("open-meshing".into()).spawn(move || { - let (world, ready, done, stop) = job; - solidcraft_engine::kernel::for_each_on_threads(&first, 16, |&i| { - if stop.load(Ordering::Relaxed) { - return; - } - if let Some(b) = world.bodies.get(i) { - // What the viewport draws and picks: a placed part's shared mesh, not a - // moved copy of it (its placements are drawn as instances of it). - let (mesh, _) = b.mesh_in_place(); - // A large mesh's shared vertices too, so its first frame does not weld them. - if shared { - crate::viewport::warm_shared_tris(&mesh); - } - if plans { - let _ = b.display_plan(); - let _ = b.plan_mesh_in_place_at(solidcraft_engine::doc::display_quality::current()); - } - } - if let Ok(mut r) = ready.lock() { - r.push(i); - } - done.fetch_add(1, Ordering::Release); - }); - }); - if spawned.is_err() { - // No thread: the viewport meshes as it draws, as before. + let spawned = threads() + && std::thread::Builder::new() + .name("open-meshing".into()) + .spawn(move || { + let (world, ready, done, stop) = job; + solidcraft_engine::kernel::for_each_on_threads(&first, 16, |&i| { + if stop.load(Ordering::Relaxed) { + return; + } + if let Some(b) = world.bodies.get(i) { + // What the viewport draws and picks: a placed part's shared mesh, not a + // moved copy of it (its placements are drawn as instances of it). + let (mesh, _) = b.mesh_in_place(); + // A large mesh's shared vertices too, so its first frame does not weld them. + if shared { + crate::viewport::warm_shared_tris(&mesh); + } + if plans { + let _ = b.display_plan(); + let _ = b.plan_mesh_in_place_at(solidcraft_engine::doc::display_quality::current()); + } + } + if let Ok(mut r) = ready.lock() { + r.push(i); + } + done.fetch_add(1, Ordering::Release); + }); + }) + .is_ok(); + if !spawned { + // No thread (the web build): the viewport meshes as it draws, as before. Every body + // is listed ready too, or the picture stays empty with "Loading parts n/n" (#10198). + if let Ok(mut r) = ready.lock() { + r.extend(0..n); + } done.store(n, Ordering::Release); } Meshing { @@ -264,6 +272,30 @@ pub(crate) fn refresh(app: &mut SolveApp, now: f64) { m.shown_at = now; } +/// Whether the meshes may be made on a thread of their own. The web build cannot start one; a +/// test says no to do the same ([`without_threads`]). +fn threads() -> bool { + #[cfg(test)] + if NO_THREADS.with(std::cell::Cell::get) { + return false; + } + true +} + +#[cfg(test)] +thread_local! { + static NO_THREADS: std::cell::Cell = const { std::cell::Cell::new(false) }; +} + +/// Run `f` as the web build runs: meshing cannot start a thread. +#[cfg(test)] +pub(crate) fn without_threads(f: impl FnOnce() -> R) -> R { + NO_THREADS.with(|t| t.set(true)); + let r = f(); + NO_THREADS.with(|t| t.set(false)); + r +} + fn profiling() -> bool { solidcraft_engine::geom::env::var_os("PROFILE_OPEN").is_some() }