A surface left without a configuration is configured again, not a crash (#7007) (#7401)

* claim: #7007

* A surface left without a configuration is configured again, not a crash (#7007)

wgpu reports device-loss errors to no error scope. On D3D12 a driver reset
found by Surface::configure drops the old configuration and reports nothing,
so the surface was taken as configured and the next acquire panicked with
"Surface is not configured for presentation". A configure that meets a lost
device or an unsupported surface is now a refused configure, the acquire runs
under an error scope, and its Validation status reconfigures the surface.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
semmelrinde
2026-10-27 12:37:31 +00:00
committed by GitHub
parent 6081226fa6
commit 0b0886b87c
4 changed files with 134 additions and 13 deletions
+58 -2
View File
@@ -5,9 +5,14 @@
//! driver's refusal can't be provoked here, so these check the capture on a real device with an
//! error the validation always reports, and the retry policy with a stand-in clock and device.
//! An error no scope catches at all is reported by the device's handler
//! (`gpu::log_uncaptured_errors`) instead of panicking (#5486).
//! (`gpu::log_uncaptured_errors`) instead of panicking (#5486). A configure that meets a lost
//! device isn't taken as configured, and an acquire wgpu refuses configures the surface again
//! (#7007).
use egui_wgpu::{Attempt, ConfigureRetry, SURFACE_RECREATE_AFTER, capture_errors, wgpu};
use egui_wgpu::{
Attempt, ConfigureError, ConfigureRetry, DeviceLost, SURFACE_RECREATE_AFTER, SurfaceErrorAction, WgpuConfiguration, capture_errors,
configure_captured, wgpu,
};
use std::time::Duration;
use web_time::Instant;
@@ -27,6 +32,57 @@ fn an_error_under_capture_is_returned_not_a_panic() {
assert_eq!(buffer.size(), 4);
}
/// A driver reset found by a surface configure (D3D12 waits for the present queue after wgpu has
/// dropped the surface's old configuration) reports nothing: wgpu passes device-loss errors to no
/// error scope. The configure was taken, the next acquire met a surface without a configuration,
/// and the device's default handler panicked: "Surface is not configured for presentation"
/// (#7007). `destroy` loses the device as a driver reset does; wgpu calls the lost callback once
/// it has polled.
#[test]
fn a_configure_that_meets_a_lost_device_is_not_taken() {
// The queue is kept: wgpu calls a destroyed device's lost callback only while it has one.
let Some((_, device, _queue)) = crate::gpu::offscreen_device() else { return };
let lost = DeviceLost::watch(&device);
let bad = wgpu::BufferDescriptor { label: Some("bad"), size: 3, usage: wgpu::BufferUsages::MAP_WRITE, mapped_at_creation: true };
// On a working device a clean configure is taken and a refused one returns its error.
assert!(configure_captured(&device, &lost, || {}).is_ok());
let refused = configure_captured(&device, &lost, || {
let _ = device.create_buffer(&bad);
});
assert!(matches!(refused, Err(ConfigureError::Refused(wgpu::Error::Validation { .. }))), "{refused:?}");
// The device is lost during the configure, which goes on and fails quietly.
let met_loss = configure_captured(&device, &lost, || {
device.destroy();
let _ = device.poll(wgpu::PollType::wait_indefinitely());
let _ = device.create_buffer(&bad);
});
assert!(matches!(met_loss, Err(ConfigureError::DeviceLost)), "{met_loss:?}");
assert!(met_loss.is_err_and(|e| e.to_string() == "the graphics device was lost"));
// Why the scope alone can't tell: on the lost device the same invalid call reports nothing.
let (_, error) = capture_errors(&device, || device.create_buffer(&bad));
assert!(error.is_none(), "{error:?}");
}
/// wgpu hands back `CurrentSurfaceTexture::Validation` for an acquire whose error was captured:
/// most often a surface without a configuration. Skipping the frame, as before, met the same
/// error on every later frame (#7007); the surface is configured again instead. The other
/// statuses keep their actions.
#[test]
fn an_acquire_wgpu_refuses_configures_the_surface_again() {
let status = WgpuConfiguration::default().on_surface_status;
let action = |s: wgpu::CurrentSurfaceTexture| match status(&s) {
SurfaceErrorAction::SkipFrame => "skip",
SurfaceErrorAction::Reconfigure => "reconfigure",
SurfaceErrorAction::RecreateSurface => "recreate",
};
assert_eq!(action(wgpu::CurrentSurfaceTexture::Validation), "reconfigure");
assert_eq!(action(wgpu::CurrentSurfaceTexture::Outdated), "reconfigure");
assert_eq!(action(wgpu::CurrentSurfaceTexture::Lost), "recreate");
assert_eq!(action(wgpu::CurrentSurfaceTexture::Occluded), "skip");
assert_eq!(action(wgpu::CurrentSurfaceTexture::Timeout), "skip");
}
/// An error no scope caught on the window's device used to reach wgpu's default handler, which
/// panics ("wgpu error: Out of Memory", #5486). With the handler installed it is reported and
/// the device goes on working.
+7
View File
@@ -318,6 +318,13 @@ SolidCraft changes:
submits nothing (`DeviceLost::probe`), so a device the driver lost is found by that
submission rather than by the first queue write, which panicked inside wgpu-core
(`PendingWrites::activate`, #4172).
- `src/lib.rs`, `src/winit.rs`: a surface configure that meets a lost device is not taken as
configured (`configure_captured`): wgpu reports device-loss errors to no error scope, and on
D3D12 a driver reset found by the configure has already dropped the surface's configuration.
The next acquire failed with "Surface is not configured for presentation" and the device's
default handler panicked (#7007). An unsupported surface is a refused configure too. The
acquire runs under `capture_errors`, and the `Validation` status it then returns reconfigures
the surface instead of skipping every later frame.
- `src/lib.rs`: creation of the egui renderer uses validation, internal and out-of-memory
error scopes, awaiting every result before returning it. A refused UI pipeline (D3D12
`egui_pipeline` shader compilation, #6648) returns `WgpuError::CreateRendererError` through
+51
View File
@@ -568,6 +568,13 @@ impl Default for WgpuConfiguration {
log::trace!("Skipping frame due to occlusion.");
SurfaceErrorAction::SkipFrame
}
wgpu::CurrentSurfaceTexture::Validation => {
// SolidCraft: the acquire's error was captured; most often the surface has
// no configuration ("Surface is not configured for presentation", #7007).
// Skipping alone would meet the same error on every frame after.
log::debug!("Dropped frame with error: {status:?}");
SurfaceErrorAction::Reconfigure
}
_ => {
log::warn!("Dropped frame with error: {status:?}");
SurfaceErrorAction::SkipFrame
@@ -830,6 +837,50 @@ pub fn capture_errors<T>(device: &wgpu::Device, f: impl FnOnce() -> T) -> (T, Op
(value, failed)
}
/// SolidCraft: why a surface configure ([`configure_captured`]) left the surface without its
/// configuration.
#[derive(Debug)]
pub enum ConfigureError {
/// The device refused it, with this error (#1935).
Refused(wgpu::Error),
/// The device was lost (#7007).
DeviceLost,
/// The adapter doesn't support the surface.
Unsupported,
}
impl std::fmt::Display for ConfigureError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Refused(error) => error.fmt(f),
Self::DeviceLost => f.write_str("the graphics device was lost"),
Self::Unsupported => f.write_str("the surface isn't supported by this adapter"),
}
}
}
/// SolidCraft: run a surface configure on `device` under [`capture_errors`], and say whether the
/// surface has its configuration. A configure that meets a lost device reports nothing: wgpu
/// passes device-loss errors to no error scope and no handler, only to the lost callback. On
/// D3D12 a driver reset is found that way, when the configure waits for the present queue, after
/// wgpu has dropped the surface's old configuration. Taken as configured, the surface's next
/// acquire failed with "Surface is not configured for presentation", and the device's default
/// error handler panicked (#7007). `lost` is the device's [`DeviceLost`], marked by then.
pub fn configure_captured(
device: &wgpu::Device,
lost: &DeviceLost,
configure: impl FnOnce(),
) -> Result<(), ConfigureError> {
let ((), failed) = capture_errors(device, configure);
if let Some(error) = failed {
return Err(ConfigureError::Refused(error));
}
if lost.is_lost() {
return Err(ConfigureError::DeviceLost);
}
Ok(())
}
/// SolidCraft: refused configures in a row after which the surface is recreated (#1935).
pub const SURFACE_RECREATE_AFTER: u32 = 3;
+18 -11
View File
@@ -107,13 +107,14 @@ impl Painter {
/// SolidCraft: a configuration the device refuses (D3D12: "Failed to wait for GPU to come
/// idle before reconfiguring the Surface", #1935) comes back as the error instead of reaching
/// the device's uncaptured-error handler, which panics. Called only through
/// [`Self::try_configure`], which says what follows.
/// the device's uncaptured-error handler, which panics; so does a configure that met a lost
/// device, or a surface the adapter doesn't support, neither of which configured it (#7007).
/// Called only through [`Self::try_configure`], which says what follows.
fn configure_surface(
surface_state: &SurfaceState,
render_state: &RenderState,
config: &SurfaceConfig,
) -> Result<(), wgpu::Error> {
) -> Result<(), crate::ConfigureError> {
profiling::function_scope!();
let SurfaceConfig {
@@ -133,13 +134,12 @@ impl Painter {
let width = surface_state.width;
let height = surface_state.height;
// SolidCraft: an unsupported surface is logged, not a panic.
// SolidCraft: an unsupported surface is a refused configure, not a panic.
let Some(default_config) = surface_state
.surface
.get_default_config(&render_state.adapter, width, height)
else {
log::error!("The surface isn't supported by this adapter");
return Ok(());
return Err(crate::ConfigureError::Unsupported);
};
let mut surf_config = wgpu::SurfaceConfiguration {
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
@@ -155,10 +155,9 @@ impl Painter {
}
let device = &render_state.device;
let ((), failed) = crate::capture_errors(device, || {
surface_state.surface.configure(device, &surf_config)
});
failed.map_or(Ok(()), Err)
crate::configure_captured(device, &render_state.device_lost, || {
surface_state.surface.configure(device, &surf_config);
})
}
/// SolidCraft: configures the surface at `now` unless a refused configure's retry isn't due
@@ -723,7 +722,15 @@ impl Painter {
if !FIRST_PRESENT.swap(true, std::sync::atomic::Ordering::Relaxed) {
crate::startup_step(crate::STEP_FIRST_PRESENT, "");
}
let output_frame = surface_state.surface.get_current_texture();
// SolidCraft: an acquire wgpu refuses (a surface left without its configuration,
// #7007) comes back as `CurrentSurfaceTexture::Validation` instead of reaching the
// device's uncaptured-error handler, which panics by default.
let (output_frame, failed) = crate::capture_errors(&render_state.device, || {
surface_state.surface.get_current_texture()
});
if let Some(error) = failed {
log::warn!("The surface gave no frame: {error}");
}
vsync_sec += start.elapsed().as_secs_f32();
output_frame
};