Wayland: a Vulkan error at a resize ends the consumer's process: the swapchain is rebuilt inside the configure listener #200

Open
opened 2026-11-18 05:44:50 +00:00 by jorijnvdgraaf · 0 comments

Summary

Window::xdg_surface_handle_configure applies a configure's new size at once: Resize calls RebuildSwapchain, and every Vulkan call in there goes through Device::CheckVkResult, which throws for any result but VK_SUCCESS. That throw starts inside a Wayland listener. A consumer can put a try around Window::Update and around a window's making, but not around this, so a device that is lost when a configure arrives ends the process. Line numbers refer to c006519.

Details

  • implementations/Crafter.Graphics-Window.cpp:1921-1927: for a configured window whose toplevel configure carried a size, xdg_surface_handle_configure calls window->Resize(newWidth, newHeight).
  • :753-771: Resize holds a new size back only while a frame is being recorded (deferredWidth, deferredHeight). Otherwise it sets width and height and calls RebuildSwapchain.
  • :774-793: RebuildSwapchain begins with Device::CheckVkResult(vkQueueWaitIdle(Device::queue)) and goes on to RecreateSwapchainAndImages, whose CreateSwapchain (:1620) checks vkCreateSwapchainKHR, vkGetSwapchainImagesKHR, vkCreateSemaphore and vkCreateImageView the same way.
  • implementations/Crafter.Graphics-Device.cpp:83: CheckVkResult throws std::runtime_error.

The listener is called from wl_display_dispatch*, so libwayland's own frames lie between the throw and the consumer's code: C, with nothing in it for an exception passing through. A try around the consumer's dispatch call would have to count on unwinding through them. After the throw, width and height hold the new size and the swapchain still has the old one.

Win32's WM_SIZE (:181-191) reaches the same Resize from the window procedure. Not tried there.

Concrete case

imsd-messages 0.0.0 (ca5cd72, the library at c006519) on its bench: a nested KWin 6.8.0 and a Vulkan layer that answers VK_ERROR_DEVICE_LOST from a set moment on. The device is lost while the window is idle, and the compositor then resizes the window before any frame is drawn. From the run's record:

imsd-bench: resizing 400x828 to 480x948; 3 s later the app's process: gone
the app's exit: 134
vkfail: the device is lost, 5801 ms after its creation; vkQueueWaitIdle meets it first
vkfail: vkQueueWaitIdle returns VK_ERROR_DEVICE_LOST
libc++abi: terminating due to uncaught exception of type std::runtime_error: VK_ERROR_DEVICE_LOST

Where a frame meets the loss first, the same build's try around Window::Update catches it and the process ends in order with exit 1. On a phone the on-screen keyboard resizes the window each time it comes or goes, so there a configure is as likely to meet a lost device as a frame is.

Suggested fix

Keep only the size in the listener and rebuild at the start of the next Render, where the try a consumer has around Window::Update covers it. Render does both halves of that already: it rebuilds after VK_ERROR_OUT_OF_DATE_KHR (:1319-1326), and after the present it applies a Resize that came while the frame was being recorded (:1523-1534).

Two things change for consumers and want a line in AGENTS.consumer.md. onResize then fires from that Render, not from the dispatch. And a consumer that draws on demand has to learn at the configure that a frame is owed, because today its onResize listener is what asks for one: an event at the configure would do.

#199 asks for the same move for another reason.

Found by imsd-messages' pre-push review (rounds 69 and 70, 2026-11-17) and shown on its bench. Waived for the app's 0.0.0 with this issue as the follow-up.

## Summary `Window::xdg_surface_handle_configure` applies a configure's new size at once: `Resize` calls `RebuildSwapchain`, and every Vulkan call in there goes through `Device::CheckVkResult`, which throws for any result but `VK_SUCCESS`. That throw starts inside a Wayland listener. A consumer can put a `try` around `Window::Update` and around a window's making, but not around this, so a device that is lost when a configure arrives ends the process. Line numbers refer to `c006519`. ## Details - `implementations/Crafter.Graphics-Window.cpp:1921-1927`: for a configured window whose toplevel configure carried a size, `xdg_surface_handle_configure` calls `window->Resize(newWidth, newHeight)`. - `:753-771`: `Resize` holds a new size back only while a frame is being recorded (`deferredWidth`, `deferredHeight`). Otherwise it sets `width` and `height` and calls `RebuildSwapchain`. - `:774-793`: `RebuildSwapchain` begins with `Device::CheckVkResult(vkQueueWaitIdle(Device::queue))` and goes on to `RecreateSwapchainAndImages`, whose `CreateSwapchain` (`:1620`) checks `vkCreateSwapchainKHR`, `vkGetSwapchainImagesKHR`, `vkCreateSemaphore` and `vkCreateImageView` the same way. - `implementations/Crafter.Graphics-Device.cpp:83`: `CheckVkResult` throws `std::runtime_error`. The listener is called from `wl_display_dispatch*`, so libwayland's own frames lie between the throw and the consumer's code: C, with nothing in it for an exception passing through. A `try` around the consumer's dispatch call would have to count on unwinding through them. After the throw, `width` and `height` hold the new size and the swapchain still has the old one. Win32's `WM_SIZE` (`:181-191`) reaches the same `Resize` from the window procedure. Not tried there. ## Concrete case imsd-messages 0.0.0 (ca5cd72, the library at c006519) on its bench: a nested KWin 6.8.0 and a Vulkan layer that answers `VK_ERROR_DEVICE_LOST` from a set moment on. The device is lost while the window is idle, and the compositor then resizes the window before any frame is drawn. From the run's record: ``` imsd-bench: resizing 400x828 to 480x948; 3 s later the app's process: gone the app's exit: 134 vkfail: the device is lost, 5801 ms after its creation; vkQueueWaitIdle meets it first vkfail: vkQueueWaitIdle returns VK_ERROR_DEVICE_LOST libc++abi: terminating due to uncaught exception of type std::runtime_error: VK_ERROR_DEVICE_LOST ``` Where a frame meets the loss first, the same build's `try` around `Window::Update` catches it and the process ends in order with exit 1. On a phone the on-screen keyboard resizes the window each time it comes or goes, so there a configure is as likely to meet a lost device as a frame is. ## Suggested fix Keep only the size in the listener and rebuild at the start of the next `Render`, where the `try` a consumer has around `Window::Update` covers it. `Render` does both halves of that already: it rebuilds after `VK_ERROR_OUT_OF_DATE_KHR` (`:1319-1326`), and after the present it applies a `Resize` that came while the frame was being recorded (`:1523-1534`). Two things change for consumers and want a line in `AGENTS.consumer.md`. `onResize` then fires from that `Render`, not from the dispatch. And a consumer that draws on demand has to learn at the configure that a frame is owed, because today its `onResize` listener is what asks for one: an event at the configure would do. #199 asks for the same move for another reason. Found by imsd-messages' pre-push review (rounds 69 and 70, 2026-11-17) and shown on its bench. Waived for the app's 0.0.0 with this issue as the follow-up.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Catcrafts/Crafter.Graphics#200
No description provided.