File tree Expand file tree Collapse file tree 1 file changed +14
-2
lines changed
Expand file tree Collapse file tree 1 file changed +14
-2
lines changed Original file line number Diff line number Diff 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 ( ) ;
You can’t perform that action at this time.
0 commit comments