SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Pictures drawn off the window thread finish before the process exits (#6062) (#6063)
ivisaf765c5fd143b8
diff --git a/apps/solidcraft/src/main.rs b/apps/solidcraft/src/main.rsindex 226bad6fbe..2137672c0e 100644--- a/apps/solidcraft/src/main.rs+++ b/apps/solidcraft/src/main.rs@@ -166,6 +166,11 @@ impl eframe::App for App {solidcraft_ui_egui::agent_chat::shutdown(&mut self.solve);// A save still being written ends before the app does.solidcraft_ui_egui::save_job::tick(&mut self.solve, true);+ // Pictures still being drawn off the window thread (Home cards, file previews) end+ // before the GPU is torn down: one drawn while the process exits can crash the driver.+ if !solidcraft_engine::capture::close(std::time::Duration::from_secs(5)) {+ log::warn!("a picture was still being drawn as the app closed");+ }solidcraft_ui_egui::recovery_ui::close(&mut self.solve);solidcraft_engine::crash::end_session();if let Some(lost_at) = self.gpu.as_ref().and_then(|g| g.lost_at) {diff --git a/crates/engine/src/capture.rs b/crates/engine/src/capture.rsindex 22d7b29dad..b71ff717b8 100644--- a/crates/engine/src/capture.rs+++ b/crates/engine/src/capture.rs@@ -5,7 +5,8 @@//! resort the host chooses, the CPU rasterizer). Every picture names the renderer that drew it.//! Without a renderer, a picture is an error and a save simply has no thumbnail.-use std::sync::{Arc, RwLock};+use std::sync::{Arc, Condvar, Mutex, RwLock};+use std::time::{Duration, Instant};use solidcraft_render::{Camera, Scene};@@ -43,6 +44,67 @@ pub fn install(renderer: Arc<Renderer>) {}}+/// Pictures being drawn, and whether the process has stopped drawing them: a picture still on+/// the GPU when the process exits can crash inside the graphics driver as it is torn down.+struct Gate {+ state: Mutex<(usize, bool)>,+ done: Condvar,+}++/// A picture being drawn, counted until it is dropped (also when the renderer panics).+struct Drawing<'a>(&'a Gate);++impl Drop for Drawing<'_> {+ fn drop(&mut self) {+ if let Ok(mut s) = self.0.state.lock() {+ s.0 = s.0.saturating_sub(1);+ }+ self.0.done.notify_all();+ }+}++impl Gate {+ const fn new() -> Gate {+ Gate { state: Mutex::new((0, false)), done: Condvar::new() }+ }++ /// One more picture being drawn; none once closed.+ fn enter(&self) -> Result<Drawing<'_>, String> {+ let mut s = self.state.lock().map_err(|_| "the picture gate is poisoned".to_string())?;+ if s.1 {+ return Err("the app is closing: no more pictures are drawn".into());+ }+ s.0 += 1;+ Ok(Drawing(self))+ }++ /// No new pictures; waits up to `within` for those being drawn. → whether they all finished.+ fn close(&self, within: Duration) -> bool {+ let end = Instant::now() + within;+ let Ok(mut s) = self.state.lock() else { return false };+ s.1 = true;+ while s.0 > 0 {+ let left = end.saturating_duration_since(Instant::now());+ if left.is_zero() {+ return false;+ }+ match self.done.wait_timeout(s, left) {+ Ok((next, _)) => s = next,+ Err(_) => return false,+ }+ }+ true+ }+}++static GATE: Gate = Gate::new();++/// For a process about to exit: no more pictures are drawn, and those being drawn are waited+/// for, up to `within`. → whether they all finished.+pub fn close(within: Duration) -> bool {+ GATE.close(within)+}+/// The installed renderer, if any.pub fn installed() -> Option<Arc<Renderer>> {RENDERER.read().ok().and_then(|r| r.clone())@@ -61,7 +123,9 @@ pub fn png(scene: &Scene, cam: &Camera, width: u32, height: u32) -> Result<Png,/// [`rgba`] with `renderer` instead of the installed one.pub fn rgba_with(renderer: Option<&Renderer>, scene: &Scene, cam: &Camera, width: u32, height: u32) -> Result<Picture, String> {let renderer = renderer.ok_or("no renderer is installed to draw pictures in this process")?;+ let drawing = GATE.enter()?;let picture = renderer(scene, cam, width, height)?;+ drop(drawing);if picture.rgba.len() as u64 != u64::from(width) * u64::from(height) * 4 {return Err(format!("the renderer returned {} bytes for {width} × {height} pixels", picture.rgba.len()));}@@ -111,4 +175,29 @@ mod tests {assert_eq!(png.renderer, "cpu");assert_eq!((warning("cpu"), warning("gpu"), warning("gpu-software")), (Some(CPU_WARNING), None, None));}++ #[test]+ fn closing_waits_for_pictures_being_drawn_and_refuses_new_ones() {+ let gate = Arc::new(Gate::new());+ let (started, go) = std::sync::mpsc::channel();+ let drawer = Arc::clone(&gate);+ let thread = std::thread::spawn(move || {+ let drawing = drawer.enter();+ let _ = started.send(());+ std::thread::sleep(Duration::from_millis(200));+ drop(drawing);+ });+ go.recv().unwrap();+ let t = Instant::now();+ assert!(gate.close(Duration::from_secs(10)));+ assert!(t.elapsed() >= Duration::from_millis(100), "it waited for the picture");+ assert!(gate.enter().is_err());+ thread.join().unwrap();+ // A picture that does not finish in time: closing gives up and says so.+ let stuck = Gate::new();+ let drawing = stuck.enter().unwrap();+ assert!(!stuck.close(Duration::from_millis(20)));+ drop(drawing);+ assert!(stuck.close(Duration::ZERO));+ }}diff --git a/crates/ui-egui/src/home.rs b/crates/ui-egui/src/home.rsindex 63782e5fa7..311a958feb 100644--- a/crates/ui-egui/src/home.rs+++ b/crates/ui-egui/src/home.rs@@ -200,6 +200,21 @@ fn remember_at(s: &Session, key: &str, #[cfg(not(target_arch = "wasm32"))] identSome(img)}+/// 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.+#[cfg(not(target_arch = "wasm32"))]+pub(crate) fn finish_pictures(h: &mut HomeState, within: std::time::Duration) {+ 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.+ while let Some(left) = end.checked_duration_since(std::time::Instant::now()) {+ if let Err(std::sync::mpsc::RecvTimeoutError::Disconnected) = rx.recv_timeout(left) {+ break;+ }+ }+ }+}+/// A Home picture is being drawn off the UI thread.#[cfg(test)]pub(crate) fn remembering(h: &HomeState) -> bool {diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rsindex 3e245c2e45..17f51060dd 100644--- a/crates/ui-egui/src/lib.rs+++ b/crates/ui-egui/src/lib.rs@@ -540,6 +540,15 @@ pub struct SolveApp {applied_system_fonts: bool,}+impl Drop for SolveApp {+ /// The Home pictures it started are drawn before it goes: one still on the GPU as the+ /// process exits can crash the graphics driver.+ fn drop(&mut self) {+ #[cfg(not(target_arch = "wasm32"))]+ home::finish_pictures(&mut self.home, std::time::Duration::from_secs(10));+ }+}+impl SolveApp {pub fn new(mut session: Session, services: Services) -> Self {#[cfg(test)]