Skip to content

Commit 17ebb2a

Browse files
authored
[core] Queue::drop is acquiring the snatch guard as well (#8691)
1 parent cc005da commit 17ebb2a

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

wgpu-core/src/device/resource.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,20 @@ impl Device {
854854
user_closures.mappings,
855855
user_closures.blas_compact_ready,
856856
queue_empty,
857-
) = queue_result
857+
) = queue_result;
858+
// DEADLOCK PREVENTION: We must drop `snatch_guard` before `queue` goes out of scope.
859+
//
860+
// `Queue::drop` acquires the snatch guard. If we still hold it when `queue` is dropped
861+
// at the end of this block, we would deadlock. This can happen in the following scenario:
862+
//
863+
// - Thread A calls `Device::maintain` while Thread B holds the last strong ref to the queue.
864+
// - Thread A calls `self.get_queue()`, obtaining a new strong ref, and enters this branch.
865+
// - Thread B drops its strong ref, making Thread A's ref the last one.
866+
// - When `queue` goes out of scope here, `Queue::drop` runs and tries to acquire the
867+
// snatch guard — but Thread A (this thread) still holds it, causing a deadlock.
868+
drop(snatch_guard);
869+
} else {
870+
drop(snatch_guard);
858871
};
859872

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

910923
// Don't hold the locks while calling release_gpu_resources.
911924
drop(fence);
912-
drop(snatch_guard);
913925

914926
if should_release_gpu_resource {
915927
self.release_gpu_resources();

0 commit comments

Comments
 (0)