From 6877690dbefa1144c05932dde2d10c52facfee60 Mon Sep 17 00:00:00 2001 From: 39ali Date: Tue, 2 Jun 2026 09:47:58 +0300 Subject: [PATCH] fix(metal): Fix hang waiting for an errored command buffer Fixes #9531 --- CHANGELOG.md | 1 + tests/tests/wgpu-gpu/poll.rs | 112 ++++++++++++++++++++++++++++++++++- wgpu-hal/src/metal/device.rs | 16 ++--- wgpu-hal/src/metal/mod.rs | 7 ++- 4 files changed, 120 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ffde1f74..d5d770eff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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`. By @andyleiserson in [#9560](https://github.com/gfx-rs/wgpu/pull/9560). diff --git a/tests/tests/wgpu-gpu/poll.rs b/tests/tests/wgpu-gpu/poll.rs index 45aa199f5..08035db6a 100644 --- a/tests/tests/wgpu-gpu/poll.rs +++ b/tests/tests/wgpu-gpu/poll.rs @@ -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) { 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 . 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. +/// 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 buf: array; + +@compute @workgroup_size(64) +fn main(@builtin(global_invocation_id) gid: vec3) { + 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()) diff --git a/wgpu-hal/src/metal/device.rs b/wgpu-hal/src/metal/device.rs index e93235f36..72bb487f1 100644 --- a/wgpu-hal/src/metal/device.rs +++ b/wgpu-hal/src/metal/device.rs @@ -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) diff --git a/wgpu-hal/src/metal/mod.rs b/wgpu-hal/src/metal/mod.rs index 7a874f019..bb3a22061 100644 --- a/wgpu-hal/src/metal/mod.rs +++ b/wgpu-hal/src/metal/mod.rs @@ -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