Repository navigation
gpu: a lost device or an unconfigured surface makes a new one instead of panicking the app - #492
Merged
Merged
Conversation
… 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.
thdxr
force-pushed
the
fix/lost-device-surface
branch
from
November 10, 2026 14:22
36d1598 to
c44153c
Compare
…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 was referenced Nov 23, 2026
This branch was previously deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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::configurerecords the surface's error sink only when the configurationsucceeds (wgpu-30.0.1
src/backend/wgpu_core.rs,SurfaceInterface::configure). When thedevice is lost, wgpu-core has already taken the surface's old presentation and returns
DeviceLost, whichhandle_error_innerdrops ("will be surfaced via callback"). So thesurface is left with no presentation and no error sink, and the next
get_current_texturetakes 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
GpuinstallsDevice::set_device_lost_callbackon every Rune-made device and exposesGpu::is_lost.Gpu::from_deviceis the same for a device an app made. AGpuliteraloutside Rune no longer builds; use
Gpu::from_device.render/warm.rs), warmed, shared or handed out, and acached or parked one is dropped instead.
window::configuretakes theGpuand fails when it is lost, before and after theconfigure, since wgpu reports nothing there.
acquiretakes theGpuand whether the surface's last configuration took; either alost device or an unconfigured surface returns
Missed::Unconfiguredwithout askingwgpu for a drawable.
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 thatfails it releases both as a hidden window with
release_gpu_when_closeddoes, recordsAppHandle::window_error, and the next open makes them again.Unconfiguredas before, so the hidden overlay makes new ones on its next show.tests
window/tests.rs: a lost device is reported byconfigureand never drawn with; asurface 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 newacquiresignature.signature follow-ups, no behavior of their own:
window/events.rs,window/stepping.rsand
window/lifecycle.rscallresize_surface(&Gpu);platform/web.rsandcrates/rune/examples/gpu_springs.rsbuild theirGpuwithGpu::from_device.review follow-ups
window::configurepolls the device (PollType::Poll) aftersurface.configureand beforeits lost check:
Device::destroyonly marks the device invalid, and its callback runs on thenext maintain, so the surface could be left with no presentation while
is_loststill readfalse. Regression test: destroy, no poll,
configuremust Err andacquiremust not panic.Win.surfaceis anOption, taken and dropped beforeself.gpu = Noneand before the newsurface is made: a DX12 surface takes the window's one DirectComposition target
(
Dx12SwapchainKind::DxgiFromVisual), so a second surface on the same HWND could never beconfigured while the old one lived.
replace_lost_surfaceretries a failed replacement on a bounded backoff (SurfaceRetries,three tries) and then gives the window up through the same path as
hidewithrelease_gpu_when_closed(wind_down+release, no event loop), recordingAppHandle::window_error; an app can open the window again, which makes a window, device andsurface.
cfg(test), so headless devices and tests in rune, rune-fx, rune-light and downstream keepfailing 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_lostand the loss watch live inrender/gpu_loss.rs;configureand itslost message in
window/configure.rs(file-size rule:render.rs1460 non-test lines,window.rsunder 800).LayerHookis re-attached to the new surface, and native Metal'sset_device_lost_callbackno-op is documented (sois_lostis never true on that backend).