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

Avoid deadlocks by using lambdas for context lock #2625

Merged
merged 26 commits into from
Jan 25, 2023
Merged

Conversation

emilk
Copy link
Owner

@emilk emilk commented Jan 24, 2023

It used to be quite simple to cause dead-locks using egui. For instance:

if let Some(point_pos) = ctx.input().pointer.latest_pos() {
     // context lock is still being held here, so any call to the egui context would deadlock
}

With this PR you must now write this as:

if let Some(point_pos) = ctx.input(|i| i.pointer.latest_pos()) {
     // context lock is NOT held here, so you wont dead-lock
}

This is a bit more noisy, but far less error prone. The Context lock is held for the duration of the lambda, so keep them short and don't call other context-related things inside them.

Other examples:

  • ctx.input().key_pressed(Key::A) -> ctx.input(|i| i.key_pressed(Key::A))
  • ui.memory().toggle_popup(popup_id) -> ui.memory_mut(|mem| mem.toggle_popup(popup_id))

Most of the work in this PR comes from @MaximOsipenko (in #1969).

Closes #2064
Closes #2546 (maybe)

TODO

  • Check for any lingering mentions of the old style in markdown files or comments
  • Update CHANGELOG.md
  • Write docs for the functions
  • Write a migration guide
  • Remove ShortRwLockHelper
  • Write a better PR description, including rational, alternatives, and migration guide
  • Self-review the PR
  • Try it out in Rerun

@emilk emilk marked this pull request as ready for review January 24, 2023 20:11
@emilk emilk merged commit 8ce0e1c into master Jan 25, 2023
@emilk emilk deleted the emilk/locks_rework branch January 25, 2023 09:24
@emilk emilk mentioned this pull request Feb 4, 2023
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 this pull request may close these issues.

Instant crash. Something to do with my std::thread implementation Calling Context::load_texture with a write lock held to the InputState causes a deadlock
2 participants