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

Add MainThreadToken to ensure file-dialogs only run on the main thread #8467

Merged
merged 11 commits into from
Dec 17, 2024

Conversation

emilk
Copy link
Member

@emilk emilk commented Dec 15, 2024

What

We have a foot-gun in our code: our file dialogs (via rfd) are only allowed to be run from the main thread (at least on Mac).
However, there is nothing stopping a user from accidentally calling these functions from a background thread, and if you test it on e.g. Linux, it may very well work.

So this PR introduces a new crate re_capabilities and a new type MainThreadToken. Any function that uses rfd should require the MainThreadToken as an argument. The MainThreadToken is only allowed to be created in fn main, and since it is neither Send nor Sync, this guarantees at compile-time that any functions that take a MainThreadToken can only be called from the main thread.

NOTE: I have no way to enforce that all uses of rfd also require MainThreadToken - we need to remember this ourselves, but I've made sure that all our current uses of rfd require a MainThreadToken.

@emilk emilk added 🧑‍💻 dev experience developer experience (excluding CI) exclude from changelog PRs with this won't show up in CHANGELOG.md labels Dec 15, 2024
Copy link

github-actions bot commented Dec 15, 2024

Web viewer built successfully. If applicable, you should also test it:

  • I have tested the web viewer
Result Commit Link Manifest
611462d https://rerun.io/viewer/pr/8467 +nightly +main

Note: This comment is updated whenever you push a commit.

Copy link
Member

@jprochazk jprochazk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we make the i_promise_i_am_on_the_main_thread function into something that actually enforces the invariant, and we don't have any way to bypass it, then I think this could work. But as it is, it's better than nothing, even if it depends on manual review 🤷

@emilk emilk added include in changelog and removed exclude from changelog PRs with this won't show up in CHANGELOG.md labels Dec 17, 2024
@emilk emilk merged commit db18508 into main Dec 17, 2024
31 checks passed
@emilk emilk deleted the emilk/re_capabilities branch December 17, 2024 10:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🧑‍💻 dev experience developer experience (excluding CI) include in changelog
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants