Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

GPU: an uncaught error on the window's device is logged, not a panic (#5486) (#5667)

momysnow10b5cc591486
diff --git a/crates/ui-egui/src/gpu.rs b/crates/ui-egui/src/gpu.rs
index 05597f1f90..4903b75119 100644
--- a/crates/ui-egui/src/gpu.rs
+++ b/crates/ui-egui/src/gpu.rs
@@ -22,6 +22,7 @@ pub struct ViewportCallback(pub Frame);
/// are meshed on the CPU. When the two targets differ, the scene is drawn offscreen and blitted.
/// Also returns the handle to what each frame drew.
pub fn install(rs: &egui_wgpu::RenderState, scene: GpuTarget, window: GpuTarget, gpu_meshing: bool) -> (GpuTarget, DrawStatsHandle) {
+ log_uncaptured_errors(&rs.device);
let tess = if gpu_meshing { solidcraft_gpu::view::tessellation(&rs.adapter, &rs.device, &rs.queue, solidcraft_gpu::SHADER) } else { None };
let scene = GpuTarget { tess: tess.is_some(), ..scene };
let window = GpuTarget { tess: tess.is_some(), ..window };
@@ -31,6 +32,26 @@ pub fn install(rs: &egui_wgpu::RenderState, scene: GpuTarget, window: GpuTarget,
(scene, stats)
}
+/// A GPU error no error scope caught is reported, never a panic: a device's default handler
+/// panics with "wgpu error: ...", which would close the app with the open design (#5486 reported
+/// "wgpu error: Out of Memory" from such a panic). The headless picture devices do the same
+/// (`Capturer::headless`). Errors the code expects are caught under error scopes where they
+/// happen, and those take priority over this handler. Errors before [`install`] (egui's own
+/// pipelines at startup) still reach the default handler, so a startup failure stays a crash
+/// report with its startup step.
+pub fn log_uncaptured_errors(device: &wgpu::Device) {
+ use std::sync::atomic::{AtomicU32, Ordering};
+ let reported = std::sync::Arc::new(AtomicU32::new(0));
+ device.on_uncaptured_error(std::sync::Arc::new(move |e: wgpu::Error| {
+ // The first few only: an invalid resource repeats its error on every frame.
+ if reported.fetch_add(1, Ordering::Relaxed) < 8 {
+ log::error!("GPU: {e}");
+ #[cfg(not(target_arch = "wasm32"))]
+ eprintln!("SolidCraft graphics: {e}");
+ }
+ }));
+}
+
/// Draw this process's headless pictures (`ui.render`, the agent's view image, thumbnails of
/// designs and saves) on the app's own device, with the viewport's pipelines (native only: a
/// browser cannot wait for the GPU to hand a picture back); where the device cannot, the CPU
diff --git a/crates/ui-egui/src/surface_errors_tests.rs b/crates/ui-egui/src/surface_errors_tests.rs
index 4468f4765e..9d3b41f61f 100644
--- a/crates/ui-egui/src/surface_errors_tests.rs
+++ b/crates/ui-egui/src/surface_errors_tests.rs
@@ -4,6 +4,8 @@
//! its surfaces under `capture_errors`, logs the error and tries again once the retry is due. A
//! driver's refusal can't be provoked here, so these check the capture on a real device with an
//! error the validation always reports, and the retry policy with a stand-in clock and device.
+//! An error no scope catches at all is reported by the device's handler
+//! (`gpu::log_uncaptured_errors`) instead of panicking (#5486).
use egui_wgpu::{Attempt, ConfigureRetry, SURFACE_RECREATE_AFTER, capture_errors, wgpu};
use std::time::Duration;
@@ -25,6 +27,24 @@ fn an_error_under_capture_is_returned_not_a_panic() {
assert_eq!(buffer.size(), 4);
}
+/// An error no scope caught on the window's device used to reach wgpu's default handler, which
+/// panics ("wgpu error: Out of Memory", #5486). With the handler installed it is reported and
+/// the device goes on working.
+#[test]
+fn an_uncaught_gpu_error_is_not_a_panic() {
+ let Some((_, device, queue)) = crate::gpu::offscreen_device() else { return };
+ crate::gpu::log_uncaptured_errors(&device);
+ // No error scope around it: mapped at creation with a size that isn't a multiple of 4.
+ let _bad =
+ device.create_buffer(&wgpu::BufferDescriptor { label: Some("bad"), size: 3, usage: wgpu::BufferUsages::MAP_WRITE, mapped_at_creation: true });
+ let (buffer, error) = capture_errors(&device, || {
+ device.create_buffer(&wgpu::BufferDescriptor { label: Some("good"), size: 4, usage: wgpu::BufferUsages::COPY_DST, mapped_at_creation: false })
+ });
+ assert!(error.is_none(), "{error:?}");
+ queue.submit([]);
+ assert_eq!(buffer.size(), 4);
+}
+
/// A surface that keeps refusing its configuration waits 16 ms before the next try, doubling with
/// each refusal in a row up to 1 s, and every few refusals the surface is recreated (asked in
/// review of #1966). Only the first refusal of the streak is worth a warning.