Skip to content

Commit

Permalink
Prevent another deadlock. (#5201)
Browse files Browse the repository at this point in the history
  • Loading branch information
nical authored Feb 5, 2024
1 parent b7b7f7d commit 32e70bc
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions wgpu-core/src/device/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,15 +372,19 @@ impl<A: HalApi> Device<A> {

let mapping_closures = life_tracker.handle_mapping(self.raw(), &self.trackers);

let queue_empty = life_tracker.queue_empty();

// Detect if we have been destroyed and now need to lose the device.
// If we are invalid (set at start of destroy) and our queue is empty,
// and we have a DeviceLostClosure, return the closure to be called by
// our caller. This will complete the steps for both destroy and for
// "lose the device".
let mut device_lost_invocations = SmallVec::new();
if !self.is_valid() && life_tracker.queue_empty() {
// We can release gpu resources associated with this device.
self.release_gpu_resources();
let mut should_release_gpu_resource = false;
if !self.is_valid() && queue_empty {
// We can release gpu resources associated with this device (but not
// while holding the life_tracker lock).
should_release_gpu_resource = true;

// If we have a DeviceLostClosure, build an invocation with the
// reason DeviceLostReason::Destroyed and no message.
Expand All @@ -393,12 +397,19 @@ impl<A: HalApi> Device<A> {
}
}

// Don't hold the lock while calling release_gpu_resources.
drop(life_tracker);

if should_release_gpu_resource {
self.release_gpu_resources();
}

let closures = UserClosures {
mappings: mapping_closures,
submissions: submission_closures,
device_lost_invocations,
};
Ok((closures, life_tracker.queue_empty()))
Ok((closures, queue_empty))
}

pub(crate) fn untrack(&self, trackers: &Tracker<A>) {
Expand Down

0 comments on commit 32e70bc

Please sign in to comment.