From e37c2268a69edd823d2e67d90fe48c3bc7fba706 Mon Sep 17 00:00:00 2001 From: Sam Haves Date: Fri, 9 Jan 2026 09:49:24 -0800 Subject: [PATCH] Release gil when compiling spec. When compiling with threading, we will block the main thread anyways waiting for all threads to complete. PiperOrigin-RevId: 854230560 Change-Id: Ief689d381afaa3aacc5d712a8df0cb4b6762251c --- python/mujoco/specs_wrapper.cc | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/python/mujoco/specs_wrapper.cc b/python/mujoco/specs_wrapper.cc index 01f9a767..f1ba1188 100644 --- a/python/mujoco/specs_wrapper.cc +++ b/python/mujoco/specs_wrapper.cc @@ -89,7 +89,12 @@ MjSpec::~MjSpec() { mj_deleteSpec(ptr); } raw::MjModel* MjSpec::Compile() { if (assets.empty()) { - auto m = mj_compile(ptr, 0); + raw::MjModel* m; + { + // Release GIL before calling mj_compile which may spawn threads + py::gil_scoped_release no_gil; + m = mj_compile(ptr, 0); + } if (!m || mjs_isWarning(ptr)) { throw py::value_error(mjs_getError(ptr)); } @@ -112,7 +117,13 @@ raw::MjModel* MjSpec::Compile() { } } } - auto m = mj_compile(ptr, &vfs); + + raw::MjModel* m; + { + // Release GIL before calling mj_compile which may spawn threads + py::gil_scoped_release no_gil; + m = mj_compile(ptr, &vfs); + } mj_deleteVFS(&vfs); if (!m || mjs_isWarning(ptr)) { throw py::value_error(mjs_getError(ptr));