Skip to content

fix: SG-43709: ImGuiPythonBridge crash on shutdown#1304

Open
markreidvfx wants to merge 1 commit into
AcademySoftwareFoundation:mainfrom
markreidvfx:nas_contrib_imgui_python_bridge_shutdown_v1
Open

fix: SG-43709: ImGuiPythonBridge crash on shutdown#1304
markreidvfx wants to merge 1 commit into
AcademySoftwareFoundation:mainfrom
markreidvfx:nas_contrib_imgui_python_bridge_shutdown_v1

Conversation

@markreidvfx

Copy link
Copy Markdown
Contributor

Linked issues

N/A

Summarize your change.

Fix a use-after-free crash on shutdown in ImGuiPythonBridge.

The s_callbacks vector was declared as a static variable holding Python object pointers. Static destructors in C++ run after main() returns with no guaranteed order. If the vector is destroyed after Py_Finalize() has shut down the Python interpreter, Py_DECREF on 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 before Py_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 static s_callbacks vector held PyObject pointers with custom deleters calling Py_DECREF. If destroyed after Py_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 in PyObject deleter.
  • ImGuiPythonBridge.h: Update callback storage type.
  • PyInterface.cpp: Call ImGuiPythonBridge::shutdown() before Py_Finalize().
  • DiagnosticsView.cpp: Update call to use new shutdown() API.

If possible, provide screenshots.

N/A

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>
@bernie-laberge bernie-laberge changed the title fix: ImGuiPythonBridge crash on shutdown fix: SG-43709: ImGuiPythonBridge crash on shutdown Jun 15, 2026
@bernie-laberge bernie-laberge added PR: Acknowledged New PR has been acknowledge by the TSC community Contribution from the Open RV Community labels Jun 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community Contribution from the Open RV Community PR: Acknowledged New PR has been acknowledge by the TSC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants