Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

fix(gpu): poll non-blockingly when recording a caught error

gedufulacb8dbdb4756
diff --git a/crates/gpu/src/health.rs b/crates/gpu/src/health.rs
index a7238e0b1..aa85f3917 100644
--- a/crates/gpu/src/health.rs
+++ b/crates/gpu/src/health.rs
@@ -105,12 +105,18 @@ impl DeviceHealth {
/// this a watched device would keep being handed to a GPU that has already failed. Polls
/// first: a lost device is a [`Fault::Lost`], and its callback may not have run yet (it fires
/// on a poll, and `Device::poll` panics on a lost device).
+ ///
+ /// The poll is a single non-blocking check, never a fence wait. This runs on the UI thread
+ /// immediately after a failed frame, so `PollType::Wait` with no timeout would stall the app
+ /// until every in-flight submission drained (or forever, if the device is wedged). A
+ /// non-blocking poll loses nothing here: wgpu still reads the fence and fires the
+ /// device-lost callback for a lost device, it just never blocks waiting for one.
pub fn caught(&self, device: &wgpu::Device, error: &wgpu::Error) {
if !self.is_ok() {
return;
}
#[cfg(not(target_arch = "wasm32"))]
- self.wait(device, None);
+ self.poll(device);
#[cfg(target_arch = "wasm32")]
let _ = device;
if !self.is_ok() {
diff --git a/crates/gpu/tests/device_loss.rs b/crates/gpu/tests/device_loss.rs
index 689a78728..82bedf7a2 100644
--- a/crates/gpu/tests/device_loss.rs
+++ b/crates/gpu/tests/device_loss.rs
@@ -1,6 +1,7 @@
//! The compositor on a lost device (#243): renders return `Unsupported` (so callers use the CPU
-//! compositor) instead of panicking with "Buffer 'pc_compose_uniforms' is invalid". Skips when no
-//! GPU adapter exists.
+//! compositor) instead of panicking with "Buffer 'pc_compose_uniforms' is invalid". Also covers a
+//! caught validation error on a watched device, which must fault the health flag so callers stop
+//! issuing GPU work. Skips when no GPU adapter exists.
use photocraft_color::{Color, ColorMode, SampleType};
use photocraft_doc::{Document, Layer, LayerContent};
@@ -163,6 +164,34 @@ fn a_captured_render_error_preserves_the_device_for_a_later_render() {
}
}
+#[test]
+fn a_watched_validation_error_faults_health_instead_of_panicking() {
+ let _lock = GPU_LOCK.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
+ let Some((adapter, device, queue)) = device() else { return };
+ let Ok(mut comp) = Compositor::try_new_with_format(&device, Compositor::preferred_acc_format(&adapter)) else { return };
+ let health = DeviceHealth::watch(&device);
+ comp.set_health(health.clone());
+ let d = doc();
+ assert!(health.is_ok(), "a fresh watched device starts healthy");
+ let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
+ comp.render(&device, &queue, &d, d.bounds(), |_, _| {
+ // A buffer past the device's valid limit is rejected synchronously, so the error
+ // reaches the compositor's error scope and never the uncaptured-error callback a
+ // watched device would otherwise record it with.
+ let _ = device.create_buffer(&wgpu::BufferDescriptor {
+ label: Some("synthetic_invalid_buffer"),
+ size: device.limits().max_buffer_size + 1,
+ usage: wgpu::BufferUsages::COPY_DST,
+ mapped_at_creation: false,
+ });
+ })
+ }));
+ assert!(result.is_ok(), "a caught validation error must return Err instead of reaching wgpu's panic handler");
+ assert!(result.unwrap().is_err(), "the invalid buffer must fail this render");
+ assert!(!health.is_ok(), "the caught error must fault the device's health so callers stop issuing GPU work");
+ assert!(health.fault().is_some(), "the fault must be reportable");
+}
+
fn assert_recovery_after_invalid_encoder(encode_only: bool) {
let _lock = GPU_LOCK.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
let Some((adapter, device, queue)) = device() else { return };