Skip to content

sim: get MetaDrive bridge working on macOS (fixes #33207) - #38283

Closed
flowmar47 wants to merge 3 commits into
commaai:masterfrom
flowmar47:metadrive-macos
Closed

sim: get MetaDrive bridge working on macOS (fixes #33207)#38283
flowmar47 wants to merge 3 commits into
commaai:masterfrom
flowmar47:metadrive-macos

Conversation

@flowmar47

@flowmar47 flowmar47 commented Jul 4, 2026

Copy link
Copy Markdown

Fixes #33207tools/sim/tests/test_metadrive_bridge.py now passes on macOS (Apple Silicon), and the same branch was verified on Linux (arm64 Ubuntu 24.04 container, xvfb): the test passes there too.

Three root causes, three small fixes:

1. manager died silently at launch (system/manager/helpers.py)
unblock_stdout() calls os.forkpty() after manager's imports have already spawned threads. On macOS the forked child is killed before main() runs, and macOS ptys discard the child's pending output on exit — so launch_openpilot.sh exited 0 with no output and nothing running. Skip the pty relay on Darwin; Linux is unchanged.

2. free_space startup condition blocks sim on a full dev disk (system/hardware/hardwared.py)
freeSpacePercent > 2 exists so the device kills processes before the phone can't boot. On a dev machine under 2% free it silently blocks onroad in simulation (on any OS). selfdrived already exempts its own free-space check under SIMULATION; this mirrors that in hardwared's startup conditions.

3. sim camera frames stamped with a fake clock (tools/sim/lib/camerad.py)
Sim camerad set eof = frame_id * 0.05s, i.e. a clock starting at 0, while every other message uses logMonoTime = time.monotonic(). locationd's filter-rewind check compares cameraOdometry.timestampEof against IMU time, so every camera observation was rejected ("Observation cameraOdometry ignored due to failed timing check") and livePose/liveCalibration never became valid. This is platform-independent — it also affects current master on Linux, and may be related to why the CI sim job started timing out. Stamp frames with time.monotonic().

Results

  • macOS (M-series, Metal): 7/8 runs pass; the last 5 consecutive runs all passed (43–124s each). The one failure was a lateral out_of_lane while a parallel Linux build was saturating the CPU.
  • Linux (arm64 container + xvfb, journald/soundd blocked as they need systemd/audio): 2/2 runs pass.

Related: acados NaN on Apple Silicon

After these fixes the car engaged but only crept (~0.15 m/s): the long MPC failed every solve (SQP_RTI: QP solver returned error status 3, "Long mpc reset, solution_status: 4"). Root cause is in commaai/dependencies: BLASFEO is built with ARMV8A_ARM_CORTEX_A57 on Darwin, and those asm kernels return NaNs on M-series. Rebuilding only libblasfeo.dylib with TARGET=GENERIC makes 100/100 solves succeed and the sim car actually drive. I'll submit that separately to commaai/dependencies; the fixes here are correct and useful independently.

Test

pytest -s -n0 openpilot/tools/sim/tests/test_metadrive_bridge.py
# 1 passed, 1 skipped

Three fixes to make tools/sim/tests/test_metadrive_bridge.py pass on macOS:

- manager: skip unblock_stdout() on Darwin. forkpty() of the already
  multi-threaded manager process kills the child before main() runs, and
  macOS ptys discard the child's pending output on exit, so manager died
  silently with exit code 0.
- hardwared: exempt the free_space startup condition under SIMULATION,
  matching the existing SIMULATION exemption in selfdrived. The 2% killall
  rationale is device-specific and silently blocks onroad on a nearly-full
  dev machine.
- sim camerad: stamp frames with time.monotonic() (the same clock as
  logMonoTime) instead of frame_id * 0.05s. locationd's filter rewind
  check compares cameraOdometry's timestampEof against IMU logMonoTime,
  so the fabricated near-zero eof made it reject every camera observation
  (platform-independent bug; also affects Linux).

Fixes commaai#33207

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RVUzZmGEtSvH77hxaCSmsn
@github-actions

github-actions Bot commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

Process replay diff report

Replays driving segments through this PR and compares the behavior to master.
Please review any changes carefully to ensure they are expected.

✅ 0 changed, 66 passed, 0 errors

flowmar47 and others added 2 commits July 3, 2026 22:36
modeld's warp kernels are compiled against the device camerad's NV12
layout from get_nv12_info() (128-aligned stride, aligned plane heights,
VENUS-padded buffer size), but the sim allocated tightly-packed w*h*3/2
buffers. Two consequences:

- modeld maps a get_nv12_info()-sized tensor over the smaller buffer,
  and the UV plane read runs ~240KB past the mapping: SIGSEGV on the
  x86 CI runners (modeld exits -11 ~8s after start), survives on macOS
  only by allocation-granularity luck
- the warp samples Y with a 2048 stride over 1928-packed data and reads
  UV from the wrong offset, so the model has been driving on a sheared,
  wrong-chroma image (verified by pushing a test pattern through the
  shipped warp pkl: packed layout smears it, this layout keeps it intact)

Publish the buffers with create_buffers_with_sizes() using the same
layout; encoderd/ui already honor the stride and uv_offset metadata.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RVUzZmGEtSvH77hxaCSmsn
Fork PRs run on ubuntu-24.04 where build_stripped.sh needs ~70s;
the 1-minute step timeout was failing build release.

Co-authored-by: flowmar47 <flowmar47@users.noreply.github.com>
@github-actions

Copy link
Copy Markdown
Contributor

This PR has had no activity for 24 days. It will be automatically closed in 7 days if there is no activity.

@github-actions github-actions Bot added the stale label Jul 29, 2026
@github-actions

github-actions Bot commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

This PR has been automatically closed due to inactivity. Feel free to re-open once activity resumes.

1 similar comment
@github-actions

github-actions Bot commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

This PR has been automatically closed due to inactivity. Feel free to re-open once activity resumes.

@github-actions github-actions Bot closed this Aug 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Get MetaDrive simulator working on macOS

2 participants