Fix non-graceful exiting behaviour in launch_passive.

The viewer currently hard crashes when the user script loops on the passive viewer handle without paying attention to `is_running`. This is considered user error, however this change makes the user script hang on the loop rather than hard crashing.

Startblock:
   diffbase is submitted
PiperOrigin-RevId: 572336261
Change-Id: Id53126a158242b947584da1f946304ae8e792d4a
This commit is contained in:
Saran Tunyasuvunakool
2023-10-10 12:38:42 -07:00
committed by Copybara-Service
parent a1d0cbd654
commit a84499e904
2 changed files with 21 additions and 15 deletions
+5 -7
View File
@@ -97,9 +97,9 @@ class SimulateWrapper {
}
}
void WaitUntilDestroyed() {
void WaitUntilExit() {
// TODO: replace with atomic wait when we migrate to C++20
while (!destroyed_.load()) {
while (simulate_ && simulate_->exitrequest.load() != 2) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
@@ -206,8 +206,7 @@ PYBIND11_MODULE(_simulate, pymodule) {
key_callback),
scn, cam, opt, pert, is_passive);
}))
.def("destroy", &SimulateWrapper::Destroy,
py::call_guard<py::gil_scoped_release>())
.def("destroy", &SimulateWrapper::Destroy)
.def("load_message", CallIfNotNull(&mujoco::Simulate::LoadMessage),
py::call_guard<py::gil_scoped_release>())
.def("load", &SimulateWrapper::Load)
@@ -266,9 +265,8 @@ PYBIND11_MODULE(_simulate, pymodule) {
int value = 0;
sim->exitrequest.compare_exchange_strong(value, 1);
wrapper.WaitUntilDestroyed();
},
py::call_guard<py::gil_scoped_release>())
wrapper.WaitUntilExit();
})
.def_property_readonly("uiloadrequest",
CallIfNotNull(+[](mujoco::Simulate& sim) {