Skip to content

gpu: a lost device or an unconfigured surface makes a new one instead of panicking the app - #492

Merged
thdxr merged 4 commits into
devfrom
fix/lost-device-surface
Nov 10, 2026
Merged

thdxr merged 4 commits into
devfrom
fix/lost-device-surface

Conversation

@thdxr

@thdxr thdxr commented Nov 10, 2026 •

Copy link
Copy Markdown
Member

what broke

A Windows app on wgpu 30.0.1 DX12 panicked on its main thread after a resume from Modern
Standby: "Error in Surface::get_current_texture_view: Validation Error / Caused by: Surface
is not configured for presentation".

why

wgpu's Surface::configure records the surface's error sink only when the configuration
succeeds (wgpu-30.0.1 src/backend/wgpu_core.rs, SurfaceInterface::configure). When the
device is lost, wgpu-core has already taken the surface's old presentation and returns
DeviceLost, which handle_error_inner drops ("will be surfaced via callback"). So the
surface is left with no presentation and no error sink, and the next get_current_texture
takes the fatal path and panics.

Rune had no way to see that: it never installed a device-lost callback, kept a parked
device for five minutes and handed it to a fresh surface without a liveness check, and its
main window configured the surface raw (window/create.rs, Win::resize_surface).

what changed

  • Gpu installs Device::set_device_lost_callback on every Rune-made device and exposes
    Gpu::is_lost. Gpu::from_device is the same for a device an app made. A Gpu literal
    outside Rune no longer builds; use Gpu::from_device.
  • A lost device is never parked (render/warm.rs), warmed, shared or handed out, and a
    cached or parked one is dropped instead.
  • window::configure takes the Gpu and fails when it is lost, before and after the
    configure, since wgpu reports nothing there.
  • acquire takes the Gpu and whether the surface's last configuration took; either a
    lost device or an unconfigured surface returns Missed::Unconfigured without asking
    wgpu for a drawable.
  • The main window's create and resize use the checked configure. A window whose device is
    lost, or whose surface refused its configuration, replaces the surface and device before
    its next frame (Host::reattach), keeping the window, its size and its place. If that
    fails it releases both as a hidden window with release_gpu_when_closed does, records
    AppHandle::window_error, and the next open makes them again.
  • An overlay drops a lost device before attaching a new surface, and its acquire reports
    Unconfigured as before, so the hidden overlay makes new ones on its next show.
  • Rune-created devices log uncaptured errors instead of panicking in wgpu's default handler.

tests

  • window/tests.rs: a lost device is reported by configure and never drawn with; a
    surface with no presentation is never asked for a drawable (the panic path).
  • render/warm_tests.rs: a parked device that died is not handed to the next surface.
  • window/occluded_surface_tests.rs (macOS) follows the new acquire signature.

signature follow-ups, no behavior of their own: window/events.rs, window/stepping.rs
and window/lifecycle.rs call resize_surface(&Gpu); platform/web.rs and
crates/rune/examples/gpu_springs.rs build their Gpu with Gpu::from_device.

review follow-ups

  • window::configure polls the device (PollType::Poll) after surface.configure and before
    its lost check: Device::destroy only marks the device invalid, and its callback runs on the
    next maintain, so the surface could be left with no presentation while is_lost still read
    false. Regression test: destroy, no poll, configure must Err and acquire must not panic.
  • Win.surface is an Option, taken and dropped before self.gpu = None and before the new
    surface is made: a DX12 surface takes the window's one DirectComposition target
    (Dx12SwapchainKind::DxgiFromVisual), so a second surface on the same HWND could never be
    configured while the old one lived.
  • replace_lost_surface retries a failed replacement on a bounded backoff (SurfaceRetries,
    three tries) and then gives the window up through the same path as hide with
    release_gpu_when_closed (wind_down + release, no event loop), recording
    AppHandle::window_error; an app can open the window again, which makes a window, device and
    surface.
  • the uncaptured-error logger is installed only on devices that draw surfaces, and not under
    cfg(test), so headless devices and tests in rune, rune-fx, rune-light and downstream keep
    failing on validation errors; each distinct message prints once (bounded). It replaces any
    handler an app set on the device before handing it over.
  • from_device/is_lost and the loss watch live in render/gpu_loss.rs; configure and its
    lost message in window/configure.rs (file-size rule: render.rs 1460 non-test lines,
    window.rs under 800).
  • the macOS present-time LayerHook is re-attached to the new surface, and native Metal's
    set_device_lost_callback no-op is documented (so is_lost is never true on that backend).

… of panicking the app

wgpu records a surface's error sink only when `Surface::configure` succeeds. A device
that went away (a driver reset, a GPU removed, a resume from Windows' Modern Standby)
makes it fail with `DeviceLost`, which wgpu drops on the floor, so the surface is left
with no presentation and no error sink and the next `get_current_texture` panics with
"Surface is not configured for presentation". Rune also parked a device for five minutes
and handed it to a new surface without asking whether it was alive, and its window
configured the surface raw.

`Gpu` now watches its device's lost callback (`Device::set_device_lost_callback`) and
reports `is_lost`; a lost device is never parked, warmed, shared or handed out, and the
checked `configure` fails for one. A window whose device is lost, or whose surface refused
its configuration, replaces both before its next frame, keeping the window, its size and
its place; failing that it releases them as a hidden window with `release_gpu_when_closed`
does, and the next open makes them again. An overlay never reuses a lost device: it hides
and makes another surface and device on its next show.

Rune-created devices also log uncaptured errors (`rune: a GPU error went uncaptured: ...`)
instead of panicking in wgpu's default handler, so a driver's validation error costs the
frame rather than the app.

`Gpu::from_device` builds a `Gpu` over a device an app made, with the same watch; a `Gpu`
literal outside Rune no longer builds.
… is retried before the window goes

`window::configure` polls the device after configuring a surface. `Device::destroy` only
marks the device invalid: its lost callback runs when the device next maintains itself, so
without the poll a destroyed-but-unreported device configured nothing while `is_lost` still
read false, `configure` returned Ok, and the next drawable panicked with "Surface is not
configured for presentation".

`Win`'s surface is an `Option`, taken and dropped before `self.gpu = None` and before the new
surface is made. A DX12 surface takes the window's one DirectComposition target
(`Dx12SwapchainKind::DxgiFromVisual`), so a replacement could never be configured while the
old swapchain still held it.

`replace_lost_surface` retries a failed replacement on a bounded backoff (`SurfaceRetries`)
and then gives the window up through the same path `hide` uses for `release_gpu_when_closed`
(`wind_down` + `release`, no event loop), recording `AppHandle::window_error`; opening the
window again makes a window, device and surface.

The uncaptured-error logger is installed only on devices that draw surfaces, and not under
`cfg(test)`, so headless devices and tests in rune, rune-fx and rune-light keep failing on
validation errors; each distinct message prints once, bounded. It replaces any handler an app
set on the device before handing it over.

The loss watch moved to `render/gpu_loss.rs` and `configure` to `window/configure.rs`, under
the file-size rule. The macOS present-time hook is re-attached to a replacement surface, and
native Metal's no-op lost callback is documented.
…loss watch reads cleanly

Rebased onto dev's window state (`Life`, `Prime`) and its renderer split.

- `Host::draw` starts `SurfaceRetries` over only after a frame presents, not when `reattach`
  returns Ok and not on the healthy path: a device lost again before its frame reaches the
  screen used to reset the count every time and make a new device about every retry. The
  retries test now covers a surface that is replaced and lost again before it presents.
- `log_uncaptured` prints each distinct message once up to 16, then one line saying further
  ones are not shown, then nothing: a persistent error can't keep printing.
- `Native` keeps the log its present hook and shadow report to, so a reattached hook after a
  device loss uses the same log and a transparent window's shadow keeps re-sampling; the
  launch cap stays the launch's.
- `reattach` stops `Prime` (`Prime::stop`) instead of zeroing it, matching dev's state machine.
- wording: the surface backoff grows linearly (`SURFACE_BACKOFF` a try) and says so; the
  frame-latency configure uses `if let` rather than an early return.
…e still gives up

`replace_lost_surface` counted only failed replacements. A reattach that worked and was lost
again before its frame presented never counted, so it made a new device about every retry
forever. Every attempt now counts, worked or not, and only a presented frame (`Host::draw`)
starts over: `SurfaceRetries::attempt` says `Replace` or `GiveUp`, and `retry` gives the backoff
after a failure that may still try. A worked replacement draws at once, with no backoff.

The give-up path leaves the window the way `hide` does before it stops drawing: `stop_showing`
(shared with `hide`) hides the `Life`, drops a pending minimize, lands a frame move and (macOS)
delivers the Fn key change; then `wind_down` and `release` give back the window, device and
surface. It used to drop a `Life::Shown` window, so `AppHandle::is_visible` and a reopen saw a
window on screen that was gone.

Also: the frame's presumed-shown drawable uses a let chain instead of an early return.

This branch was previously deployed

1 inactive deployment
release — ac239d63 Deployed Nov 10, 2026 by thdxr via build #416
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant