Repository navigation
fix(render): defer glfw.init() on macOS until a GLFWContext is created - #570
Open
akushonkamen wants to merge 1 commit into
Open
akushonkamen wants to merge 1 commit into
akushonkamen wants to merge 1 commit into
Conversation
Importing 'dm_control._render.glfw_renderer' (reachable via 'import dm_control.viewer' or 'dm_control.render') called 'glfw.init()' at module import time. On macOS this disrupts the shared NSApplication state used by other GUI toolkits, most notably matplotlib's default 'MacOSX' backend (google-deepmind#377). Keep the import-time initialization on all other platforms so that the automatic backend selection in 'dm_control/_render/__init__.py' can still fall back to EGL/OSMesa when GLFW cannot initialize (e.g. headless Linux), and initialize inside 'GLFWContext.__init__' on macOS instead. 'glfwInit' is a no-op when GLFW is already initialized, so the extra call is safe.
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
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.
Fixes #377
Summary
import dm_control._render.glfw_renderer(reachable viaimport dm_control.viewerordm_control.render) currently callsglfw.init()at module import time. On macOS this disrupts the sharedNSApplicationstate used by other GUI toolkits — most notably matplotlib's defaultMacOSXbackend (#377). This PR defers GLFW initialization on macOS until aGLFWContextis actually created.Root cause
dm_control/_render/glfw_renderer.pyused to runglfw.init()at import time (guarded by atry/exceptthat convertsGLFWErrorintoImportError). Becausedm_control/_render/__init__.pyimports the GLFW backend as part of its automatic backend probing, anyimport dm_control.viewerorimport dm_control.rendertriggered GLFW initialization, even when no GLFW rendering was ever requested. Initializing GLFW attaches to the process-wideNSApplicationon macOS, which interferes with other GUI event loops sharing that state (#377).Fix
_init_glfw()(sameGLFWError→ImportErrorbehavior).dm_control/_render/__init__.pycan still fall back to EGL/OSMesa when GLFW cannot initialize (e.g. headless Linux) — behavior unchanged there._init_glfw()insideGLFWContext.__init__instead.glfwInitis a no-op when GLFW is already initialized, so the extra call at context creation is safe.Testing
Red → green (macOS arm64, darwin 24.6, Python 3.12, dm_control 1.0.49, mujoco 3.16.0):
Additional checks, all passing with the fix applied:
dm_control/_render/glfw_renderer_test.py:GLFWContextTest.test_init/test_make_current/test_freeing+GlfwInitAtImportTest— 5 tests OKdm_control/_render/base_test.py— 7 tests OKdm_control/_render/executor/render_executor_test.py— 30 tests OK (skipped=8)dm_control/viewertest files:application_test.py,runtime_test.py,user_input_test.py,util_test.pyOK;renderer_test.pyOK (skipped=40);views_test.pyOK (skipped=1)python -m py_compileon both changed files — OKphysics.render()on the main thread withMUJOCO_GL=glfw) — succeeds both before and after the fixglfwmodule countinginit()calls,import dm_control.viewercallsglfw.init()1× before the fix and 0× after;GLFWContext(...)still initializes exactly once.Honest limitations:
GLFWContextTest.test_repeatedly_create_and_destroy_contextaborts on this machine both before and after the fix:run_threadedinvokes GLFW from a worker thread, which trips macOS's main-thread-only rule. That is a pre-existing environment limitation, symmetric with respect to this change — not introduced by this patch.MacOSXwindow being killed) does not reproduce on the current dependency versions with either import ordering; this is disclosed in a follow-up comment on [viewer] Importing viewer module on macos immediately kills maplotlib windows #377. This patch is therefore a defensive removal of a global import-time side effect.AI disclosure
This change was prepared with the assistance of an AI coding agent (GLM, via the Contributor automation workflow); the GitHub account owner reviewed, tested, and is submitting the contribution under the project CLA.