Windows: a lost graphics device restarts the app instead of crashing (#10240) (#11109)

* claim: #10240

* Lost GPU: a loss met by a queue write restarts the app instead of crashing (#10240)

The per-frame probe (#6528) finds a device the driver reset before the
frame's uploads, but the driver resets a device at any moment: a reset
that lands after the probe is still met first by a queue write or the
present, and wgpu-core unwraps the driver's Lost there
(PendingWrites::activate) instead of losing the device. That panic
ended the app and wrote a crash report.

- egui-wgpu: the painter runs each frame under DeviceLost::survive. A
  panic the frame meets is taken for the loss once the probe then finds
  the device lost (the probe's path loses the device and runs its lost
  callback); the painter draws nothing more and the app restarts as for
  any lost device, keeping unsaved changes for recovery. Any other panic
  carries on unwinding.
- Crash reports: a panic inside wgpu-core that unwraps Lost is the
  device loss, not a bug, and is not reported. A command that meets it
  fails; the next frame's probe restarts the app.

---------

Co-authored-by: MateuszSychXpl <151755914+MateuszSychXpl@users.noreply.github.com>
This commit is contained in:
Mateusz Sych
2026-11-04 17:15:58 +00:00
committed by GitHub
parent 3e70ad9876
commit 6488a540c9
6 changed files with 123 additions and 0 deletions
+14
View File
@@ -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);
+16
View File
@@ -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");
+26
View File
@@ -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());
}
}
+3
View File
@@ -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.
+18
View File
@@ -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<R>(
&self,
device: &wgpu::Device,
queue: &wgpu::Queue,
draw: impl FnOnce() -> R,
) -> Option<R> {
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
+46
View File
@@ -626,6 +626,52 @@ impl Painter {
textures_delta: &mut epaint::textures::TexturesDelta,
capture_data: Vec<UserData>,
window: &Arc<winit::window::Window>,
) -> 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<UserData>,
window: &Arc<winit::window::Window>,
) -> f32 {
profiling::function_scope!();