fix: prevent segfault when using MuJoCo viewer Reload button#120
Open
SHABRAWii wants to merge 1 commit into
Open
fix: prevent segfault when using MuJoCo viewer Reload button#120SHABRAWii wants to merge 1 commit into
SHABRAWii wants to merge 1 commit into
Conversation
Two race conditions crash the simulator on Reload: 1. PhysicsLoop calls mj_deleteData/mj_deleteModel while the bridge threads are still reading from those pointers. Fix: add two std::atomic<bool> globals (g_bridge_pause_request / g_bridge_paused) as a pause/resume handshake. PhysicsLoop waits for the bridge to stop before freeing memory. UnitreeSdk2BridgeThread destroys and recreates the bridge on each reload instead of holding stale pointers forever. 2. RecurrentThread::~RecurrentThread() destroys mFunc (the run() lambda) before sending pthread_cancel, and never sets mQuit. If the thread wakes between those two steps it calls the destroyed std::function and hits std::terminate(). Fix: replace RecurrentThread with a std::thread owned by RobotBridge, controlled by a std::atomic<bool> stop_flag_. pre_destroy() sets the flag and joins the thread — guaranteed clean exit before any destructor runs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Two race conditions crash the simulator on Reload:
PhysicsLoop calls mj_deleteData/mj_deleteModel while the bridge threads are still reading from those pointers. Fix: add two std::atomic globals (g_bridge_pause_request / g_bridge_paused) as a pause/resume handshake. PhysicsLoop waits for the bridge to stop before freeing memory. UnitreeSdk2BridgeThread destroys and recreates the bridge on each reload instead of holding stale pointers forever.
RecurrentThread::~RecurrentThread() destroys mFunc (the run() lambda) before sending pthread_cancel, and never sets mQuit. If the thread wakes between those two steps it calls the destroyed std::function and hits std::terminate(). Fix: replace RecurrentThread with a std::thread owned by RobotBridge, controlled by a std::atomic stop_flag_. pre_destroy() sets the flag and joins the thread — guaranteed clean exit before any destructor runs.