imgui: Context recovery / graceful handling of bad state
I’m using vanilla Dear Imgui v1.60 WIP.
I have an app that exposes imgui API to plugins using a scripting language. Now, in my own code I can be sure that I am “well-behaved” - for example, I match calls to ImGui::Begin() with calls to ImGui::End(). But I can’t be sure that plugins will be the same. I also have live reloading of plugin scripts, and sometimes I end up trying to load plugin code that won’t compile (I can keep using the last-known-good version in this case) or worse: plugin code that isn’t well-behaved with respect to imgui API. Consider a plugin that consists entirely of [the scrip equivalent of] the following:
ImGui::Begin("This won't end well.");
I end up hitting this assertion:
IM_ASSERT(g.CurrentWindowStack.Size == 1); // Mismatched Begin()/End() calls
As a feature request, is it possible to consider supporting more graceful failures? I don’t have a solid idea in mind, but you can see my use case above. It could be something like D3D12’s command lists, where the CloseCommandList() function returns a status code indicating whether this was a valid or malformed command list - maybe ImGui::EndFrame() could return a similar value and just drop the frame altogether, or render as much of it as it can [and I realize how this is a fuzzy concept - I’m just thinking out loud here].
Is there some other way to do this that I’m missing? I wouldn’t mind running the plugin code in its own imgui context (multiple context support rocks!), but currently that doesn’t seem to solve the problem I have.
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Reactions: 3
- Comments: 52 (28 by maintainers)
Commits related to this issue
- Error recovery: Extraneous/undesired calls to End() are now being caught by an assert in the End() function itself at the call site (instead of being reported in EndFrame). Past the assert, they don't... — committed to ocornut/imgui by ocornut 8 years ago
- Fixes crash/assert bug introduced in d845135 (#1651): would assert when showing the CTRL+Tab list and or fallback "...." tooltip. — committed to ocornut/imgui by ocornut 8 years ago
- Revert "Fixes crash/assert bug introduced in d845135 (#1651): would assert when showing the CTRL+Tab list and or fallback "...." tooltip." This reverts commit 1b0e38df47e7b29932717e695e4a76e0bcbec55b... — committed to ocornut/imgui by ocornut 8 years ago
- Alternative fix for bug introduced in d845135 (#1651), fix CTRL+Tab and fallback tooltip. — committed to ocornut/imgui by ocornut 8 years ago
- TabBar: Added extra mis-usage error recovery. Past the assert, common mis-usage don't lead to hard crashes any more, facilitating integration with scripting languages. (#1651) — committed to ocornut/imgui by ocornut 8 years ago
- Error handling: Assert if user mistakenly calls End() instead of EndChild() on a child window. (#1651) Internals: Moved some error handling code. — committed to ocornut/imgui by ocornut 7 years ago
- Internals: Routing recoverable user errors via IMGUI_USER_ERROR() macro. (#1651) — committed to ocornut/imgui by ocornut 7 years ago
- Internals: added experimental ErrorCheckEndFrameRecover() to unroll/end/pop. (#1651, #3600) — committed to ocornut/imgui by ocornut 6 years ago
- Disabled: Added assert guard for mismatching BeginDisabled()/EndDisabled() blocks. (#211) + Added asserts for missing PopItemFlag() calls. Added both to ErrorCheckEndFrameRecover (#1651) — committed to ocornut/imgui by ocornut 5 years ago
- Misc: extracted ErrorCheckEndWindowRecover() out of ErrorCheckEndFrameRecover(). (#1651) — committed to ocornut/imgui by ocornut 5 years ago
- Better error reporting for PopStyleColor()/PopStyleVar() + easier to recover. (#1651) — committed to ocornut/imgui by ocornut 4 years ago
- Misc: made ErrorCheckEndWindowRecover() handle font stack. (#6431, #1651) — committed to ocornut/imgui by ocornut 4 years ago
- Error check: clarified that carriage returns are emitted by our code. Added helper default callback. Comments. (#1651) (doesn't affect test engine hook for it as trailing \n are trimmed anyhow) — committed to ocornut/imgui by ocornut 2 years ago
- Error Handling: fixed an issue ErrorCheckEndWindowRecover() when aborting in a child inside a tab bar. (#1651) — committed to ocornut/imgui by ocornut 2 years ago
- Error handling: rework error tooltip logic (will be reused by upcoming feature). (#7961, #7669, #1651) + Comments — committed to ocornut/imgui by ocornut 2 years ago
- Error Handling, Debug Log: added IMGUI_DEBUG_LOG_ERROR() with special handling. (#5855, #1651, #5654) — committed to ocornut/imgui by ocornut 2 years ago
- Error Handling: rewired asserts in PopID(), PopFont(), PopItemFlag(), EndDisabled(), PopTextWrapPos(), PopFocusScope(), PopItemWidth() to use IM_ASSERT_USER_ERROR(). (#1651, #5654) — committed to ocornut/imgui by ocornut 2 years ago
- Error Handling: replaced log callback in recovery functions with calls to IM_ASSERT_USER_ERROR(). (#1651, #5654) This commit is not meant to be functional as-is (it will break test engine recovery). ... — committed to ocornut/imgui by ocornut 2 years ago
- Error Handling: enabled experimental recovery systems. (#1651, #5654) Setup a couple of features to configure them, including ways to display error tooltips instead of assserting. — committed to ocornut/imgui by ocornut 2 years ago
- Error Handling: fixed not rewinding to recorded tree and id stack size (#1651) — committed to ocornut/imgui by ocornut 2 years ago
Pushed this in master as 9712bff Note this is technically undocumented/internals api so come with little guarantee and won’t be widely advertised but we’ve been using it in the TestEngine.
I’m working on a few things
The great bit is that if you use ImGuiErrorFlags_NoAssert you can get away with a tooltip:
Edit:final API/names are different but essentially similar features.
Branch https://github.com/ocornut/imgui/compare/features/error_recovery?expand=1
I need to stress that it is unlikely that recovery is going to be 100% perfect all the time but I believe if it handles 99% of realistic error cases then we may be good. This is not merged yet. Curious if you have feedback on whether this can be useful to your case.
The only feature “regression” is that if you are to disable _EnableRecovery it doesn’t get you exactly the same asserts as before.
I believe if the feature works it should be a non-issue, but I would need to see how it behave in real world vs my few tests.
(I think I may simultaneously start working on official scoped/raii helpers so people have them in a a separate header, even if I don’t fancy them)
Could you open an issue with those two, for tracking, preferably with a basic most repro? Thank you.
Now that we’ve shipped our major update and I’ve had a bit of time to breathe, I’m finally migrating Pioneer from ImGui 1.89.9 to 1.91.9b.
Feedback regarding error handling:
ErrorRecoveryTryToRecoverStateinside a table before a valid call toEndTable()would prematurely end that table? I know it’s a very specialized scenario, and I personally don’t have an organic repro case in the Pioneer codebase, but I think it’s worth considering.In general, the error recovery feature works very well and neatly replaces the existing cobbled-together error handling we’ve been carrying.
(By asking questions or posting self-answer it is essentially asking me to interact which defeat the purpose of create new issues to silo or track things. I’m answering here assuming this is a closure on this topic. It’s my fault for not reacting to your first post which nicely actually asked “Is this still the correct issue to post on?”).
As per https://github.com/ocornut/imgui/wiki/Error-Handling, the idea is that you can call
ErrorRecoveryTryToRecoverState()yourself right after your script execution without waiting forEndFrame(). So you have a finer control of when things are recovered/logged and with which flags. Also useful if you need to differentiate one script from another when handling errors.One additional concern for your consideration is that the recovery feature may also be usable / extremely useful in a “graceful exit” scenario in the presence of external error handling.
We’re currently using a slightly patched and integrated version of ImGui 1.89.8’s
ErrorCheckEndWindowRecoverin Pioneer Space Sim as a way to ensure ImGui is left in a sane state if Lua code (the main user of the ImGui API) throws an error; when entering a protected call anImGuiStackSizesobject is filled and saved and if an error is thrown the error handler unwinds the various ImGui stacks.Of note, we’ve had to patch
ImGuiStackSizesto add an additionalSizeOfTabBarStackvariable - from my off-hand skimming at the moment I think the error handling code in 1.89.8 will produce an invalid result if running ErrorCheckEndWindowRecover in a child window nested inside a tab bar item. We also recover the IDStack to its saved state rather than a depth of1, and I suspect the TreeDepth state also should be saved and recovered for completeness.The proposed
TryToRecoverWindowStatefunction I believe would be extremely useful to this specific use case, in addition to the other changes in that branch - we’d definitely benefit from the ability to haveIM_ASSERT_USER_ERRORlog errors rather than remaining unhandled and leading to a segfault at worst.We’ve been using that function for a long time in the test engine. It’s not perfect but doesn’t make things worse, and recovers many cases.
The way we’ve been handling stack unrolling on error in Pioneer has been to save a copy of the stack sizes, including the CurrentWindowStackSize, when entering a protected call (
try {} catch-equivalent) and to then handle the necessary End/Pops if an error occurred during the protected call. Looking at ErrorCheckEndFrameRecover, this appears to be very similar to what we are doing; however ImGui’s function unwinds the entire stack instead of supporting mid-frame unrolling.Would you be interested in a pull request that allows unrolling to a “known state” (implemented with
ImGuiStackSizes) mid-frame? It would need a few minutes to detach from Pioneer’s logging system but is otherwise based on the existing ImGui functionality with a few extra variables to enable unrolling to a “mid-window” state.