Viewer: exit cleanly after a GPU device loss (SIX-234) #415

Manually merged
github merged 1 commit from gh-pr/415/head into gh-pr/415/base 2026-11-27 00:39:30 +00:00
Owner

@⁠holdmysocks on GitHub · 2026-11-26 21:47 UTC · original
holdmysocks:six-234-device-loss → master · +98 −5 · 4 files · 1 commits · merged 2026-11-26 23:26 UTC

Fixes SIX-234. After #390, a real device loss still ended in exit code 101. egui-wgpu 0.36.2 panicked on the next paint (renderer.rs:984, "Failed to create staging buffer for index data"), so the red "restart the viewer" line was never drawn.

What changes

Once the device is lost, the viewer shows a native message box ("The GPU device was lost … Restart the viewer.") and exits with code 3.

  • App::logic and App::ui check the GPU_LOST flag before any 3D view, screenshot or gpu_time read-back touches the device. ui checks it again after the frame is built and before egui-wgpu paints it.
  • A panic hook turns any panic that happens after the loss into the same clean exit, whether in egui-wgpu, a wgpu helper such as create_buffer_init, or an upload thread. Before the loss, panics behave as they always did.
  • The Map 3D model and cell upload threads stop once the flag is set, so nothing more is written to the lost device.
  • With --exit (unattended runs) the message box is skipped: the run logs the reason and exits 3.
  • #390's red in-window line is removed: it could never be drawn.

Why a message box and exit rather than keeping the window alive

eframe gives the app no way to skip egui-wgpu's paint, and every paint needs the lost device: the surface texture plus the vertex and index staging buffers. Keeping the window alive would mean rebuilding the device and every renderer, texture and cache the viewer holds. A message box needs no GPU. Together with the panic hook, it also covers paths the frame-start check can't reach, such as a loss during egui's paint or on another thread. Restarting is what the user has to do either way.

Testing without a driver fault

SIXNG_TEST_GPU_LOSS=N destroys the device after N frames. A real fault marks the device invalid and calls the lost callback in one step. destroy() calls the callback only from a later poll, once the queue has drained. So the hook sets the flag first, the way the callback would, then polls so the callback logs its reason. Documented in the README.

  • Before the frame-end check and the hook ordering, the hook reproduced the exact egui-wgpu panic (renderer.rs:984), plus the frame-timer "read-back buffer destroyed" error from the original report. The panic hook turned it into exit 3.
  • Model viewer, the Byron repro, loss after 60 frames: 8 runs, all exit 3, no panic.
  • Map 3D, loss after 240 frames while streaming: 3 runs on the final code, all exit 3. Each logs one wgpu create_buffer_init panic from that frame's 3D drawing, which the hook catches. The default panic handler never ran.
  • Without --exit, the message box showed over the viewer, and pressing OK closed it.
  • The same model shot without the env var: exit 0, screenshot written.
  • cargo test --release -p sixng-gui: 53 passed. cargo check --release --no-default-features -p sixng-gui: clean.

All runs on the RTX 4080.

**@⁠holdmysocks** on GitHub · 2026-11-26 21:47 UTC · [original](https://github.com/holdmysocks/SixNG/pull/415) `holdmysocks:six-234-device-loss` → `master` · +98 −5 · 4 files · 1 commits · merged 2026-11-26 23:26 UTC Fixes SIX-234. After #390, a real device loss still ended in exit code 101. egui-wgpu 0.36.2 panicked on the next paint (renderer.rs:984, "Failed to create staging buffer for index data"), so the red "restart the viewer" line was never drawn. ## What changes Once the device is lost, the viewer shows a **native message box** ("The GPU device was lost … Restart the viewer.") and **exits with code 3**. - `App::logic` and `App::ui` check the `GPU_LOST` flag before any 3D view, screenshot or `gpu_time` read-back touches the device. `ui` checks it again after the frame is built and before egui-wgpu paints it. - A panic hook turns any panic that happens after the loss into the same clean exit, whether in egui-wgpu, a wgpu helper such as `create_buffer_init`, or an upload thread. Before the loss, panics behave as they always did. - The Map 3D model and cell upload threads stop once the flag is set, so nothing more is written to the lost device. - With `--exit` (unattended runs) the message box is skipped: the run logs the reason and exits 3. - #390's red in-window line is removed: it could never be drawn. ## Why a message box and exit rather than keeping the window alive eframe gives the app no way to skip egui-wgpu's paint, and every paint needs the lost device: the surface texture plus the vertex and index staging buffers. Keeping the window alive would mean rebuilding the device and every renderer, texture and cache the viewer holds. A message box needs no GPU. Together with the panic hook, it also covers paths the frame-start check can't reach, such as a loss during egui's paint or on another thread. Restarting is what the user has to do either way. ## Testing without a driver fault `SIXNG_TEST_GPU_LOSS=N` destroys the device after N frames. A real fault marks the device invalid and calls the lost callback in one step. `destroy()` calls the callback only from a later poll, once the queue has drained. So the hook sets the flag first, the way the callback would, then polls so the callback logs its reason. Documented in the README. - Before the frame-end check and the hook ordering, the hook reproduced the exact egui-wgpu panic (renderer.rs:984), plus the frame-timer "read-back buffer destroyed" error from the original report. The panic hook turned it into exit 3. - Model viewer, the Byron repro, loss after 60 frames: 8 runs, all exit 3, no panic. - Map 3D, loss after 240 frames while streaming: 3 runs on the final code, all exit 3. Each logs one wgpu `create_buffer_init` panic from that frame's 3D drawing, which the hook catches. The default panic handler never ran. - Without `--exit`, the message box showed over the viewer, and pressing OK closed it. - The same model shot without the env var: exit 0, screenshot written. - `cargo test --release -p sixng-gui`: 53 passed. `cargo check --release --no-default-features -p sixng-gui`: clean. All runs on the RTX 4080.
After a device loss egui-wgpu panicked on the next paint ("Failed to
create staging buffer"), so the viewer exited with 101 before the red
restart line from #390 was drawn. eframe has no way to skip a paint, so
once the device is lost the viewer now shows a native message box (no
GPU needed) and exits with code 3:

- App::logic and App::ui check the flag before any 3D view, screenshot
  or frame timer touches the device, and again after the frame is built,
  before egui-wgpu paints it.
- A panic hook turns any panic after the loss (egui-wgpu, a wgpu helper,
  an upload thread) into the same clean exit.
- The Map 3D model and cell upload threads stop once the flag is set.
- SIXNG_TEST_GPU_LOSS=N destroys the device after N frames, for testing.
  `--exit` skips the box for unattended runs.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 00:39:28 +00:00
github manually merged commit 026d5496ec into gh-pr/415/base 2026-11-27 00:39:30 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.