Backends: DX12: unbounded waits can hang the render thread indefinitely #9632

Closed
opened 2026-11-20 19:05:22 +00:00 by grzybr1 · 0 comments
grzybr1 commented 2026-11-20 19:05:22 +00:00 (Migrated from github.com)

Version/Branch of Dear ImGui:

1.93.1 WIP (19303), docking, tip 33d9ed0d6

Back-ends:

imgui_impl_win32 + imgui_impl_dx12

Compiler, OS:

MSVC 2022 v143, Windows 11 26300, Intel iGPU, Release

Full config/build information:

Dear ImGui 1.93.1 WIP (19303)
--------------------------------
sizeof(size_t): 8, sizeof(ImDrawIdx): 2, sizeof(ImDrawVert): 20
define: __cplusplus=199711
define: _WIN32
define: _WIN64
define: _MSC_VER=1944
define: _MSVC_LANG=201402
define: IMGUI_HAS_VIEWPORT
define: IMGUI_HAS_DOCK
IM_ASSERT: runs expression: OK. expand size: OK
--------------------------------
io.BackendPlatformName: imgui_impl_win32
io.BackendRendererName: imgui_impl_dx12
io.ConfigFlags: 0x00000483
 NavEnableKeyboard
 NavEnableGamepad
 DockingEnable
 ViewportsEnable
io.ConfigDpiScaleFonts
io.ConfigDpiScaleViewports
io.ConfigViewportsNoDecoration
io.ConfigViewportsNoDefaultParent
io.ConfigNavCaptureKeyboard
io.ConfigInputTextCursorBlink
io.ConfigWindowsResizeFromEdges
io.ConfigMemoryCompactTimer = 60.0
io.BackendFlags: 0x00003C1E
 HasMouseCursors
 HasSetMousePos
 PlatformHasViewports
 HasMouseHoveredViewport
 HasParentViewport
 RendererHasVtxOffset
 RendererHasTextures
 RendererHasViewports
--------------------------------
io.Fonts: 1 fonts, Flags: 0x00000000, TexSize: 512,128
io.Fonts->FontLoaderName: stb_truetype
io.DisplaySize: 1264.00,761.00
io.DisplayFramebufferScale: 1.00,1.00
--------------------------------
style.WindowPadding: 8.00,8.00
style.WindowBorderSize: 1.00
style.FramePadding: 4.00,3.00
style.FrameRounding: 3.00
style.FrameBorderSize: 0.00
style.ItemSpacing: 8.00,4.00
style.ItemInnerSpacing: 4.00,4.00

Build info:

  • on top of stock examples/example_win32_directx12/example_win32_directx12.vcxproj, Release | x64
  • PlatformToolset=v143 and WindowsTargetPlatformVersion=10.0 passed on the MSBuild
  • Multi-viewports enabled (the example's default, main.cpp:141)
  • D3D12 device was created on the Intel adapter (Intel UMD loaded, no WARP)

Details:

Problem

We suffer from occasional hangs in our Dear ImGui application. Investigating them prompted me to create this issue: I have not been able to identify the root cause in the field, but I have a way to reproduce it and I believe the hangs can be stopped by improving the DX12 backend structurally.

Several waits in the DX12 backend are unbounded, and in some places the code does not handle predictable failures of the calls responsible for signalling the objects being waited on. Any failure that stops them being signalled parks the render thread forever.

The problem is not new, I am aware of two relevant issues raised here:

I believe both are relevant to us even though we are on DX12 - and for #9546, the fix commit 545d1a8 says as much:

Backends: DX10, DX11, DX12: fixed a crash rendering multi-viewports if a ClearRenderTargetView() or CreateSwapChain() call failed. (#9546)
Likely to be incomplete for DX12.

Goal/Expectations

I would like to stop those hangs, and created a PR #9633 with a fix that would do that (deliberately minimal, so as not to pre-empt your design decisions or second-guess the bigger picture).

DX9 already takes the position that the backend surfaces device loss and the application recovers (imgui_impl_dx9.cpp:617). DX12 has no equivalent, and because the wait never returns, the application has no opportunity to do anything about it.

Context

We have a viewer application using Dear ImGui (we also support the project by purchasing Dear ImGui Test Engine).

In our use case we care about the app running for long periods of time (it gathers "live" data in memory for presentation/monitoring purposes).

Our application is based on a very slightly modified DX12 example.

Details

The DX12 swapchains are created with DXGI_SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT. DXGI signals that object as Present() retires frames. Once the device is removed, Present() fails and the object is never signalled again. Every wait on it is INFINITE, so the render thread parks permanently, stops pumping messages, and Windows marks the window "Not responding".

Present()'s HRESULT is only ever compared against DXGI_STATUS_OCCLUDED (examples/example_win32_directx12/main.cpp:317). DXGI_ERROR_DEVICE_REMOVED, DXGI_ERROR_DEVICE_RESET and DXGI_ERROR_DEVICE_HUNG are ignored, so the loop proceeds to the next frame and blocks.

Unbounded waits, on docking@33d9ed0d6:

File Line Function Call
backends/imgui_impl_dx12.cpp 648 ImGui_ImplDX12_UpdateTexture() WaitForSingleObject(bd->FenceEvent, INFINITE)
backends/imgui_impl_dx12.cpp 1188 ImGui_WaitForPendingOperations() WaitForSingleObject(bd->FenceEvent, INFINITE)
backends/imgui_impl_dx12.cpp 1200 ImGui_WaitForNextFrameContext() WaitForMultipleObjects(2, ..., TRUE, INFINITE)
backends/imgui_impl_dx12.cpp 1204 ImGui_WaitForNextFrameContext() WaitForSingleObject(vd->SwapChainWaitableObject, INFINITE)
examples/example_win32_directx12/main.cpp 522 WaitForPendingOperations() WaitForSingleObject(g_fenceEvent, INFINITE)
examples/example_win32_directx12/main.cpp 532 WaitForNextFrameContext() WaitForMultipleObjects(2, ..., TRUE, INFINITE)
examples/example_win32_directx12/main.cpp 535 WaitForNextFrameContext() WaitForSingleObject(g_hSwapChainWaitableObject, INFINITE)

Some of those unbounded waits also exist on master (backends/imgui_impl_dx12.cpp:572, examples/example_win32_directx12/main.cpp:502/512/515). I have only tested docking.

Why 545d1a8 does not cover this

In ImGui_ImplDX12_RenderWindow():

if (vd->SwapChain == nullptr)      // imgui_impl_dx12.cpp:1264
    return;

ImGui_ImplDX12_FrameContext* frame_context = ImGui_WaitForNextFrameContext(vd);   // :1267

After a device removal the swapchain pointer is still non-null, so the guard does not fire and control goes straight into the wait on the next line. It closes the crash path from #9546, but not the hang.

Screenshots/Video:

n/a

Minimal, Complete and Verifiable Example code:

Below is an MCVE, please note that it is by necessity somewhat artificial, but nonetheless it does exercise the code path that would be taken in case of an unclean GPU removal, a driver reset, or a Timeout Detection and Recovery (TDR) event:

--- a/examples/example_win32_directx12/main.cpp
+++ b/examples/example_win32_directx12/main.cpp
@@ -233,7 +233,26 @@
         ImGui_ImplDX12_NewFrame();
         ImGui_ImplWin32_NewFrame();
         ImGui::NewFrame();
 
+        // [REPRO] Simulate a GPU device removal, as caused by undocking a laptop, a driver
+        // reset or a TDR. Reproduces in both Debug and Release: the IM_ASSERT() calls in the
+        // backend are in ImGui_WaitForPendingOperations(), which is not on the render path.
+        {
+            ImGui::Begin("Device removal repro");
+            ImGui::Text("Drag this window out of the main window to make it a viewport,");
+            ImGui::Text("then press the button. Both windows stop repainting permanently.");
+            if (ImGui::Button("Remove device"))
+            {
+                ID3D12Device5* device5 = nullptr;
+                if (SUCCEEDED(g_pd3dDevice->QueryInterface(IID_PPV_ARGS(&device5))))
+                {
+                    device5->RemoveDevice();
+                    device5->Release();
+                }
+            }
+            ImGui::End();
+        }
+
         // 1. Show the big demo window (Most of the sample code is in ImGui::ShowDemoWindow()! You can browse its code to learn more about Dear ImGui!).
         if (show_demo_window)
             ImGui::ShowDemoWindow(&show_demo_window);

ID3D12Device5::RemoveDevice() is Microsoft's documented test-only API for removing a device, so this uses the vendor's own fault-injection hook rather than provoking undefined behaviour. It needs no extra includes - ID3D12Device5 comes from d3d12.h, which the example already includes.

This will produce a stack trace like below (line numbers valid with the patch applied):

 ntdll.dll!NtWaitForSingleObject()
 KernelBase.dll!WaitForSingleObjectEx()
>[Inline Frame] example_win32_directx12.exe!WaitForNextFrameContext() Line 554
 example_win32_directx12.exe!main(int __formal, char * * __formal) Line 297
 [Inline Frame] example_win32_directx12.exe!invoke_main() Line 78
 example_win32_directx12.exe!__scrt_common_main_seh() Line 288
 kernel32.dll!BaseThreadInitThunk()
 ntdll.dll!RtlUserThreadStart()

Patched line 554 is stock 535, patched 297 is stock 277.

I was able to reproduce the issue in both Debug and Release configurations.

### Version/Branch of Dear ImGui: 1.93.1 WIP (19303), docking, tip 33d9ed0d6 ### Back-ends: imgui_impl_win32 + imgui_impl_dx12 ### Compiler, OS: MSVC 2022 v143, Windows 11 26300, Intel iGPU, Release ### Full config/build information: ```cpp Dear ImGui 1.93.1 WIP (19303) -------------------------------- sizeof(size_t): 8, sizeof(ImDrawIdx): 2, sizeof(ImDrawVert): 20 define: __cplusplus=199711 define: _WIN32 define: _WIN64 define: _MSC_VER=1944 define: _MSVC_LANG=201402 define: IMGUI_HAS_VIEWPORT define: IMGUI_HAS_DOCK IM_ASSERT: runs expression: OK. expand size: OK -------------------------------- io.BackendPlatformName: imgui_impl_win32 io.BackendRendererName: imgui_impl_dx12 io.ConfigFlags: 0x00000483 NavEnableKeyboard NavEnableGamepad DockingEnable ViewportsEnable io.ConfigDpiScaleFonts io.ConfigDpiScaleViewports io.ConfigViewportsNoDecoration io.ConfigViewportsNoDefaultParent io.ConfigNavCaptureKeyboard io.ConfigInputTextCursorBlink io.ConfigWindowsResizeFromEdges io.ConfigMemoryCompactTimer = 60.0 io.BackendFlags: 0x00003C1E HasMouseCursors HasSetMousePos PlatformHasViewports HasMouseHoveredViewport HasParentViewport RendererHasVtxOffset RendererHasTextures RendererHasViewports -------------------------------- io.Fonts: 1 fonts, Flags: 0x00000000, TexSize: 512,128 io.Fonts->FontLoaderName: stb_truetype io.DisplaySize: 1264.00,761.00 io.DisplayFramebufferScale: 1.00,1.00 -------------------------------- style.WindowPadding: 8.00,8.00 style.WindowBorderSize: 1.00 style.FramePadding: 4.00,3.00 style.FrameRounding: 3.00 style.FrameBorderSize: 0.00 style.ItemSpacing: 8.00,4.00 style.ItemInnerSpacing: 4.00,4.00 ``` Build info: - on top of stock `examples/example_win32_directx12/example_win32_directx12.vcxproj`, Release | x64 - `PlatformToolset=v143` and `WindowsTargetPlatformVersion=10.0` passed on the MSBuild - Multi-viewports enabled (the example's default, `main.cpp:141`) - D3D12 device was created on the Intel adapter (Intel UMD loaded, no WARP) ### Details: ## Problem We suffer from occasional hangs in our Dear ImGui application. Investigating them prompted me to create this issue: I have not been able to identify the root cause in the field, but I have a way to reproduce it and I believe the hangs can be stopped by improving the DX12 backend structurally. **Several waits in the DX12 backend are unbounded, and in some places the code does not handle predictable failures of the calls responsible for signalling the objects being waited on. Any failure that stops them being signalled parks the render thread forever.** The problem is not new, I am aware of two relevant issues raised here: * #9546 * #8418 I believe both are relevant to us even though we are on DX12 - and for #9546, the fix commit 545d1a8 says as much: > Backends: DX10, DX11, DX12: fixed a crash rendering multi-viewports if a ClearRenderTargetView() or CreateSwapChain() call failed. (#9546) > Likely to be incomplete for DX12. ## Goal/Expectations I would like to stop those hangs, and created a PR #9633 with a fix that would do that (deliberately minimal, so as not to pre-empt your design decisions or second-guess the bigger picture). DX9 already takes the position that the backend surfaces device loss and the application recovers (`imgui_impl_dx9.cpp:617`). DX12 has no equivalent, and because the wait never returns, the application has no opportunity to do anything about it. ## Context We have a viewer application using Dear ImGui (we also support the project by purchasing [Dear ImGui Test Engine](https://github.com/ocornut/imgui_test_engine)). In our use case we care about the app running for long periods of time (it gathers "live" data in memory for presentation/monitoring purposes). Our application is based on a very slightly modified DX12 example. ## Details The DX12 swapchains are created with `DXGI_SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT`. DXGI signals that object as `Present()` retires frames. Once the device is removed, `Present()` fails and the object is never signalled again. Every wait on it is `INFINITE`, so the render thread parks permanently, stops pumping messages, and Windows marks the window "Not responding". `Present()`'s `HRESULT` is only ever compared against `DXGI_STATUS_OCCLUDED` (`examples/example_win32_directx12/main.cpp:317`). `DXGI_ERROR_DEVICE_REMOVED`, `DXGI_ERROR_DEVICE_RESET` and `DXGI_ERROR_DEVICE_HUNG` are ignored, so the loop proceeds to the next frame and blocks. Unbounded waits, on `docking@33d9ed0d6`: | File | Line | Function | Call | |------|------|----------|------| | `backends/imgui_impl_dx12.cpp` | 648 | `ImGui_ImplDX12_UpdateTexture()` | `WaitForSingleObject(bd->FenceEvent, INFINITE)` | | `backends/imgui_impl_dx12.cpp` | 1188 | `ImGui_WaitForPendingOperations()` | `WaitForSingleObject(bd->FenceEvent, INFINITE)` | | `backends/imgui_impl_dx12.cpp` | 1200 | `ImGui_WaitForNextFrameContext()` | `WaitForMultipleObjects(2, ..., TRUE, INFINITE)` | | `backends/imgui_impl_dx12.cpp` | 1204 | `ImGui_WaitForNextFrameContext()` | `WaitForSingleObject(vd->SwapChainWaitableObject, INFINITE)` | | `examples/example_win32_directx12/main.cpp` | 522 | `WaitForPendingOperations()` | `WaitForSingleObject(g_fenceEvent, INFINITE)` | | `examples/example_win32_directx12/main.cpp` | 532 | `WaitForNextFrameContext()` | `WaitForMultipleObjects(2, ..., TRUE, INFINITE)` | | `examples/example_win32_directx12/main.cpp` | 535 | `WaitForNextFrameContext()` | `WaitForSingleObject(g_hSwapChainWaitableObject, INFINITE)` | Some of those unbounded waits also exist on `master` (`backends/imgui_impl_dx12.cpp:572`, `examples/example_win32_directx12/main.cpp:502/512/515`). I have only tested `docking`. ### Why 545d1a8 does not cover this In `ImGui_ImplDX12_RenderWindow()`: ```cpp if (vd->SwapChain == nullptr) // imgui_impl_dx12.cpp:1264 return; ImGui_ImplDX12_FrameContext* frame_context = ImGui_WaitForNextFrameContext(vd); // :1267 ``` After a device removal the swapchain pointer is still non-null, so the guard does not fire and control goes straight into the wait on the next line. It closes the crash path from #9546, but not the hang. ### Screenshots/Video: n/a ### Minimal, Complete and Verifiable Example code: Below is an MCVE, please note that it is by necessity somewhat artificial, but nonetheless it does exercise the code path that would be taken in case of an unclean GPU removal, a driver reset, or a Timeout Detection and Recovery (TDR) event: ```diff --- a/examples/example_win32_directx12/main.cpp +++ b/examples/example_win32_directx12/main.cpp @@ -233,7 +233,26 @@ ImGui_ImplDX12_NewFrame(); ImGui_ImplWin32_NewFrame(); ImGui::NewFrame(); + // [REPRO] Simulate a GPU device removal, as caused by undocking a laptop, a driver + // reset or a TDR. Reproduces in both Debug and Release: the IM_ASSERT() calls in the + // backend are in ImGui_WaitForPendingOperations(), which is not on the render path. + { + ImGui::Begin("Device removal repro"); + ImGui::Text("Drag this window out of the main window to make it a viewport,"); + ImGui::Text("then press the button. Both windows stop repainting permanently."); + if (ImGui::Button("Remove device")) + { + ID3D12Device5* device5 = nullptr; + if (SUCCEEDED(g_pd3dDevice->QueryInterface(IID_PPV_ARGS(&device5)))) + { + device5->RemoveDevice(); + device5->Release(); + } + } + ImGui::End(); + } + // 1. Show the big demo window (Most of the sample code is in ImGui::ShowDemoWindow()! You can browse its code to learn more about Dear ImGui!). if (show_demo_window) ImGui::ShowDemoWindow(&show_demo_window); ``` ID3D12Device5::RemoveDevice() is Microsoft's documented test-only API for removing a device, so this uses the vendor's own fault-injection hook rather than provoking undefined behaviour. It needs no extra includes - ID3D12Device5 comes from d3d12.h, which the example already includes. This will produce a stack trace like below (line numbers valid with the patch applied): ``` ntdll.dll!NtWaitForSingleObject() KernelBase.dll!WaitForSingleObjectEx() >[Inline Frame] example_win32_directx12.exe!WaitForNextFrameContext() Line 554 example_win32_directx12.exe!main(int __formal, char * * __formal) Line 297 [Inline Frame] example_win32_directx12.exe!invoke_main() Line 78 example_win32_directx12.exe!__scrt_common_main_seh() Line 288 kernel32.dll!BaseThreadInitThunk() ntdll.dll!RtlUserThreadStart() ``` Patched line 554 is stock 535, patched 297 is stock 277. I was able to reproduce the issue in both Debug and Release configurations.
Sign in to join this conversation.
No milestone
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
ocornut/imgui#9632
No description provided.