fix: release the gil when calling compile in spec.recompile

The pybind for recompile was not releasing the GIL when it called mj_recompile, this can caused deadlocks with the
asset loading threads. For example if a resource provider was trying to call Python.

To resolve this the GIL is release just for the mj_recompile.

Addresses: https://github.com/google-deepmind/mujoco/issues/3118
This commit is contained in:
robin.moss
2026-02-23 13:29:15 +00:00
parent 79d14035f9
commit 0c9083723c
2 changed files with 24 additions and 2 deletions
+12 -1
View File
@@ -77,7 +77,18 @@ py::tuple RecompileSpec(raw::MjSpec* spec, const MjModelWrapper& old_m,
raw::MjModel* m = static_cast<raw::MjModel*>(mju_malloc(sizeof(mjModel)));
m->buffer = nullptr;
raw::MjData* d = mj_copyData(nullptr, old_m.get(), old_d.get());
if (mj_recompile(spec, nullptr, m, d)) {
bool compile_failed = false;
{
// Release GIL before calling mj_recompile which may spawn threads
py::gil_scoped_release no_gil;
if (mj_recompile(spec, nullptr, m, d)) {
compile_failed = true;
}
}
if (compile_failed) {
throw py::value_error(mjs_getError(spec));
}
+12 -1
View File
@@ -400,7 +400,18 @@ py::tuple RecompileSpec(raw::MjSpec* spec, const MjModelWrapper& old_m,
raw::MjModel* m = static_cast<raw::MjModel*>(mju_malloc(sizeof(mjModel)));
m->buffer = nullptr;
raw::MjData* d = mj_copyData(nullptr, old_m.get(), old_d.get());
if (mj_recompile(spec, nullptr, m, d)) {
bool compile_failed = false;
{
// Release GIL before calling mj_recompile which may spawn threads
py::gil_scoped_release no_gil;
if (mj_recompile(spec, nullptr, m, d)) {
compile_failed = true;
}
}
if (compile_failed) {
throw py::value_error(mjs_getError(spec));
}