mirror of
https://github.com/storytold/craft-libs.git
synced 2026-12-01 10:48:15 +01:00
GPU: idle polls detect device loss and uploads recheck out-of-memory
poll_reads now runs the device validity check when no read is pending, so device_status reports a destroyed device found by an idle poll. It polls before checking whether reads are ready, so a read whose map completes in the same poll returns its bytes. upload_buffer checks the device health again after its validity probe and returns an out-of-memory error recorded during that probe instead of a handle.
This commit is contained in:
1 parent
9254c39fc5
commit
fd4b036867
2 files changed
+76
-5
No files matched your search
@@ -516,6 +516,10 @@ impl GpuContext for WgpuContext {
|
||||
}
|
||||
// Loss bypasses error scopes and its callback can wait for pending queue work.
|
||||
d.check_device_valid()?;
|
||||
// The probe's poll can run queue callbacks that record an out-of-memory error.
|
||||
if let Some(fault) = d.health.fault() {
|
||||
return Err(fault.into());
|
||||
}
|
||||
let id = u32::try_from(d.buffers.len()).map_err(|_| GpuFault::Operation("too many buffers".into()))?;
|
||||
d.buffers.push(Some((buffer, true_len)));
|
||||
Ok(HandleId(id))
|
||||
@@ -675,11 +679,18 @@ impl GpuContext for WgpuContext {
|
||||
let mut outcomes = std::mem::take(&mut self.ready);
|
||||
#[cfg(all(not(target_arch = "wasm32"), feature = "adapter-probe"))]
|
||||
if let Backing::Device(d) = &mut self.backing {
|
||||
// A non-blocking poll that also runs after an error other than a loss was recorded,
|
||||
// and records a loss instead of panicking when the device is gone.
|
||||
d.health.probe(&d.device);
|
||||
let validity =
|
||||
if d.pending.iter().any(|p| p.state.lock().map(|state| state.is_none()).unwrap_or(true)) { d.check_device_valid() } else { Ok(()) };
|
||||
// The idle validity check includes the poll.
|
||||
let validity = if d.pending.is_empty() {
|
||||
d.check_device_valid()
|
||||
} else {
|
||||
d.health.probe(&d.device);
|
||||
if d.pending.iter().any(|p| p.state.lock().map(|state| state.is_none()).unwrap_or(true)) {
|
||||
// Waiting reads need a validity check, which polls again.
|
||||
d.check_device_valid()
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
};
|
||||
let fault = d.health.polling_fault().map(GpuFault::from).or_else(|| validity.err());
|
||||
if let Some(fault) = fault {
|
||||
let fault = d.prefer_device_loss(fault);
|
||||
|
||||
@@ -677,6 +677,35 @@ mod device_from_craft_gpu_device {
|
||||
assert!(d.pending.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_upload_rechecks_out_of_memory_reported_during_the_validity_probe() {
|
||||
let opened = opened();
|
||||
let mut ctx = WgpuContext::from_opened(WorkerKind::Main, &opened);
|
||||
let health = opened.health.clone();
|
||||
opened.queue.on_submitted_work_done(move || {
|
||||
health.mark(Fault::Error(craft_gpu_device::OUT_OF_MEMORY.into()));
|
||||
});
|
||||
assert_eq!(opened.health.fault(), None);
|
||||
|
||||
assert_eq!(ctx.upload_buffer("during", b"abcd"), Err(GpuFault::Operation(craft_gpu_device::OUT_OF_MEMORY.into())));
|
||||
assert!(opened.health.fault().is_some_and(|fault| fault.is_out_of_memory()));
|
||||
let Backing::Device(d) = &ctx.backing else { panic!("a device backing") };
|
||||
assert!(d.buffers.is_empty(), "no handle is kept for the failed upload");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn polling_with_no_pending_read_detects_destruction_before_callback_delivery() {
|
||||
let opened = opened();
|
||||
let mut ctx = WgpuContext::from_opened(WorkerKind::Main, &opened);
|
||||
opened.device.set_device_lost_callback(|_, _| {});
|
||||
opened.device.destroy();
|
||||
assert_eq!(opened.health.device_status(), Ok(()));
|
||||
|
||||
assert!(ctx.poll_reads().is_empty());
|
||||
assert!(opened.health.device_lost());
|
||||
assert!(matches!(ctx.device_status(), Err(GpuFault::DeviceLost(_))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_read_preserves_out_of_memory_precedence_after_the_validity_probe() {
|
||||
for oom_first in [true, false] {
|
||||
@@ -901,6 +930,37 @@ mod device_from_craft_gpu_device {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_read_completed_during_poll_survives_destruction_by_a_queue_callback() {
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
|
||||
let opened = opened();
|
||||
let mut ctx = WgpuContext::from_opened(WorkerKind::Main, &opened);
|
||||
let handle = ctx.upload_buffer("pending", b"abcd").expect("upload");
|
||||
let request = ctx.request_read(handle).expect("read");
|
||||
let Backing::Device(d) = &ctx.backing else { panic!("a device backing") };
|
||||
let state = Arc::clone(&d.pending[0].state);
|
||||
assert!(state.lock().expect("map state").is_none());
|
||||
opened.device.set_device_lost_callback(|_, _| {});
|
||||
let destroyed = Arc::new(AtomicBool::new(false));
|
||||
let flag = Arc::clone(&destroyed);
|
||||
let device = opened.device.clone();
|
||||
opened.queue.on_submitted_work_done(move || {
|
||||
assert!(matches!(*state.lock().expect("map state"), Some(Ok(()))));
|
||||
device.destroy();
|
||||
flag.store(true, Ordering::Release);
|
||||
});
|
||||
assert!(!destroyed.load(Ordering::Acquire));
|
||||
|
||||
let outcomes = ctx.poll_reads();
|
||||
assert!(destroyed.load(Ordering::Acquire));
|
||||
assert_eq!(outcomes.len(), 1);
|
||||
assert_eq!(outcomes[0].request, request);
|
||||
assert_eq!(outcomes[0].handle, handle);
|
||||
assert_eq!(outcomes[0].result, Ok(b"abcd".to_vec()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn polling_pending_reads_prefers_loss_before_callback_delivery() {
|
||||
let opened = opened();
|
||||
|
||||
Reference in new issue
Block a user