Skip to content
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

Proposal: Enable main thread checking on Apple platforms #22

Closed
madsmtm opened this issue Sep 13, 2023 · 1 comment · Fixed by #24
Closed

Proposal: Enable main thread checking on Apple platforms #22

madsmtm opened this issue Sep 13, 2023 · 1 comment · Fixed by #24

Comments

@madsmtm
Copy link
Contributor

madsmtm commented Sep 13, 2023

On Apple platforms (macOS, iOS, tvOS and watchOS), basically all UI code is required to run on the main thread.

In Xcode 9.0 Apple introduced the "Main Thread Checker", which is a way for developers to enable extra assertions to ensure that their application conform to these requirements.

It is unclear whether doing work off the main thread is UB in the strictest sense or not (depends on how atomic AppKit/UIKit's internal structures are, which I've yet to find documentation about), but I think cargo-careful would be a good place to enable such a thing anyhow.

Xcode enables it by default, and Apple's own documentation reports a 1–2% CPU overhead, so I don't think there's anything to be worried about there. Also, if AppKit or UIKit is not loaded into the process, the checker simply does nothing.

Enabling it is as simple as inserting a dynamic library:

DYLD_INSERT_LIBRARIES=/Applications/Xcode.app/Contents/Developer/usr/lib/libMainThreadChecker.dylib ./target/debug/my_binary

(Of course an actual implementation in cargo-careful should use the current Xcode version via. xcode-select --print-path, and with proper error handling for people who don't have Xcode installed / have a too old version of Xcode to include that dylib).

For prosperity, I'll link this excellent blog post about the Main Thread Checker that outlines some undocumented options, if anyone's interested.

@RalfJung
Copy link
Owner

I've never used an Apple device in my life, so I can't really comment on this, but if someone can figure out a reliable way to implement this automatically (ideally something tested by our CI, maybe behind a flag), then sure, sounds useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants