From aba219f91098311fe4c83d9c12d2158f45f3c1f3 Mon Sep 17 00:00:00 2001 From: Andy Leiserson Date: Wed, 6 May 2026 11:03:00 -0700 Subject: [PATCH] test: Check for device loss (#9477) --- tests/src/expectations.rs | 17 ++++++++++++++ tests/src/init.rs | 22 ++++++++++++++++--- tests/tests/wgpu-gpu/clip_distances.rs | 2 +- tests/tests/wgpu-gpu/draw_index.rs | 2 +- tests/tests/wgpu-gpu/oob_indexing.rs | 2 +- tests/tests/wgpu-gpu/per_vertex/mod.rs | 2 +- .../wgpu-gpu/shader/array_size_overrides.rs | 2 +- .../shader/zero_init_workgroup_mem.rs | 2 +- 8 files changed, 42 insertions(+), 9 deletions(-) diff --git a/tests/src/expectations.rs b/tests/src/expectations.rs index 50972cf69..60f76bc88 100644 --- a/tests/src/expectations.rs +++ b/tests/src/expectations.rs @@ -201,6 +201,23 @@ impl FailureCase { self } + /// Matches this failure case against an unexpected driver error. + /// + /// Depending on build configuration, the error may surface as either a + /// panic raised by the `internal_error_panic` feature (with the supplied + /// message as a substring), or as a device-loss. Either behavior is + /// accepted. In the device loss case, the original message has been + /// discarded, and there is some risk this accepts a different error than + /// intended (but it must be a device loss due to an unexpected driver + /// error, which should be rare). + pub fn unexpected_error(mut self, msg: &'static str) -> Self { + self.reasons.push(FailureReason::panic().with_message(msg)); + self.reasons.push(FailureReason::panic().with_message( + "Device lost: Unexpected error variant (driver implementation is at fault)", + )); + self + } + /// Test is flaky with the given configuration. Do not assert failure. /// /// Use this _very_ sparyingly, and match as tightly as you can, including giving a specific failure message. diff --git a/tests/src/init.rs b/tests/src/init.rs index a848bab55..9ed3c5df9 100644 --- a/tests/src/init.rs +++ b/tests/src/init.rs @@ -2,6 +2,18 @@ use wgpu::{Adapter, Backends, Device, Features, Instance, Limits, Queue}; use crate::{report::AdapterReport, TestParameters}; +/// Default device-lost callback installed by [`initialize_device`]. Panics on +/// any non-[`wgpu::DeviceLostReason::Destroyed`] device loss, which will +/// cause the test to be treated as a failure. +/// +/// Tests intentionally provoking device loss should install their own callback +/// with [`wgpu::Device::set_device_lost_callback`]. +fn default_device_lost_callback(reason: wgpu::DeviceLostReason, message: String) { + if reason != wgpu::DeviceLostReason::Destroyed { + panic!("Device lost: {message}"); + } +} + /// Initialize the logger for the test runner. pub fn init_logger() { // We don't actually care if it fails @@ -182,10 +194,14 @@ pub async fn initialize_device( }) .await; - match bundle { - Ok(b) => b, + let (device, queue) = match bundle { + Ok((device, queue)) => (device, queue), Err(e) => panic!("Failed to initialize device: {e}"), - } + }; + + device.set_device_lost_callback(default_device_lost_callback); + + (device, queue) } /// Create a canvas for testing. diff --git a/tests/tests/wgpu-gpu/clip_distances.rs b/tests/tests/wgpu-gpu/clip_distances.rs index a15933cfb..273c08be9 100644 --- a/tests/tests/wgpu-gpu/clip_distances.rs +++ b/tests/tests/wgpu-gpu/clip_distances.rs @@ -16,7 +16,7 @@ static CLIP_DISTANCES: GpuTestConfiguration = GpuTestConfiguration::new() wgpu_test::FailureCase::molten_vk() .validation_error("Shader library compile failed") .validation_error("could not be compiled into pipeline") - .panic("Unexpected Vulkan error: ERROR_INITIALIZATION_FAILED"), + .unexpected_error("Unexpected Vulkan error: ERROR_INITIALIZATION_FAILED"), ), ) .run_async(clip_distances); diff --git a/tests/tests/wgpu-gpu/draw_index.rs b/tests/tests/wgpu-gpu/draw_index.rs index ea03dc6f9..7acd3dc06 100644 --- a/tests/tests/wgpu-gpu/draw_index.rs +++ b/tests/tests/wgpu-gpu/draw_index.rs @@ -74,7 +74,7 @@ static DRAW_INDEX: GpuTestConfiguration = GpuTestConfiguration::new() wgpu_test::FailureCase::molten_vk() .validation_error("could not be compiled into pipeline") .validation_error("vkDestroyDevice") - .panic("Unexpected Vulkan error: ERROR_INITIALIZATION_FAILED"), + .unexpected_error("Unexpected Vulkan error: ERROR_INITIALIZATION_FAILED"), ), ) .run_async(test); diff --git a/tests/tests/wgpu-gpu/oob_indexing.rs b/tests/tests/wgpu-gpu/oob_indexing.rs index 343b91a92..1d24e3d93 100644 --- a/tests/tests/wgpu-gpu/oob_indexing.rs +++ b/tests/tests/wgpu-gpu/oob_indexing.rs @@ -23,7 +23,7 @@ static RESTRICT_WORKGROUP_PRIVATE_FUNCTION_LET: GpuTestConfiguration = GpuTestCo FailureCase::molten_vk() .validation_error("Shader library compile failed") .validation_error("could not be compiled into pipeline") - .panic("Unexpected Vulkan error: ERROR_INITIALIZATION_FAILED"), + .unexpected_error("Unexpected Vulkan error: ERROR_INITIALIZATION_FAILED"), ), ) .run_async(|ctx| async move { diff --git a/tests/tests/wgpu-gpu/per_vertex/mod.rs b/tests/tests/wgpu-gpu/per_vertex/mod.rs index 21d2046d2..de55f7bd7 100644 --- a/tests/tests/wgpu-gpu/per_vertex/mod.rs +++ b/tests/tests/wgpu-gpu/per_vertex/mod.rs @@ -40,7 +40,7 @@ static PER_VERTEX: GpuTestConfiguration = GpuTestConfiguration::new() .expect_fail( wgpu_test::FailureCase::molten_vk() .validation_error("could not be compiled into pipeline") - .panic("Unexpected Vulkan error: ERROR_INITIALIZATION_FAILED"), + .unexpected_error("Unexpected Vulkan error: ERROR_INITIALIZATION_FAILED"), ), ) .run_async(per_vertex); diff --git a/tests/tests/wgpu-gpu/shader/array_size_overrides.rs b/tests/tests/wgpu-gpu/shader/array_size_overrides.rs index ba6487abf..9409d3232 100644 --- a/tests/tests/wgpu-gpu/shader/array_size_overrides.rs +++ b/tests/tests/wgpu-gpu/shader/array_size_overrides.rs @@ -60,7 +60,7 @@ static ARRAY_SIZE_OVERRIDES: GpuTestConfiguration = GpuTestConfiguration::new() wgpu_test::FailureCase::molten_vk() .validation_error("Shader library compile failed") .validation_error("could not be compiled into pipeline") - .panic("Unexpected Vulkan error: ERROR_INITIALIZATION_FAILED"), + .unexpected_error("Unexpected Vulkan error: ERROR_INITIALIZATION_FAILED"), ), ) .run_async(move |ctx| async move { diff --git a/tests/tests/wgpu-gpu/shader/zero_init_workgroup_mem.rs b/tests/tests/wgpu-gpu/shader/zero_init_workgroup_mem.rs index a02099091..5a3584725 100644 --- a/tests/tests/wgpu-gpu/shader/zero_init_workgroup_mem.rs +++ b/tests/tests/wgpu-gpu/shader/zero_init_workgroup_mem.rs @@ -25,7 +25,7 @@ static ZERO_INIT_WORKGROUP_MEMORY: GpuTestConfiguration = GpuTestConfiguration:: wgpu_test::FailureCase::molten_vk() .validation_error("Shader library compile failed") .validation_error("could not be compiled into pipeline") - .panic("Unexpected Vulkan error: ERROR_INITIALIZATION_FAILED"), + .unexpected_error("Unexpected Vulkan error: ERROR_INITIALIZATION_FAILED"), ), ) .run_async(|ctx| async move {