From fbeffe544874e9e76139f068b526971eb2bece3b Mon Sep 17 00:00:00 2001 From: Fredrik Hallin Date: Sat, 28 Nov 2026 08:21:22 +0100 Subject: [PATCH] Retire GPU contexts when driver polling panics --- crates/gpu/src/context.rs | 10 +++++++--- crates/gpu/src/readback.rs | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/crates/gpu/src/context.rs b/crates/gpu/src/context.rs index 7125ea526..f0d14f8f0 100644 --- a/crates/gpu/src/context.rs +++ b/crates/gpu/src/context.rs @@ -1090,7 +1090,7 @@ impl GpuContext { }, _ => self.poll_lock.lock().unwrap_or_else(|e| e.into_inner()), }; - self.device.poll(kind) + self.readbacks.poll(|| self.device.poll(kind))? }; res.map_err(|e| { let error = format!("GPU readback poll: {e}"); @@ -1114,7 +1114,11 @@ impl GpuContext { let slice = remaining.min(Self::POLL_SLICE); let res = { let _guard = self.poll_lock.lock().unwrap_or_else(|e| e.into_inner()); - self.device.poll(wgpu::PollType::Wait { submission_index: Some(submission_index.clone()), timeout: Some(slice) }) + match self.readbacks.poll(|| self.device.poll(wgpu::PollType::Wait { submission_index: Some(submission_index.clone()), timeout: Some(slice) })) + { + Ok(result) => result, + Err(_) => return, + } }; let done = match res { Ok(_) => true, @@ -1347,7 +1351,7 @@ impl GpuContext { (remaining, std::time::Duration::ZERO, Ok(wgpu::PollStatus::Poll)) } else { let slice = remaining.min(Self::POLL_SLICE); - let res = self.device.poll(wgpu::PollType::Wait { submission_index: submission.clone(), timeout: Some(slice) }); + let res = self.readbacks.poll(|| self.device.poll(wgpu::PollType::Wait { submission_index: submission.clone(), timeout: Some(slice) }))?; (remaining, slice, res) } }; diff --git a/crates/gpu/src/readback.rs b/crates/gpu/src/readback.rs index 51d941640..f41e36ff5 100644 --- a/crates/gpu/src/readback.rs +++ b/crates/gpu/src/readback.rs @@ -181,6 +181,20 @@ impl Readbacks { } } + /// A driver polling panic invalidates this context just as a reported device failure does. + /// Do not let later renders reuse it or leave its readback callbacks waiting for a timeout. + pub(crate) fn poll(&self, poll: impl FnOnce() -> T) -> Result { + self.check()?; + std::panic::catch_unwind(std::panic::AssertUnwindSafe(poll)).map_err(|panic| { + let detail = + panic.downcast_ref::().map(String::as_str).or_else(|| panic.downcast_ref::<&str>().copied()).unwrap_or("internal driver error"); + let error = format!("GPU polling failed ({detail}); context retired"); + self.retire(error.clone()); + log::error!("{error}"); + error + }) + } + pub(crate) fn start(self: &Arc, done: impl FnOnce(Result, Error>) + wgpu::WasmNotSend + 'static) -> Result { let mut done: Option = Some(Box::new(done)); let result = { @@ -445,6 +459,26 @@ mod tests { use super::*; use std::sync::atomic::{AtomicUsize, Ordering}; + #[test] + fn polling_panic_retires_context_and_settles_readbacks_once() { + let me = Readbacks::new().unwrap(); + let (tx, rx) = std::sync::mpsc::channel(); + let ticket = me + .start(move |result| { + tx.send(result).unwrap(); + }) + .unwrap(); + let error = me.poll(|| panic!("submission index mismatch")).unwrap_err(); + assert!(error.contains("submission index mismatch")); + assert_eq!(rx.recv().unwrap(), Err(error.clone())); + assert_eq!(me.check(), Err(error)); + ticket.complete(Ok(vec![1])); + assert!(rx.try_recv().is_err(), "a late callback cannot complete twice"); + let called = std::cell::Cell::new(false); + assert!(me.poll(|| called.set(true)).is_err()); + assert!(!called.get(), "a retired device is never polled again"); + } + #[test] fn timeout_racing_late_map_callback_unmaps_once() { let mapping = Arc::new(Mapping::new());