Skip to content

Commit

Permalink
wait for the last successful submission to complete before dropping t…
Browse files Browse the repository at this point in the history
…he `Queue`
  • Loading branch information
teoxoy committed Oct 15, 2024
1 parent 312114a commit 59c81c2
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions wgpu-core/src/device/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,50 @@ crate::impl_storage_item!(Queue);
impl Drop for Queue {
fn drop(&mut self) {
resource_log!("Drop {}", self.error_ident());

let snatch_guard = self.device.snatchable_lock.read();
let fence = self.device.fence.read();

let last_successful_submission_index = self
.device
.last_successful_submission_index
.load(Ordering::Acquire);

let wait_res = unsafe {
self.device.raw().wait(
fence.as_ref(),
last_successful_submission_index,
crate::device::CLEANUP_WAIT_MS,
)
};
drop(fence);

match wait_res {
Ok(true) => {}
// Note: If we don't panic here we are in UB land (destroying resources while they are still in use by the GPU).
Ok(false) => {
panic!("We timed out while waiting on the last successful submission to complete!");
}
Err(e) => {
panic!(
"We ran into an error while waiting on the last successful submission to complete! - {e}"
);
}
}

let (submission_closures, mapping_closures, queue_empty) =
self.maintain(last_successful_submission_index, &snatch_guard);
drop(snatch_guard);

assert!(queue_empty);

let closures = crate::device::UserClosures {
mappings: mapping_closures,
submissions: submission_closures,
device_lost_invocations: SmallVec::new(),
};
closures.fire();

// SAFETY: We are in the Drop impl and we don't use self.pending_writes anymore after this point.
let pending_writes = unsafe { ManuallyDrop::take(&mut self.pending_writes.lock()) };
pending_writes.dispose(self.device.raw());
Expand Down

0 comments on commit 59c81c2

Please sign in to comment.