Files
photocraft/crates/testkit/src/gpu.rs
Dan HiornsandClaude Opus 5.5 c18ee8c88b Tests: one GPU harness at a time in every GPU test binary (WARP crash) (#3212)
On Windows, GPU UI test binaries crashed with STATUS_ACCESS_VIOLATION
whenever two of their tests ran at once: arbitrary_rotation_preview 3
runs in 4 on main, and color_managed_canvas, crop_default_mode,
drag_preview_canvas and full_res_preview at random. egui_kittest
prefers a CPU adapter, which on Windows is WARP over DX12, and a dump
puts the fault inside d3d10warp.dll with two WARP devices rendering on
parallel test threads. Bare egui harnesses with no PhotoCraft code
crash the same way, and Vulkan or GL never does. CI's Windows runners
have no GPU, so they run on WARP too.

Three binaries already held a per-binary lock for the same class of
driver bug (Mesa llvmpipe and RADV, #194; libvulkan with NVIDIA and
lavapipe). That lock now lives once in photocraft-testkit
(`gpu_lock`, with the reasons) and every test in the twelve GPU test
binaries without it takes it on its first line. Under nextest each
test is its own process and the lock is free.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 19:27:26 -04:00

65 lines
2.5 KiB
Rust

//! One GPU harness at a time per test process.
//!
//! `cargo test` runs a binary's tests on parallel threads, and several graphics drivers crash
//! when one process drives wgpu devices from more than one thread at once:
//!
//! - **WARP** (Windows): egui_kittest prefers a CPU adapter, which on Windows is WARP over DX12,
//! so every kittest GPU test there runs on it. Two or more WARP devices rendering in parallel
//! fault inside `d3d10warp.dll` (`STATUS_ACCESS_VIOLATION`); the GitHub Windows runners have no
//! GPU, so CI runs on WARP too.
//! - **Mesa** llvmpipe over GL and RADV (#194).
//! - **libvulkan** with the NVIDIA and lavapipe drivers both installed: `wgpu::Instance::new` on
//! several threads segfaults.
//!
//! So every test that builds a GPU harness (or its own wgpu device) holds [`gpu_lock`] for its
//! whole run, harness included. Under nextest each test is its own process and the lock costs
//! nothing; `.config/nextest.toml` caps how many of those run at once instead.
use std::sync::{Mutex, MutexGuard, PoisonError};
static GPU_LOCK: Mutex<()> = Mutex::new(());
/// Hold this for as long as the test uses a GPU harness or device: take it on the test's first
/// line (`let _gpu = gpu_lock();`) so it is released last. A test that failed while holding it
/// doesn't block the rest.
pub fn gpu_lock() -> MutexGuard<'static, ()> {
GPU_LOCK.lock().unwrap_or_else(PoisonError::into_inner)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_panicked_holder_does_not_block_the_next_test() {
let _ = std::thread::spawn(|| {
let _gpu = gpu_lock();
panic!("a GPU test failed");
})
.join();
drop(gpu_lock());
}
#[test]
fn holders_take_turns() {
use std::sync::atomic::{AtomicUsize, Ordering};
static INSIDE: AtomicUsize = AtomicUsize::new(0);
static MAX: AtomicUsize = AtomicUsize::new(0);
let threads: Vec<_> = (0..8)
.map(|_| {
std::thread::spawn(|| {
let _gpu = gpu_lock();
let now = INSIDE.fetch_add(1, Ordering::SeqCst) + 1;
MAX.fetch_max(now, Ordering::SeqCst);
std::thread::sleep(std::time::Duration::from_millis(2));
INSIDE.fetch_sub(1, Ordering::SeqCst);
})
})
.collect();
for t in threads {
assert!(t.join().is_ok());
}
assert_eq!(MAX.load(Ordering::SeqCst), 1);
}
}