Handling VK_ERROR_INITIALIZATION_FAILED in vkCreateSwapchainKHR on window resize

When I resize a window, or maximize or restore it, sometimes (like 1/50 of the times) I get a VK_ERROR_INITIALIZATION_FAILED error when recreating the swapchain with vkCreateSwapchainKHR. I gather from here that this error could occur if the window size has changed again between the time one last queried the window size (using vkGetPhysicalDeviceSurfaceCapabilitiesKHR()) and when you then try to recreate the swapchain. I don’t know however how to then handle the error. I’ve tried re-queryng the window size (which indeed returns a different size than the one returned previously) but when I then recreate the swapchain, I get a VK_ERROR_NATIVE_WINDOW_IN_USE_KHR error. I don’t understand why the window may be in use, as before trying to recreate the swapchain I’ve destroyed the previous one (which was ‘retired’ on the first failed vkCreateSwapchainKHR() call to which I passed the current surface as the oldSwapchain parameter, after which I destroy it and its images). I even tried destroying and recreating the window’s vkSurface but I still get the error.

The only way I can currently get around the issue is by destroying the current swapchain before recreating it with vkCreateSwapchainKHR(), but that goes against the recommended practice of passing the current swapchain as oldSwapchain. When I avoid this I never get the VK_ERROR_INITIALIZATION_FAILED but I can see that resizing a window is noticeably slower.

I’m testing this on Ubuntu 22.04 with X11 with Nvidia drivers (I’ve tried various driver version).

Thanks for any help.

Vk guide, chapter 3 window resizing should have your back

Thanks but I think I’m already doing what the guide says, handling VK_ERROR_OUT_OF_DATE_KHR. It works most of the time, only occasionally I get the VK_ERROR_INITIALIZATION_FAILED error and when that happens I don’t know how to recover. And again it works always if I don’t use oldSwapchain when recreating the swapchain after VK_ERROR_OUT_OF_DATE_KHR, but if I do that, resizes are noticeable slower than when passing the old swapchain as oldSwapchain to vkCreateSwapchainKHR().