fix: SG-43709: ImGuiPythonBridge crash on shutdown#1304
Open
markreidvfx wants to merge 1 commit into
Open
Conversation
The static s_callbacks vector was destroyed during static deallocation, after the Python interpreter had already been finalized. Any PyObject pointers still held by the vector would Py_DECREF into a dead interpreter, causing a use-after-free crash. Heap-allocate the callback list so its lifetime is managed explicitly, and call shutdown() before Py_Finalize() to release all Python references while the interpreter is still alive. Acquire the GIL in the PyObject deleter to make ref-count operations thread-safe. Signed-off-by: Mark Reid <markreid@netflixanimation.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Linked issues
N/A
Summarize your change.
Fix a use-after-free crash on shutdown in
ImGuiPythonBridge.The
s_callbacksvector was declared as a static variable holding Python object pointers. Static destructors in C++ run aftermain()returns with no guaranteed order. If the vector is destroyed afterPy_Finalize()has shut down the Python interpreter,Py_DECREFon the held objects crashes.This crash does not always happen — it depends on static destructor ordering (which is undefined and varies between compilers and link order) and whether any ImGui Python callbacks were registered during the session. If the vector is empty or happens to be destroyed before
Py_Finalize(), no crash occurs.Fix by heap-allocating the callback list and adding a
shutdown()call beforePy_Finalize()to release Python references while the interpreter is still alive.Describe the reason for the change.
During shutdown, C++ static destructors run after
main()returns. The statics_callbacksvector heldPyObjectpointers with custom deleters callingPy_DECREF. If destroyed afterPy_Finalize(), those deleters operate on a dead interpreter, causing a use-after-free crash.Describe what you have tested and on which operating system.
Tested on Rocky Linux 9.
Add a list of changes, and note any that might need special attention during the review.
ImGuiPythonBridge.cpp: Heap-allocate callback list; acquire GIL inPyObjectdeleter.ImGuiPythonBridge.h: Update callback storage type.PyInterface.cpp: CallImGuiPythonBridge::shutdown()beforePy_Finalize().DiagnosticsView.cpp: Update call to use newshutdown()API.If possible, provide screenshots.
N/A