google-deepmind / mujoco_mpc

Real-time behaviour synthesis with MuJoCo, using Predictive Control
https://github.com/deepmind/mujoco_mpc
Apache License 2.0
1.06k stars 157 forks source link

ModelDerivatives::Compute makes thread-unsafe call to mj_transitionFD #335

Open nimrod-gileadi opened 3 months ago

nimrod-gileadi commented 3 months ago

Here: https://github.com/google-deepmind/mujoco_mpc/blob/79db2bc0cc8e1ca4e96af0859718c265cdd22caf/mjpc/planners/model_derivatives.cc#L96

multiple threads are calling mjd_transitionFD on the same mjData instance. Unfortunately, mjd_transitionFD takes a non-const mjData*, and performs MuJoCo stack allocations. Those allocations are not thread safe and will get mixed up between threads, so the resulting computation is likely nonsense.

This was discovered by running tests with ThreadSanitizer.

nimrod-gileadi commented 3 months ago

Actually, every thread should have its own mjData, so I may have misdiagnosed the issue.