From eb752a4209ca5b073b85df104a96da6b74331fbf Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 13 Nov 2026 05:10:23 -0800 Subject: [PATCH 1/2] craft-ui: visual regression tests take turns with the wgpu renderer The visual_regression test binary crashed with SIGSEGV now and then on the Ubuntu CI runner, where wgpu uses Vulkan (WGPU_BACKEND=vulkan) with Mesa's software driver. Four of its tests each create a WgpuTestRenderer, which creates its own Vulkan instance and device, and the test harness runs them on parallel threads. When one test uses its device while another creates or destroys one, the Vulkan loader can read a device entry that is being freed. Each of those tests now takes a shared lock before it creates its renderer and holds it until the renderer is dropped. A poisoned lock is taken over, so a failed test does not block the others. --- crates/ui/tests/visual_regression.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/crates/ui/tests/visual_regression.rs b/crates/ui/tests/visual_regression.rs index 532c9dfc7..d9902965b 100644 --- a/crates/ui/tests/visual_regression.rs +++ b/crates/ui/tests/visual_regression.rs @@ -9,6 +9,7 @@ mod fixtures; use std::path::PathBuf; +use std::sync::{Mutex, MutexGuard, PoisonError}; use egui::{Color32, Context, Event, FullOutput, Modifiers, PointerButton, Pos2, RawInput, Rect, Response, TextureId, Ui}; use egui_kittest::{TestRenderer, wgpu::WgpuTestRenderer}; @@ -28,6 +29,17 @@ struct Capture { meshes: Option>, } +/// Lets one test at a time hold a wgpu renderer. With Mesa's software Vulkan driver, a test +/// binary that renders on two devices at once from parallel test threads now and then crashes +/// with SIGSEGV. +static GPU_LOCK: Mutex<()> = Mutex::new(()); + +/// Take this before creating a renderer (`let _gpu = gpu_lock();`), so it is released after the +/// renderer is dropped. A test that failed while holding it does not block the others. +fn gpu_lock() -> MutexGuard<'static, ()> { + GPU_LOCK.lock().unwrap_or_else(PoisonError::into_inner) +} + fn context(theme: Theme, scale: f32) -> Context { let ctx = Context::default(); ctx.set_theme(theme.egui()); @@ -184,6 +196,7 @@ impl Artifacts { #[test] fn rendered_controls_match_independent_reference_pixels_and_meshes() { + let _gpu = gpu_lock(); let mut renderer = WgpuTestRenderer::new(); let mut artifacts = Artifacts::new("controls"); let mut count = 0; @@ -213,6 +226,7 @@ fn rendered_controls_match_independent_reference_pixels_and_meshes() { #[test] fn visual_comparisons_detect_color_spacing_and_stroke_mutations() { + let _gpu = gpu_lock(); let mut renderer = WgpuTestRenderer::new(); let mut artifacts = Artifacts::new("negative-controls"); let mut count = 0; @@ -241,6 +255,7 @@ fn toolbar_fitting_matches_literal_group_and_overflow_positions() { use craft_ui::layout::fit_toolbar; use egui::{Align, Layout, Stroke, StrokeKind, UiBuilder, pos2, vec2}; + let _gpu = gpu_lock(); let mut renderer = WgpuTestRenderer::new(); let mut artifacts = Artifacts::new("toolbar"); let mut count = 0; @@ -295,6 +310,7 @@ fn widget_palette_renders_all_five_states_with_existing_borders_and_expansion() use craft_ui::theme::WidgetPalette; use egui::{CornerRadius, Stroke, StrokeKind, pos2, vec2}; + let _gpu = gpu_lock(); let mut renderer = WgpuTestRenderer::new(); let mut artifacts = Artifacts::new("widget-palette"); for theme in [Theme::Light, Theme::Dark] { From 23b10d78fb2d86a9648951c75d498cb911372510 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 13 Nov 2026 06:05:11 -0800 Subject: [PATCH 2/2] craft-ui: unit tests that render take turns with the wgpu renderer Each egui_kittest wgpu renderer creates its own Vulkan instance and device. When one test uses its device while a test on another thread creates or destroys one, the Vulkan loader now and then crashes the test binary with SIGSEGV. The previous commit serialized the four rendering tests in tests/visual_regression.rs; the craft-ui unit test binary has the same pattern in two tests: combo::tests::the_button_and_menu_look_like_eguis and tabs::dialog::tests::selected_tab_erases_a_baseline_centered_on_its_bottom_edge. Both now take crate::gpu_test_lock() before building their harness and hold it until the harness is dropped. The comment on the lock in visual_regression.rs now names the loader race instead of concurrent rendering alone. --- crates/ui/src/combo/tests.rs | 1 + crates/ui/src/lib.rs | 11 +++++++++++ crates/ui/src/tabs/dialog.rs | 1 + crates/ui/tests/visual_regression.rs | 6 +++--- 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/crates/ui/src/combo/tests.rs b/crates/ui/src/combo/tests.rs index 90e1afb15..fbef2413a 100644 --- a/crates/ui/src/combo/tests.rs +++ b/crates/ui/src/combo/tests.rs @@ -170,6 +170,7 @@ fn scene(ui: &mut egui::Ui, ours: bool) { } fn render(ours: bool, hover: Option<&str>, open: Option<&str>) -> image::RgbaImage { + let _gpu = crate::gpu_test_lock(); // Two of the combo boxes have no label: the scene compares looks, not names. let mut h = Harness::builder().with_size(egui::vec2(480.0, 700.0)).with_accessibility_check(false).build_ui(move |ui| scene(ui, ours)); h.run(); diff --git a/crates/ui/src/lib.rs b/crates/ui/src/lib.rs index e5a2d06c2..bbdd912ac 100644 --- a/crates/ui/src/lib.rs +++ b/crates/ui/src/lib.rs @@ -27,3 +27,14 @@ pub mod text; pub mod theme; pub use combo::ComboBox; + +/// Lets one unit test at a time hold a wgpu renderer. Each renderer creates its own Vulkan instance +/// and device. When one test uses its device while a test on another thread creates or destroys +/// one, the Vulkan loader now and then crashes the test binary with SIGSEGV. Take the lock before +/// building the harness (`let _gpu = crate::gpu_test_lock();`), so it is released after the harness +/// is dropped. A test that failed while holding it does not block the others. +#[cfg(test)] +pub(crate) fn gpu_test_lock() -> std::sync::MutexGuard<'static, ()> { + static LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(()); + LOCK.lock().unwrap_or_else(std::sync::PoisonError::into_inner) +} diff --git a/crates/ui/src/tabs/dialog.rs b/crates/ui/src/tabs/dialog.rs index 292cbb4cf..e4820b042 100644 --- a/crates/ui/src/tabs/dialog.rs +++ b/crates/ui/src/tabs/dialog.rs @@ -123,6 +123,7 @@ mod tests { #[test] fn selected_tab_erases_a_baseline_centered_on_its_bottom_edge() { + let _gpu = crate::gpu_test_lock(); let mut h = egui_kittest::Harness::builder().with_size(Vec2::new(120.0, 80.0)).wgpu().build_ui(|ui| { ui.painter().rect_filled(ui.max_rect(), 0.0, Color32::WHITE); let rect = Rect::from_min_size(egui::pos2(10.0, 10.0), Vec2::new(100.0, 26.0)); diff --git a/crates/ui/tests/visual_regression.rs b/crates/ui/tests/visual_regression.rs index d9902965b..87edb8b4f 100644 --- a/crates/ui/tests/visual_regression.rs +++ b/crates/ui/tests/visual_regression.rs @@ -29,9 +29,9 @@ struct Capture { meshes: Option>, } -/// Lets one test at a time hold a wgpu renderer. With Mesa's software Vulkan driver, a test -/// binary that renders on two devices at once from parallel test threads now and then crashes -/// with SIGSEGV. +/// Lets one test at a time hold a wgpu renderer. Each renderer creates its own Vulkan instance and +/// device. When one test uses its device while a test on another thread creates or destroys one, +/// the Vulkan loader now and then crashes the test binary with SIGSEGV. static GPU_LOCK: Mutex<()> = Mutex::new(()); /// Take this before creating a renderer (`let _gpu = gpu_lock();`), so it is released after the