mirror of
https://github.com/ocornut/imgui.git
synced 2026-11-27 00:53:33 +00:00
Backends: DX12: unbounded waits can hang the render thread indefinitely #9632
Labels
No labels
accidentally closed
android
backends
bug
building
clipper
coloredit
color spaces
cplusplus
data types
demo
disabled
doc
docking
dpi
drag drop
drawing/drawlist
dropped
dx12
enhancement
focus
font/text
gallery
help wanted
idle/power saving
ime
in progress
inputs
inputtext
internals
label/id and id stack
language bindings
layout
linux/bsd
menus
multi-contexts
multi-threading
multi-viewports
nav
news/info
opengl
optimization
osx/ios
overlap
plot/graph
popups
question
scrolling
selection
settings
sliders/drags
style
tables/columns
tabs
test wanted
tools
tooltips
tree
tricks & tips
useful widgets
vulkan
web
window pos/depth
window sizing
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
ocornut/imgui#9632
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Version/Branch of Dear ImGui:
1.93.1 WIP (19303), docking, tip
33d9ed0d6Back-ends:
imgui_impl_win32 + imgui_impl_dx12
Compiler, OS:
MSVC 2022 v143, Windows 11 26300, Intel iGPU, Release
Full config/build information:
Build info:
examples/example_win32_directx12/example_win32_directx12.vcxproj, Release | x64PlatformToolset=v143andWindowsTargetPlatformVersion=10.0passed on the MSBuildmain.cpp:141)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
545d1a8says as much: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 asPresent()retires frames. Once the device is removed,Present()fails and the object is never signalled again. Every wait on it isINFINITE, so the render thread parks permanently, stops pumping messages, and Windows marks the window "Not responding".Present()'sHRESULTis only ever compared againstDXGI_STATUS_OCCLUDED(examples/example_win32_directx12/main.cpp:317).DXGI_ERROR_DEVICE_REMOVED,DXGI_ERROR_DEVICE_RESETandDXGI_ERROR_DEVICE_HUNGare ignored, so the loop proceeds to the next frame and blocks.Unbounded waits, on
docking@33d9ed0d6:backends/imgui_impl_dx12.cppImGui_ImplDX12_UpdateTexture()WaitForSingleObject(bd->FenceEvent, INFINITE)backends/imgui_impl_dx12.cppImGui_WaitForPendingOperations()WaitForSingleObject(bd->FenceEvent, INFINITE)backends/imgui_impl_dx12.cppImGui_WaitForNextFrameContext()WaitForMultipleObjects(2, ..., TRUE, INFINITE)backends/imgui_impl_dx12.cppImGui_WaitForNextFrameContext()WaitForSingleObject(vd->SwapChainWaitableObject, INFINITE)examples/example_win32_directx12/main.cppWaitForPendingOperations()WaitForSingleObject(g_fenceEvent, INFINITE)examples/example_win32_directx12/main.cppWaitForNextFrameContext()WaitForMultipleObjects(2, ..., TRUE, INFINITE)examples/example_win32_directx12/main.cppWaitForNextFrameContext()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 testeddocking.Why
545d1a8does not cover thisIn
ImGui_ImplDX12_RenderWindow():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:
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):
Patched line 554 is stock 535, patched 297 is stock 277.
I was able to reproduce the issue in both Debug and Release configurations.