mirror of
https://github.com/storytold/craft-libs.git
synced 2026-12-01 10:48:15 +01:00
GPU: pending reads check device validity between polls and wait slices
A destroyed shared device can defer its loss callback until the queue drains, so poll_reads and the blocking wait in read_buffer now run the device validity check while a read's map has not completed, and fail the pending reads with GpuFault::DeviceLost. request_read also returns the recorded work fault before a non-loss error from its validity probe, so out of memory keeps its precedence.
This commit is contained in:
3 files changed
+142
-34
No files matched your search
@@ -312,33 +312,42 @@ impl DeviceHealth {
|
||||
/// loss. Native only: the web has no blocking wait.
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
pub fn wait(&self, device: &wgpu::Device, index: wgpu::SubmissionIndex) -> bool {
|
||||
self.wait_with_check(device, index, || true)
|
||||
}
|
||||
|
||||
/// This method calls `check` before each [`Self::wait`] slice and stops if it returns false.
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
pub fn wait_with_check(&self, device: &wgpu::Device, index: wgpu::SubmissionIndex, check: impl FnMut() -> bool) -> bool {
|
||||
const SLICE: Duration = Duration::from_millis(100);
|
||||
self.wait_unbounded(|| {
|
||||
let poll = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
|
||||
device.poll(wgpu::PollType::Wait { submission_index: Some(index.clone()), timeout: Some(SLICE) })
|
||||
}));
|
||||
match poll {
|
||||
Ok(Ok(_)) => SlicePoll::Done,
|
||||
Ok(Err(wgpu::PollError::Timeout)) => SlicePoll::Pending,
|
||||
Ok(Err(e)) => {
|
||||
log::warn!("wgpu poll: {e}");
|
||||
SlicePoll::Failed
|
||||
self.wait_unbounded(
|
||||
|| {
|
||||
let poll = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
|
||||
device.poll(wgpu::PollType::Wait { submission_index: Some(index.clone()), timeout: Some(SLICE) })
|
||||
}));
|
||||
match poll {
|
||||
Ok(Ok(_)) => SlicePoll::Done,
|
||||
Ok(Err(wgpu::PollError::Timeout)) => SlicePoll::Pending,
|
||||
Ok(Err(e)) => {
|
||||
log::warn!("wgpu poll: {e}");
|
||||
SlicePoll::Failed
|
||||
}
|
||||
Err(_) => {
|
||||
self.lost_by_poll();
|
||||
SlicePoll::Failed
|
||||
}
|
||||
}
|
||||
Err(_) => {
|
||||
self.lost_by_poll();
|
||||
SlicePoll::Failed
|
||||
}
|
||||
}
|
||||
})
|
||||
},
|
||||
check,
|
||||
)
|
||||
}
|
||||
|
||||
/// [`Self::wait`]'s loop: `poll` runs one slice and reports what wgpu returned. The loop ends
|
||||
/// when a slice finished or failed, and before the next slice once a clone of this flag
|
||||
/// recorded a loss or an error other than out of memory.
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn wait_unbounded(&self, mut poll: impl FnMut() -> SlicePoll) -> bool {
|
||||
fn wait_unbounded(&self, mut poll: impl FnMut() -> SlicePoll, mut check: impl FnMut() -> bool) -> bool {
|
||||
loop {
|
||||
if !self.pollable() {
|
||||
if !self.pollable() || !check() || !self.pollable() {
|
||||
return false;
|
||||
}
|
||||
match poll() {
|
||||
@@ -657,26 +666,60 @@ mod tests {
|
||||
let h = DeviceHealth::new();
|
||||
let other = h.clone();
|
||||
let mut polls = 0;
|
||||
let finished = h.wait_unbounded(|| {
|
||||
polls += 1;
|
||||
if polls == 2 {
|
||||
other.mark(Fault::Error("Validation Error".into()));
|
||||
}
|
||||
assert!(polls <= 2, "a slice ran after the fault");
|
||||
SlicePoll::Pending
|
||||
});
|
||||
let finished = h.wait_unbounded(
|
||||
|| {
|
||||
polls += 1;
|
||||
if polls == 2 {
|
||||
other.mark(Fault::Error("Validation Error".into()));
|
||||
}
|
||||
assert!(polls <= 2, "a slice ran after the fault");
|
||||
SlicePoll::Pending
|
||||
},
|
||||
|| true,
|
||||
);
|
||||
assert!(!finished);
|
||||
assert_eq!(polls, 2);
|
||||
// out of memory alone keeps the wait going until the work finishes
|
||||
let oom = DeviceHealth::new();
|
||||
oom.mark(Fault::Error(OUT_OF_MEMORY.into()));
|
||||
let mut polls = 0;
|
||||
assert!(oom.wait_unbounded(|| {
|
||||
polls += 1;
|
||||
if polls < 3 { SlicePoll::Pending } else { SlicePoll::Done }
|
||||
}));
|
||||
assert!(oom.wait_unbounded(
|
||||
|| {
|
||||
polls += 1;
|
||||
if polls < 3 { SlicePoll::Pending } else { SlicePoll::Done }
|
||||
},
|
||||
|| true,
|
||||
));
|
||||
assert_eq!(polls, 3);
|
||||
assert!(!DeviceHealth::new().wait_unbounded(|| SlicePoll::Failed));
|
||||
assert!(!DeviceHealth::new().wait_unbounded(|| SlicePoll::Failed, || true));
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn a_validity_check_between_wait_slices_stops_pending_work() {
|
||||
for fault in [None, Some(Fault::Lost("destroyed".into())), Some(Fault::Error("Validation Error".into()))] {
|
||||
let health = DeviceHealth::new();
|
||||
let polls = std::cell::Cell::new(0);
|
||||
let finished = health.wait_unbounded(
|
||||
|| {
|
||||
polls.set(polls.get() + 1);
|
||||
if polls.get() == 1 { SlicePoll::Pending } else { SlicePoll::Done }
|
||||
},
|
||||
|| {
|
||||
if polls.get() == 0 {
|
||||
return true;
|
||||
}
|
||||
if let Some(fault) = &fault {
|
||||
health.mark(fault.clone());
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
},
|
||||
);
|
||||
assert!(!finished);
|
||||
assert_eq!(polls.get(), 1);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -398,7 +398,9 @@ impl WgpuContext {
|
||||
#[cfg(feature = "adapter-probe")]
|
||||
if let Backing::Device(d) = &mut self.backing
|
||||
&& (d.health.polling_fault().is_some()
|
||||
|| d.pending.iter().find(|p| p.request == request).is_some_and(|p| !d.health.wait(&d.device, p.submission.clone())))
|
||||
|| d.pending.iter().find(|p| p.request == request).is_some_and(|p| {
|
||||
!d.health.wait_with_check(&d.device, p.submission.clone(), || !matches!(d.check_device_valid(), Err(GpuFault::DeviceLost(_))))
|
||||
}))
|
||||
{
|
||||
d.health.probe(&d.device);
|
||||
if d.health.device_status().is_err() {
|
||||
@@ -555,10 +557,14 @@ impl GpuContext for WgpuContext {
|
||||
.get(handle.0 as usize)
|
||||
.and_then(Option::as_ref)
|
||||
.ok_or_else(|| d.prefer_device_loss(GpuFault::InvalidJob("bad handle".into())))?;
|
||||
d.check_device_valid()?;
|
||||
let validity = d.check_device_valid();
|
||||
if let Err(loss @ GpuFault::DeviceLost(_)) = validity {
|
||||
return Err(loss);
|
||||
}
|
||||
if let Some(fault) = self.take_work_fault() {
|
||||
return Err(fault);
|
||||
}
|
||||
validity?;
|
||||
}
|
||||
match &mut self.backing {
|
||||
Backing::Staging(buffers) => {
|
||||
@@ -672,7 +678,9 @@ impl GpuContext for WgpuContext {
|
||||
// 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 fault = d.health.polling_fault().map(GpuFault::from);
|
||||
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(()) };
|
||||
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);
|
||||
outcomes.extend(d.pending.drain(..).map(|p| ReadOutcome { request: p.request, handle: p.handle, result: Err(fault.clone()) }));
|
||||
|
||||
@@ -649,6 +649,30 @@ mod device_from_craft_gpu_device {
|
||||
assert!(d.pending.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_read_preserves_out_of_memory_precedence_after_the_validity_probe() {
|
||||
for oom_first in [true, false] {
|
||||
let opened = opened();
|
||||
let mut ctx = WgpuContext::from_opened(WorkerKind::Main, &opened);
|
||||
let handle = ctx.upload_buffer("before", b"abcd").expect("upload");
|
||||
let health = opened.health.clone();
|
||||
opened.queue.on_submitted_work_done(move || {
|
||||
let faults = [Fault::Error(craft_gpu_device::OUT_OF_MEMORY.into()), Fault::Error("Validation Error".into())];
|
||||
for index in if oom_first { [0, 1] } else { [1, 0] } {
|
||||
health.mark(faults[index].clone());
|
||||
}
|
||||
});
|
||||
assert_eq!(opened.health.fault(), None);
|
||||
let next_request = ctx.next_request;
|
||||
|
||||
assert_eq!(ctx.request_read(handle), Err(GpuFault::Operation(craft_gpu_device::OUT_OF_MEMORY.into())));
|
||||
assert_eq!(ctx.next_request, next_request);
|
||||
assert!(ctx.ready.is_empty());
|
||||
let Backing::Device(d) = &ctx.backing else { panic!("a device backing") };
|
||||
assert!(d.pending.is_empty());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn new_work_preserves_out_of_memory_precedence_over_validation_errors() {
|
||||
for oom_first in [true, false] {
|
||||
@@ -816,6 +840,39 @@ mod device_from_craft_gpu_device {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pending_reads_detect_destruction_without_map_or_loss_callbacks() {
|
||||
for wait in [false, true] {
|
||||
let opened = opened();
|
||||
let mut ctx = WgpuContext::from_opened(WorkerKind::Main, &opened);
|
||||
let handle = ctx.upload_buffer("pending", b"abcd").expect("upload");
|
||||
let first = ctx.request_read(handle).expect("first read");
|
||||
let second = ctx.request_read(handle).expect("second read");
|
||||
let Backing::Device(d) = &mut ctx.backing else { panic!("a device backing") };
|
||||
for pending in &mut d.pending {
|
||||
pending.state = std::sync::Arc::new(std::sync::Mutex::new(None));
|
||||
}
|
||||
opened.device.set_device_lost_callback(|_, _| {});
|
||||
opened.device.destroy();
|
||||
assert_eq!(opened.health.fault(), None);
|
||||
|
||||
if wait {
|
||||
assert_eq!(ctx.wait_between_reads(first), None);
|
||||
assert!(opened.health.device_lost());
|
||||
}
|
||||
let outcomes = ctx.poll_reads();
|
||||
assert_eq!(outcomes.len(), 2);
|
||||
assert!(outcomes.iter().any(|outcome| outcome.request == first));
|
||||
assert!(outcomes.iter().any(|outcome| outcome.request == second));
|
||||
for outcome in outcomes {
|
||||
assert_eq!(outcome.handle, handle);
|
||||
assert!(matches!(outcome.result, Err(GpuFault::DeviceLost(_))));
|
||||
}
|
||||
assert!(opened.health.device_lost());
|
||||
assert!(ctx.poll_reads().is_empty());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn polling_pending_reads_prefers_loss_before_callback_delivery() {
|
||||
let opened = opened();
|
||||
|
||||
Reference in new issue
Block a user