From 2ecf54bf39ba84b60d4c7c3682437ed768fa6bf3 Mon Sep 17 00:00:00 2001 From: Skyfuller1303 Date: Fri, 6 Nov 2026 22:00:40 +0100 Subject: [PATCH 1/3] Share one wgpu device between the desktop app's kittest tests Every ui test built its harness with `.wgpu()`, which creates a fresh wgpu instance and device (WARP on Windows) per test. The tests run on parallel threads, and creating and dropping several D3D12 devices at the same time crashed the test process with STATUS_ACCESS_VIOLATION (0xc0000005) in the middle of the run, in most runs of `cargo test -p craft-cloud-desktop --lib` on Windows. With `--test-threads=1` it never happened. Create the instance, adapter, device and queue once, keep them in a `OnceLock` (so no device is torn down while other threads render) and hand them to each harness with `wgpu_setup(WgpuSetup::Existing(..))`. Every harness still gets its own renderer. Snapshots are unchanged. Test code only; this works on every backend kittest picks (lavapipe, WARP, Metal). --- apps/craft-cloud-desktop/src/ui/tests.rs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/apps/craft-cloud-desktop/src/ui/tests.rs b/apps/craft-cloud-desktop/src/ui/tests.rs index e059f6be..6017ba9b 100644 --- a/apps/craft-cloud-desktop/src/ui/tests.rs +++ b/apps/craft-cloud-desktop/src/ui/tests.rs @@ -6,6 +6,7 @@ //! [`TOLERANCE`] in a channel. use std::path::PathBuf; +use std::sync::OnceLock; use craft_cloud_app::{DeviceId, DeviceInfo, PairingInfo, RunState, Status, Uuid}; use egui_kittest::Harness; @@ -89,9 +90,28 @@ fn devices() -> Vec { ] } +/// The one wgpu instance, adapter, device and queue every test renders with. Tests run on parallel threads, and +/// creating and dropping a D3D12 device (WARP) per harness at the same time crashed the test process with an access +/// violation. Each harness still gets a renderer of its own on the shared device. It is never dropped, so no device is +/// torn down while other threads render. +fn shared_wgpu() -> eframe::egui_wgpu::WgpuSetup { + static SHARED: OnceLock = OnceLock::new(); + let shared = SHARED.get_or_init(|| { + let state = + egui_kittest::wgpu::create_render_state(egui_kittest::wgpu::default_wgpu_setup(), eframe::egui_wgpu::RendererOptions::PREDICTABLE); + eframe::egui_wgpu::WgpuSetupExisting { + instance: state.instance.clone(), + adapter: state.adapter.clone(), + device: state.device.clone(), + queue: state.queue.clone(), + } + }); + eframe::egui_wgpu::WgpuSetup::Existing(shared.clone()) +} + /// The harness: the window drawn for the model, its events kept. fn harness(model: Model, theme: egui::Theme) -> Harness<'static, (Model, Vec)> { - Harness::builder().with_size(egui::Vec2::from(WINDOW_SIZE)).with_theme(theme).wgpu().build_ui_state( + Harness::builder().with_size(egui::Vec2::from(WINDOW_SIZE)).with_theme(theme).wgpu_setup(shared_wgpu()).build_ui_state( |ui, (model, events): &mut (Model, Vec)| { events.extend(show(ui, model, NOW)); }, From 353bb65e18d7bc7f914817d94e315e814b647da7 Mon Sep 17 00:00:00 2001 From: Skyfuller1303 Date: Sat, 7 Nov 2026 11:07:04 +0100 Subject: [PATCH 2/3] Let every desktop test module use the shared wgpu device Other test modules of the app (such as a Security page test module in a stacked pull request) build their own kittest harnesses. Make shared_wgpu() visible to them, so all of the app's harnesses render on one device. --- apps/craft-cloud-desktop/src/ui/tests.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/craft-cloud-desktop/src/ui/tests.rs b/apps/craft-cloud-desktop/src/ui/tests.rs index 6017ba9b..6d882484 100644 --- a/apps/craft-cloud-desktop/src/ui/tests.rs +++ b/apps/craft-cloud-desktop/src/ui/tests.rs @@ -93,8 +93,8 @@ fn devices() -> Vec { /// The one wgpu instance, adapter, device and queue every test renders with. Tests run on parallel threads, and /// creating and dropping a D3D12 device (WARP) per harness at the same time crashed the test process with an access /// violation. Each harness still gets a renderer of its own on the shared device. It is never dropped, so no device is -/// torn down while other threads render. -fn shared_wgpu() -> eframe::egui_wgpu::WgpuSetup { +/// torn down while other threads render. Every kittest harness of the app, in any test module, must render with it. +pub(super) fn shared_wgpu() -> eframe::egui_wgpu::WgpuSetup { static SHARED: OnceLock = OnceLock::new(); let shared = SHARED.get_or_init(|| { let state = From 29b95db03d1f53fb2de6bcd37277d53ee6c5a988 Mon Sep 17 00:00:00 2001 From: Skyfuller1303 Date: Mon, 9 Nov 2026 13:26:36 +0100 Subject: [PATCH 3/3] Desktop tests: render one test at a time on the shared device (wgpu#10547 corrupts the heap otherwise) --- apps/craft-cloud-desktop/src/ui/tests.rs | 51 ++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/apps/craft-cloud-desktop/src/ui/tests.rs b/apps/craft-cloud-desktop/src/ui/tests.rs index 6d882484..34de481f 100644 --- a/apps/craft-cloud-desktop/src/ui/tests.rs +++ b/apps/craft-cloud-desktop/src/ui/tests.rs @@ -6,7 +6,7 @@ //! [`TOLERANCE`] in a channel. use std::path::PathBuf; -use std::sync::OnceLock; +use std::sync::{Mutex, MutexGuard, OnceLock, PoisonError}; use craft_cloud_app::{DeviceId, DeviceInfo, PairingInfo, RunState, Status, Uuid}; use egui_kittest::Harness; @@ -93,7 +93,7 @@ fn devices() -> Vec { /// The one wgpu instance, adapter, device and queue every test renders with. Tests run on parallel threads, and /// creating and dropping a D3D12 device (WARP) per harness at the same time crashed the test process with an access /// violation. Each harness still gets a renderer of its own on the shared device. It is never dropped, so no device is -/// torn down while other threads render. Every kittest harness of the app, in any test module, must render with it. +/// torn down while other threads render. Every kittest harness of the app, in any test module, must render with it through [`serialized`]. pub(super) fn shared_wgpu() -> eframe::egui_wgpu::WgpuSetup { static SHARED: OnceLock = OnceLock::new(); let shared = SHARED.get_or_init(|| { @@ -109,9 +109,54 @@ pub(super) fn shared_wgpu() -> eframe::egui_wgpu::WgpuSetup { eframe::egui_wgpu::WgpuSetup::Existing(shared.clone()) } +/// Held while a test touches the GPU. A panicking test poisons it, which says nothing about the GPU, so the poison is ignored. +static GPU: Mutex<()> = Mutex::new(()); + +fn gpu() -> MutexGuard<'static, ()> { + GPU.lock().unwrap_or_else(PoisonError::into_inner) +} + +/// A kittest renderer on the shared device that does its GPU work one test at a time. +pub(super) struct Serialized(Option); + +impl Serialized { + fn inner(&mut self) -> &mut egui_kittest::wgpu::WgpuTestRenderer { + self.0.as_mut().expect("the renderer lives until the drop") + } +} + +impl egui_kittest::TestRenderer for Serialized { + fn handle_delta(&mut self, delta: &mut egui::TexturesDelta) { + let _gpu = gpu(); + self.inner().handle_delta(delta); + } + + fn render(&mut self, ctx: &egui::Context, output: &egui::FullOutput) -> Result { + let _gpu = gpu(); + self.inner().render(ctx, output) + } +} + +impl Drop for Serialized { + fn drop(&mut self) { + let _gpu = gpu(); + drop(self.0.take()); + } +} + +/// The renderer every kittest harness of the app uses: the shared device, with creating, drawing and dropping the +/// renderer done one test at a time. The DX12 backend of wgpu hands out shader blobs from a cache whose reference +/// counting is not thread-safe, so renderers made on parallel threads corrupt the heap (gfx-rs/wgpu#10547), and drawing +/// in parallel on WARP crashes in its JIT (emilk/egui#8772). The lock can go once a wgpu release with the fix +/// (gfx-rs/wgpu#10805) is in use. +pub(super) fn serialized() -> Serialized { + let _gpu = gpu(); + Serialized(Some(egui_kittest::wgpu::WgpuTestRenderer::from_setup(shared_wgpu()))) +} + /// The harness: the window drawn for the model, its events kept. fn harness(model: Model, theme: egui::Theme) -> Harness<'static, (Model, Vec)> { - Harness::builder().with_size(egui::Vec2::from(WINDOW_SIZE)).with_theme(theme).wgpu_setup(shared_wgpu()).build_ui_state( + Harness::builder().with_size(egui::Vec2::from(WINDOW_SIZE)).with_theme(theme).renderer(serialized()).build_ui_state( |ui, (model, events): &mut (Model, Vec)| { events.extend(show(ui, model, NOW)); },