-
Notifications
You must be signed in to change notification settings - Fork 10.5k
sim: fix macOS MetaDrive support #36875
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for contributing to openpilot! In order for us to review your PR as quickly as possible, check the following:
- Convert your PR to a draft unless it's ready to review
- Read the contributing docs
- Before marking as "ready for review", ensure:
- the goal is clearly stated in the description
- all the tests are passing
- the change is something we merge
- include a route or your device' dongle ID if relevant
|
|
||
| if __name__ == "__main__": | ||
| unblock_stdout() | ||
| # Skip unblock_stdout on macOS - os.forkpty() causes crashes with raylib/AppKit |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why? i believe this used to work
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The issue is described here: #36785
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh i see the issue, but why does it happen now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MetaDrive's raylib initializes AppKit before manager.py runs. unblock_stdout() then calls os.forkpty(), which triggers macOS CoreFoundation's fork safety crash (can't fork after Cocoa init). Doesn't affect on-device since no GUI frameworks are loaded before manager starts.
The Objective-C runtime can't be both thread-safe and fork-safe. After AppKit/CoreFoundation initializes, it may have threads running or state that can't be safely duplicated via fork(). Apple's solution: crash with the message:
The process has forked and you cannot use this CoreFoundation functionality safely. You MUST exec().
The rule is: after fork(), you must immediately exec() a new program (which replaces the process image). But os.forkpty() in unblock_stdout() forks and continues running Python in the child — no exec().
Workarounds:
- spawn instead of fork (what multiprocessing uses by default on macOS now)
- OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES env var (must be set before process starts)
- Or just skip the fork entirely on macOS (what my fix does)
My fix is cleanest approach since unblock_stdout() isn't critical, from what I can tell. Even from looking back at the git history, it's not clear why it's there in the first place. But take for instance it's there to potentially speed up on-device logging pipelines; since the sim runs in terminal, blocking shouldn't be a concern.
Skip unblock_stdout() on macOS - os.forkpty() crashes after raylib/AppKit initialization due to CoreFoundation fork safety.
02d6df9 to
a97db68
Compare
Fix MetaDrive simulator support on macOS. Two issues prevented the simulator from running:
processNotRunning errors:
logcatdandproclogdare Android-only processes that trigger engagement-blocking errors on macOS. These are now blocked in the simulation launch script.CoreFoundation fork crash: After the Qt→raylib UI migration,
os.forkpty()inunblock_stdout()crashes on macOS because CoreFoundation/AppKit doesn't allow fork() after initialization. This is now skipped on macOS.Related bounty: Get MetaDrive simulator working on macOS
Verification
Tested on macOS 14 (M3):
BIG=1Known Issue
Camera view has a rendering artifact (diagonal distortion in corner) - this is an upstream panda3d/MetaDrive issue on macOS, not an openpilot bug.