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:
@@ -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));
|
||||
}
|
||||
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user