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
master from
Jan 25, 2023

Conversation

emilk
Copy link
Owner

@emilk 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

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