fix(metal): Fix hang waiting for an errored command buffer

Fixes #9531
This commit is contained in:
39ali authored and Andy Leiserson committed 2026-06-04 17:11:59 -07:00
1 parent e0a6065c65
commit 6877690dbe
4 files changed
+120 -16

No files matched your search

+1
View File
@@ -244,6 +244,7 @@ By @beholdnec in [#8505](https://github.com/gfx-rs/wgpu/pull/8505).
- Fix crash on fence creation when running in a MacOS Seatbelt sandbox. By @wumpf in [#9415](https://github.com/gfx-rs/wgpu/pull/9415)
- Improved command buffer completion handling. By @39ali in [#9328](https://github.com/gfx-rs/wgpu/pull/9328).
- Wait using a condition variable, instead of polling.
- Fixed a hang in `Device::poll(PollType::wait_indefinitely())` when a Metal command buffer exits with an error.
- Fixed structure field names incorrectly ignoring reserved keywords in the Metal (MSL) backend. By @39ali [#9379](https://github.com/gfx-rs/wgpu/pull/9379).
- Restore the `Queue::as_raw` method, which was removed without good reason in v29. It now returns `&ProtocolObject<dyn MTLCommandQueue>`. By @andyleiserson in [#9560](https://github.com/gfx-rs/wgpu/pull/9560).
+109 -3
View File
@@ -1,18 +1,20 @@
use std::{num::NonZeroU64, time::Duration};
use wgpu::{
BindGroupDescriptor, BindGroupEntry, BindGroupLayoutDescriptor, BindGroupLayoutEntry,
Backends, BindGroupDescriptor, BindGroupEntry, BindGroupLayoutDescriptor, BindGroupLayoutEntry,
BindingResource, BindingType, BufferBindingType, BufferDescriptor, BufferUsages, CommandBuffer,
CommandEncoderDescriptor, ComputePassDescriptor, PollType, ShaderStages,
CommandEncoderDescriptor, ComputePassDescriptor, ComputePipelineDescriptor,
PipelineLayoutDescriptor, PollType, ShaderModuleDescriptor, ShaderSource, ShaderStages,
};
use wgpu_test::{
gpu_test, GpuTestConfiguration, GpuTestInitializer, TestParameters, TestingContext,
gpu_test, FailureCase, GpuTestConfiguration, GpuTestInitializer, TestParameters, TestingContext,
};
pub fn all_tests(vec: &mut Vec<GpuTestInitializer>) {
vec.extend([
WAIT,
WAIT_INDEFINITELY_LONG_RUNNING,
WAIT_WITH_TIMEOUT,
WAIT_WITH_TIMEOUT_MAX,
DOUBLE_WAIT,
@@ -85,6 +87,110 @@ static WAIT: GpuTestConfiguration = GpuTestConfiguration::new()
.unwrap();
});
/// Regression test for <https://github.com/gfx-rs/wgpu/issues/9531>. In common
/// configurations, Metal will terminate this command buffer due to "impacting
/// interactivity". Depending on which wait-for-completion code path was used, `wgpu` could
/// previously hang waiting for `MTLCommandBufferStatus::Completed`, when the actual status
/// was the terminal `MTLCommandBufferStatus::Error`.
///
/// At present this test expects no hang. <https://github.com/gfx-rs/wgpu/issues/9545>
/// proposes propagating the failure by losing the device. If that is implemented, this test
/// should be updated accordingly. Care may be needed to avoid flakiness in cases where no
/// termination occurs.
#[gpu_test]
static WAIT_INDEFINITELY_LONG_RUNNING: GpuTestConfiguration = GpuTestConfiguration::new()
.parameters(
TestParameters::default()
.test_features_limits()
.skip(FailureCase::backend(!Backends::METAL)),
)
.run_async(|ctx| async move {
const SHADER: &str = r#"
@group(0) @binding(0) var<storage, read_write> buf: array<u32>;
@compute @workgroup_size(64)
fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
var x: u32 = gid.x ^ 0xDEADBEEFu;
for (var i: u32 = 0u; i < 5000000u; i++) {
x ^= x << 13u;
x ^= x >> 17u;
x ^= x << 5u;
}
buf[gid.x] = x;
}
"#;
const N_THREADS: u32 = 1024 * 64;
let module = ctx.device.create_shader_module(ShaderModuleDescriptor {
label: None,
source: ShaderSource::Wgsl(SHADER.into()),
});
let buffer = ctx.device.create_buffer(&BufferDescriptor {
label: None,
size: (N_THREADS as u64) * 4,
usage: BufferUsages::STORAGE,
mapped_at_creation: false,
});
let bind_group_layout = ctx
.device
.create_bind_group_layout(&BindGroupLayoutDescriptor {
label: None,
entries: &[BindGroupLayoutEntry {
binding: 0,
visibility: ShaderStages::COMPUTE,
ty: BindingType::Buffer {
ty: BufferBindingType::Storage { read_only: false },
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
}],
});
let pipeline_layout = ctx
.device
.create_pipeline_layout(&PipelineLayoutDescriptor {
label: None,
bind_group_layouts: &[Some(&bind_group_layout)],
immediate_size: 0,
});
let pipeline = ctx
.device
.create_compute_pipeline(&ComputePipelineDescriptor {
label: None,
layout: Some(&pipeline_layout),
module: &module,
entry_point: Some("main"),
compilation_options: Default::default(),
cache: None,
});
let bind_group = ctx.device.create_bind_group(&BindGroupDescriptor {
label: None,
layout: &bind_group_layout,
entries: &[BindGroupEntry {
binding: 0,
resource: buffer.as_entire_binding(),
}],
});
let mut encoder = ctx
.device
.create_command_encoder(&CommandEncoderDescriptor::default());
{
let mut cpass = encoder.begin_compute_pass(&ComputePassDescriptor::default());
cpass.set_pipeline(&pipeline);
cpass.set_bind_group(0, &bind_group, &[]);
cpass.dispatch_workgroups(N_THREADS / 64, 1, 1);
}
ctx.queue.submit(Some(encoder.finish()));
// TODO(https://github.com/gfx-rs/wgpu/issues/9545): `wgpu` should raise an error
// for the terminated command buffer (e.g. lose the device), and this test should
// check for the expected error. Care may be needed to avoid flakiness in cases
// where no termination occurs.
ctx.async_poll(PollType::wait_indefinitely()).await.unwrap();
});
#[gpu_test]
static WAIT_WITH_TIMEOUT: GpuTestConfiguration = GpuTestConfiguration::new()
.parameters(TestParameters::default().enable_noop())
+5 -11
View File
@@ -1922,19 +1922,13 @@ impl crate::Device for super::Device {
}
}
if let Some(deadline) =
timeout.and_then(|timeout| std::time::Instant::now().checked_add(timeout))
{
while *lock < wait_value {
let result = condvar.wait_until(&mut lock, deadline);
if result.timed_out() {
return Ok(*lock >= wait_value);
}
if let Some(timeout) = timeout {
let result = condvar.wait_while_for(&mut lock, |value| *value < wait_value, timeout);
if result.timed_out() {
return Ok(*lock >= wait_value);
}
} else {
while *lock < wait_value {
condvar.wait(&mut lock);
}
condvar.wait_while(&mut lock, |value| *value < wait_value);
}
Ok(true)
+5 -2
View File
@@ -1050,8 +1050,11 @@ impl Fence {
let mut max_value = *self.sync.0.lock();
let pending_command_buffers = self.pending_command_buffers.read();
for &(value, ref cmd_buf) in pending_command_buffers.iter() {
if cmd_buf.status() == MTLCommandBufferStatus::Completed {
max_value = value;
match cmd_buf.status() {
MTLCommandBufferStatus::Completed | MTLCommandBufferStatus::Error => {
max_value = value;
}
_ => {}
}
}
max_value