-
Notifications
You must be signed in to change notification settings - Fork 984
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CommandEncoder::finish
sometimes panics unexpectedly when ComputePass
is not explicitly dropped beforehand
#6145
Comments
If the cause is exactly as described, perhaps a simple fix for this would be to add: impl Drop for ComputePass { fn drop(&mut self) {} }
impl Drop for RenderPass { fn drop(&mut self) {} } This should require the lifetime to be valid when the pass is dropped. |
I can confirm the crash on my Win11 machine. The workaround typically shown in examples right now is to scope the --- a/src/main.rs
+++ b/src/main.rs
@@ -5,8 +5,10 @@
pollster::block_on(adapter.request_device(&Default::default(), None)).unwrap();
let mut encoder = device.create_command_encoder(&Default::default());
- let compute_pass = encoder.begin_compute_pass(&Default::default());
- // set up the compute pass...
+ {
+ let compute_pass = encoder.begin_compute_pass(&Default::default());
+ // set up the compute pass...
+ }
let cmd_buffer = encoder.finish();
assert!(device …but, of course, this is still a paper cut we should strive to eliminate in our Rust-y interpretation of the WebGPU API. |
I interpret @kpreid's suggestion to mean a diff. like this against current diff --git a/wgpu-core/src/command/compute.rs b/wgpu-core/src/command/compute.rs
index eb929740c8..f216d1b40f 100644
--- a/wgpu-core/src/command/compute.rs
+++ b/wgpu-core/src/command/compute.rs
@@ -95,6 +95,10 @@
}
}
+impl Drop for ComputePass {
+ fn drop(&mut self) {}
+}
+
#[derive(Clone, Debug, Default)]
pub struct ComputePassDescriptor<'a> {
pub label: Label<'a>,
diff --git a/wgpu-core/src/command/render.rs b/wgpu-core/src/command/render.rs
index e4d93b042e..efc80d7475 100644
--- a/wgpu-core/src/command/render.rs
+++ b/wgpu-core/src/command/render.rs
@@ -321,6 +321,10 @@
}
}
+impl Drop for RenderPass {
+ fn drop(&mut self) {}
+}
+
#[derive(Debug, PartialEq)]
enum OptionalState {
Unused, …but this doesn't change any behavior in the MRE posted in the OP. |
They do still have lifetimes to their encoder. No, the RenderPass drop needs to be added in |
Ah, I see, I should have checked before writing that :) |
Going to tackle this as part of #6619. I need to write a test to make sure I didn't break anything anyway. |
Description
Borrow shortening can cause
CommandEncoder::finish
to be callable before a derivedComputePass
is automatically dropped. Since theDrop
impl onComputePassInner
is what unlocks theCommandEncoder
, this leads to an unexpected validation error, which can be avoided by explicitly dropping of theComputePass
before callingCommandEncoder::finish
.The same likely applies to
RenderPass
.Repro steps
Expected vs observed behavior
A panic occurs at line 12:
Since
ComputePass
's lifetime is bounded by a mutable borrow of theCommandEncoder
, one would expect theCommandEncoder
to be unlocked and a call tofinish
to be valid as soon as said borrow is invalidated. This is, however, not the case, since theDrop
impl ofComputePassInner
is only run at the end of scope, not as soon as the borrow is invalidated.The documentation of
CommandEncoder::begin_compute_pass
does contain the following line:As long as the returned ComputePass has not ended, any mutating operation on this command encoder causes an error and invalidates it.
. It does not specify that "a compute pass ending" specifically means "theComputePass
being dropped".Platform
wgpu = "22.1.0"
The text was updated successfully, but these errors were encountered: