Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions wgpu-core/src/device/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,20 @@ impl Device {
user_closures.mappings,
user_closures.blas_compact_ready,
queue_empty,
) = queue_result
) = queue_result;
// DEADLOCK PREVENTION: We must drop `snatch_guard` before `queue` goes out of scope.
//
// `Queue::drop` acquires the snatch guard. If we still hold it when `queue` is dropped
// at the end of this block, we would deadlock. This can happen in the following scenario:
//
// - Thread A calls `Device::maintain` while Thread B holds the last strong ref to the queue.
// - Thread A calls `self.get_queue()`, obtaining a new strong ref, and enters this branch.
// - Thread B drops its strong ref, making Thread A's ref the last one.
// - When `queue` goes out of scope here, `Queue::drop` runs and tries to acquire the
// snatch guard — but Thread A (this thread) still holds it, causing a deadlock.
drop(snatch_guard);
} else {
drop(snatch_guard);
};

// Based on the queue empty status, and the current finished submission index, determine the result of the poll.
Expand Down Expand Up @@ -909,7 +922,6 @@ impl Device {

// Don't hold the locks while calling release_gpu_resources.
drop(fence);
drop(snatch_guard);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: I think it's fine—but I'm not positive—to no longer be locking the snatch lock when we take the device lost closure. Tagging in @cwfitzgerald to double-check my conclusion here. Does that sound right, Connor?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see any reason why this would be problematic. The snatch lock doesn't guard the device lost closure in any way.


if should_release_gpu_resource {
self.release_gpu_resources();
Expand Down
Loading