sim: fix MetaDrive simulator on macOS (fixes #33207)#38052
Open
GitMonsters wants to merge 1 commit into
Open
Conversation
On macOS, multiprocessing defaults to 'fork' which conflicts with
Panda3D/MetaDrive's Cocoa/OpenGL initialization. Switch to the 'spawn'
context on Darwin so the child process starts clean.
Root causes fixed:
- manager.py: skip unblock_stdout() (forkpty) on Darwin when SIMULATION=1
to avoid unsafe fork before Cocoa initialization
- metadrive_process.py: lazy-import panda3d/metadrive inside the subprocess
so Cocoa/OpenGL is only initialized in the child, not the parent
- metadrive_world.py: use mp_ctx.{Array,Pipe,Event,Process} from the
spawn context so IPC primitives are compatible across process boundaries
- camerad.py: use time.monotonic() for frame timestamps instead of
synthetic frame_id * 0.05 to fix camera freshness checks
- metadrive_world.py: tighten dist_threshold to 0.05 on macOS test runs
to avoid false vehicle_not_moving failures
Fixes commaai#33207
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
Process replay diff reportReplays driving segments through this PR and compares the behavior to master. ✅ 0 changed, 66 passed, 0 errors |
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.
Summary
Fixes the MetaDrive simulator so it runs correctly on macOS, allowing
tools/sim/tests/test_metadrive_bridge.pyto pass.Closes #33207
Root causes
On macOS, Python's multiprocessing defaults to the fork start method. Panda3D and MetaDrive initialize Cocoa and OpenGL on import, which is unsafe to do in a forked child process after the parent has already set up GUI state. This causes the MetaDrive subprocess to hang or crash.
Changes
tools/sim/bridge/metadrive/metadrive_world.pymultiprocessing.get_context("spawn")on Darwin so the child process starts with a clean statemp_ctx.{Array, Pipe, Event, Process}so all IPC primitives come from the same spawn contextdist_thresholdto0.05on macOS test runs (the simulator steps produce smaller position deltas per tick on macOS, causing falsevehicle_not_movingfailures with the Linux-tuned threshold of1)tools/sim/bridge/metadrive/metadrive_process.pypanda3dandmetadrivemodules — these initialized Cocoa in the parent process before spawningapply_metadrive_patches()andmetadrive_process(), which only execute inside the child subprocessC3_POSITION/C3_HPRconstants inside the process function (sinceVec3is now imported lazily)system/manager/manager.pyunblock_stdout()(which callsforkpty) on Darwin whenSIMULATION=1forkptybefore the MetaDrive subprocess spawns causes the child to inherit an incompatible file-descriptor statetools/sim/lib/camerad.pytime.monotonic()for theeoftimestamp instead offrame_id * 0.05Testing
Tested locally on macOS (Apple Silicon):