From f64eae77341c04d3632aa9e99eb69e6272eb589d Mon Sep 17 00:00:00 2001 From: RedMindZ Date: Mon, 21 Apr 2025 18:08:57 +0300 Subject: [PATCH] Fixed a deadlock caused by locking the device's snatchable lock **after** locking the queue's pending writes (#7582) --- wgpu-core/src/device/queue.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/wgpu-core/src/device/queue.rs b/wgpu-core/src/device/queue.rs index 585c1a7b5..7e868b909 100644 --- a/wgpu-core/src/device/queue.rs +++ b/wgpu-core/src/device/queue.rs @@ -476,6 +476,8 @@ impl Queue { return Ok(()); }; + let snatch_guard = self.device.snatchable_lock.read(); + // Platform validation requires that the staging buffer always be // freed, even if an error occurs. All paths from here must call // `device.pending_writes.consume`. @@ -489,6 +491,7 @@ impl Queue { }; let result = self.write_staging_buffer_impl( + &snatch_guard, &mut pending_writes, &staging_buffer, buffer, @@ -522,6 +525,7 @@ impl Queue { let buffer = buffer.get()?; + let snatch_guard = self.device.snatchable_lock.read(); let mut pending_writes = self.pending_writes.lock(); // At this point, we have taken ownership of the staging_buffer from the @@ -531,6 +535,7 @@ impl Queue { let staging_buffer = staging_buffer.flush(); let result = self.write_staging_buffer_impl( + &snatch_guard, &mut pending_writes, &staging_buffer, buffer, @@ -583,6 +588,7 @@ impl Queue { fn write_staging_buffer_impl( &self, + snatch_guard: &SnatchGuard, pending_writes: &mut PendingWrites, staging_buffer: &FlushedStagingBuffer, buffer: Arc, @@ -595,8 +601,7 @@ impl Queue { .set_single(&buffer, wgt::BufferUses::COPY_DST) }; - let snatch_guard = self.device.snatchable_lock.read(); - let dst_raw = buffer.try_raw(&snatch_guard)?; + let dst_raw = buffer.try_raw(snatch_guard)?; self.same_device_as(buffer.as_ref())?; @@ -614,7 +619,7 @@ impl Queue { to: wgt::BufferUses::COPY_SRC, }, }) - .chain(transition.map(|pending| pending.into_hal(&buffer, &snatch_guard))) + .chain(transition.map(|pending| pending.into_hal(&buffer, snatch_guard))) .collect::>(); let encoder = pending_writes.activate(); unsafe {