mirror of
https://github.com/storytold/effectcraft.git
synced 2026-12-02 20:20:57 -05:00
Guard cleanup submission and reject results raced by device retirement
This commit is contained in:
1 parent
2c522e4f73
commit
ec422cdb11
2 files changed
+30
-13
No files matched your search
@@ -1090,7 +1090,7 @@ impl GpuContext {
|
||||
},
|
||||
_ => self.poll_lock.lock().unwrap_or_else(|e| e.into_inner()),
|
||||
};
|
||||
self.readbacks.poll(|| self.device.poll(kind))?
|
||||
self.readbacks.run_device(|| self.device.poll(kind))?
|
||||
};
|
||||
res.map_err(|e| {
|
||||
let error = format!("GPU readback poll: {e}");
|
||||
@@ -1114,7 +1114,9 @@ impl GpuContext {
|
||||
let slice = remaining.min(Self::POLL_SLICE);
|
||||
let res = {
|
||||
let _guard = self.poll_lock.lock().unwrap_or_else(|e| e.into_inner());
|
||||
match self.readbacks.poll(|| self.device.poll(wgpu::PollType::Wait { submission_index: Some(submission_index.clone()), timeout: Some(slice) }))
|
||||
match self
|
||||
.readbacks
|
||||
.run_device(|| self.device.poll(wgpu::PollType::Wait { submission_index: Some(submission_index.clone()), timeout: Some(slice) }))
|
||||
{
|
||||
Ok(result) => result,
|
||||
Err(_) => return,
|
||||
@@ -1351,7 +1353,8 @@ impl GpuContext {
|
||||
(remaining, std::time::Duration::ZERO, Ok(wgpu::PollStatus::Poll))
|
||||
} else {
|
||||
let slice = remaining.min(Self::POLL_SLICE);
|
||||
let res = self.readbacks.poll(|| self.device.poll(wgpu::PollType::Wait { submission_index: submission.clone(), timeout: Some(slice) }))?;
|
||||
let res =
|
||||
self.readbacks.run_device(|| self.device.poll(wgpu::PollType::Wait { submission_index: submission.clone(), timeout: Some(slice) }))?;
|
||||
(remaining, slice, res)
|
||||
}
|
||||
};
|
||||
@@ -1728,14 +1731,14 @@ impl GpuContext {
|
||||
/// count; if the wait frees none of them, later frames only poll until `dying` drops (a
|
||||
/// stopped queue must not re-wait [`crate::readback::TIMEOUT`] on every frame).
|
||||
pub fn pool_frame_end(&self) {
|
||||
// CPU fallback still ends frames. Never submit cleanup work to a retired queue.
|
||||
// CPU fallback still ends frames. Skip cleanup once retirement is already known.
|
||||
if self.readbacks.check().is_err() {
|
||||
return;
|
||||
}
|
||||
let before = self.pool.lock().map(|mut p| p.frame_end()).unwrap_or_default();
|
||||
if before > 0 {
|
||||
// A work-done callback waits for a submission; an empty one closes the wait.
|
||||
let index = self.queue.submit([]);
|
||||
let Ok(index) = self.readbacks.run_device(|| self.queue.submit([])) else { return };
|
||||
self.poll_wait(index, crate::readback::TIMEOUT);
|
||||
if let Ok(mut p) = self.pool.lock() {
|
||||
p.waited(before);
|
||||
|
||||
@@ -181,18 +181,21 @@ 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<T>(&self, poll: impl FnOnce() -> T) -> Result<T, Error> {
|
||||
/// A driver panic invalidates this context just as a reported device failure does.
|
||||
/// Driver calls can synchronously retire the context through error callbacks, so never
|
||||
/// hold the retirement lock across them. Reject results if retirement races the call.
|
||||
pub(crate) fn run_device<T>(&self, run: impl FnOnce() -> T) -> Result<T, Error> {
|
||||
self.check()?;
|
||||
std::panic::catch_unwind(std::panic::AssertUnwindSafe(poll)).map_err(|panic| {
|
||||
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(run)).map_err(|panic| {
|
||||
let detail =
|
||||
panic.downcast_ref::<String>().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");
|
||||
let error = format!("GPU operation failed ({detail}); context retired");
|
||||
self.retire(error.clone());
|
||||
log::error!("{error}");
|
||||
error
|
||||
})
|
||||
})?;
|
||||
self.check()?;
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub(crate) fn start(self: &Arc<Self>, done: impl FnOnce(Result<Vec<u8>, Error>) + wgpu::WasmNotSend + 'static) -> Result<Ticket, Error> {
|
||||
@@ -468,17 +471,28 @@ mod tests {
|
||||
tx.send(result).unwrap();
|
||||
})
|
||||
.unwrap();
|
||||
let error = me.poll(|| panic!("submission index mismatch")).unwrap_err();
|
||||
let error = me.run_device(|| 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!(me.run_device(|| called.set(true)).is_err());
|
||||
assert!(!called.get(), "a retired device is never polled again");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn retirement_during_a_driver_call_rejects_its_result_without_locking_callbacks() {
|
||||
let me = Readbacks::new().unwrap();
|
||||
let result = me.run_device(|| {
|
||||
// A device-error callback can run inside poll or submit, before the call returns.
|
||||
me.retire("device lost during submission".into());
|
||||
42
|
||||
});
|
||||
assert_eq!(result, Err("device lost during submission".into()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn timeout_racing_late_map_callback_unmaps_once() {
|
||||
let mapping = Arc::new(Mapping::new());
|
||||
|
||||
Reference in new issue
Block a user