diff --git a/crates/engine/src/crash.rs b/crates/engine/src/crash.rs index 9385dc9f0..0a585167a 100644 --- a/crates/engine/src/crash.rs +++ b/crates/engine/src/crash.rs @@ -435,6 +435,13 @@ fn hook(info: &std::panic::PanicHookInfo<'_>) -> bool { } let message = limited(info.payload_as_str().unwrap_or("(not a text message)"), MAX_MESSAGE); let location = info.location().map_or_else(|| "(unknown)".to_string(), |l| format!("{}:{}", l.file(), l.line())); + if graphics_device_lost(&message, &location) { + // What met the loss survives it (the window's painter, a command), and the app starts + // again as for any lost device: not a crash to report. + eprintln!("SolidCraft graphics: the device was lost during GPU work ({location}); starting again"); + IN_HOOK.store(false, Ordering::SeqCst); + return true; + } let sig = signature(&location, &message); let recovered = solidcraft_kernel::guarded(); let in_command = IN_COMMAND.with(Cell::get) > 0; @@ -493,6 +500,13 @@ fn hook(info: &std::panic::PanicHookInfo<'_>) -> bool { quiet } +/// A panic inside wgpu that unwraps the driver's `Lost`: a graphics device reset (Windows' TDR) +/// met first by a queue write or a present, which wgpu-core unwraps instead of losing the device +/// (`PendingWrites::activate`, #10240). It is the device loss, not a SolidCraft bug. +fn graphics_device_lost(message: &str, location: &str) -> bool { + location.contains("wgpu-core") && message.ends_with(": Lost") +} + /// Marks a command running on this thread; panics inside it wait for [`command_done`]. pub(crate) struct Scope(bool); diff --git a/crates/engine/src/crash_tests.rs b/crates/engine/src/crash_tests.rs index 684e14fb9..85113e223 100644 --- a/crates/engine/src/crash_tests.rs +++ b/crates/engine/src/crash_tests.rs @@ -637,6 +637,22 @@ fn signatures_ignore_numbers_but_not_places() { assert_ne!(signature("a.rs:1", "edge"), signature("a.rs:1", "face")); } +/// A graphics device reset met first by a queue write panics inside wgpu-core (#10240, #6528): +/// that is the device loss, which the app restarts after, not a crash to report. The same words +/// from anywhere else, and wgpu's other panics, still are. +#[test] +fn a_lost_graphics_device_met_inside_wgpu_is_not_a_crash() { + let lost = "called `Result::unwrap()` on an `Err` value: Lost"; + let windows = r"C:\Users\runneradmin\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\wgpu-core-30.0.1\src\device\queue.rs:503"; + let linux = "/home/runner/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/wgpu-core-30.0.1/src/device/queue.rs:503"; + assert!(graphics_device_lost(lost, windows)); + assert!(graphics_device_lost(lost, linux)); + assert!(!graphics_device_lost(lost, "crates/gpu/src/view.rs:3189"), "not inside wgpu"); + assert!(!graphics_device_lost("called `Result::unwrap()` on an `Err` value: OutOfMemory", linux)); + assert!(!graphics_device_lost("Error in Queue::submit: Validation Error", linux)); + assert!(!graphics_device_lost(lost, "/home/runner/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/wgpu-30.0.1/src/api/queue.rs:1")); +} + #[test] fn dates_are_formatted_in_utc() { assert_eq!(utc(0), "1970-01-01T00:00:00Z"); diff --git a/crates/ui-egui/src/gpu.rs b/crates/ui-egui/src/gpu.rs index 31a05cecb..3be5b0520 100644 --- a/crates/ui-egui/src/gpu.rs +++ b/crates/ui-egui/src/gpu.rs @@ -1542,4 +1542,30 @@ mod tests { let _ = device.poll(wgpu::PollType::wait_indefinitely()); assert!(!lost.is_lost() && lost.reason().is_none(), "{:?}", lost.reason()); } + + /// A driver reset that lands after the frame's probe is met by a queue write (or the present), + /// which panics inside wgpu (#10240). The painter survives that panic once the device turns + /// out lost, and draws nothing more; on a working device a panic is a bug and carries on. A + /// driver reset can't be made here: `destroy` loses the device in wgpu itself, so the probe + /// after the panic finds it already marked. + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn a_frame_that_meets_a_lost_device_is_survived() { + use egui_wgpu::wgpu; + let Some((_, device, queue)) = super::offscreen_device() else { + eprintln!("skipping: no GPU adapter"); + return; + }; + let lost = egui_wgpu::DeviceLost::watch(&device); + let met = || -> f32 { std::panic::panic_any(String::from("called `Result::unwrap()` on an `Err` value: Lost")) }; + assert_eq!(lost.survive(&device, &queue, || 0.5), Some(0.5)); + let bug = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| lost.survive(&device, &queue, met))); + assert!(bug.is_err(), "a panic on a working device carries on"); + assert!(!lost.is_lost()); + + device.destroy(); + let _ = device.poll(wgpu::PollType::wait_indefinitely()); + assert_eq!(lost.survive(&device, &queue, met), None, "the frame's panic was the loss"); + assert!(lost.is_lost()); + } } diff --git a/vendor/README.md b/vendor/README.md index 0b1aaa045..11a7b85e8 100644 --- a/vendor/README.md +++ b/vendor/README.md @@ -79,6 +79,9 @@ SolidCraft changes: driver lost is found by opening that command list rather than by the first queue write, which panicked inside wgpu-core (`PendingWrites::activate`, #4172). An empty submission only signalled a fence, which a reset D3D12 device let through (#6528). + A reset that lands after the probe is still met by a queue write or the present: the painter + runs the frame under `DeviceLost::survive`, which takes that panic for the loss once the probe + then finds the device lost, and lets any other panic carry on (#10240). - `src/lib.rs`, `src/winit.rs`: a surface configure that meets a lost device is not taken as configured (`configure_captured`): wgpu reports device-loss errors to no error scope, and on D3D12 a driver reset found by the configure has already dropped the surface's configuration. diff --git a/vendor/egui-wgpu/src/lib.rs b/vendor/egui-wgpu/src/lib.rs index 773207ea4..94778079d 100644 --- a/vendor/egui-wgpu/src/lib.rs +++ b/vendor/egui-wgpu/src/lib.rs @@ -202,6 +202,24 @@ impl DeviceLost { queue.submit([encoder.finish()]); self.is_lost() } + + /// Run `draw`, GPU work on this device, and survive a panic it meets once the device is lost: + /// `None` then. The driver resets a device at any moment, so a reset can land after the + /// probe and a queue write (or the present) still be the first call to meet it, which panics + /// inside wgpu (`PendingWrites::activate` unwraps `Lost`, #10240). After such a panic the + /// probe meets the loss and marks it. Any other panic carries on unwinding. + pub fn survive( + &self, + device: &wgpu::Device, + queue: &wgpu::Queue, + draw: impl FnOnce() -> R, + ) -> Option { + match std::panic::catch_unwind(std::panic::AssertUnwindSafe(draw)) { + Ok(drawn) => Some(drawn), + Err(_) if self.probe(device, queue) => None, + Err(panic) => std::panic::resume_unwind(panic), + } + } } /// SolidCraft: a frame acquired from a window's surface, which gives its swapchain image back diff --git a/vendor/egui-wgpu/src/winit.rs b/vendor/egui-wgpu/src/winit.rs index bc671a80d..7dea37b0b 100644 --- a/vendor/egui-wgpu/src/winit.rs +++ b/vendor/egui-wgpu/src/winit.rs @@ -626,6 +626,52 @@ impl Painter { textures_delta: &mut epaint::textures::TexturesDelta, capture_data: Vec, window: &Arc, + ) -> f32 { + // SolidCraft: a device lost while the frame is drawn, after its probe, panics inside wgpu + // at the first queue write to meet it (#10240). That panic is survived like a loss the + // probe meets: the painter draws nothing more and the app restarts. + let watch = self.render_state.as_ref().map(|rs| { + ( + rs.device_lost.clone(), + rs.device.clone(), + rs.queue.clone(), + ) + }); + let context = self.context.clone(); + let paint = || { + self.paint_frame( + viewport_id, + pixels_per_point, + clear_color, + clipped_primitives, + textures_delta, + capture_data, + window, + ) + }; + let Some((lost, device, queue)) = watch else { + return paint(); + }; + lost.survive(&device, &queue, paint).unwrap_or_else(|| { + log::error!( + "The graphics device was lost during a frame: {}", + lost.reason().unwrap_or_default() + ); + context.request_repaint_of(viewport_id); + 0.0 + }) + } + + #[expect(clippy::too_many_arguments)] + fn paint_frame( + &mut self, + viewport_id: ViewportId, + pixels_per_point: f32, + clear_color: [f32; 4], + clipped_primitives: &[epaint::ClippedPrimitive], + textures_delta: &mut epaint::textures::TexturesDelta, + capture_data: Vec, + window: &Arc, ) -> f32 { profiling::function_scope!();