Skip to content

Commit

Permalink
Fix code that could lead to a possible deadlock.
Browse files Browse the repository at this point in the history
Drop implementations are not called until the end of a statement. The statement changed in this commit therefore took 4 read locks on a RwLock which can lead to problems if a write lock is requested between any of these read locks. The code looks like it would only hold one lock at a time but it does not drop any of the locks until after the arithmatic operations complete, which leads to this situation. See https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=996079046184329f3a9df1cd19c87da8 to see this in action. The fix is to just take one lock and share it between the three calls to num_presses, letting it drop at the end of the scope.
  • Loading branch information
DusterTheFirst committed Mar 19, 2022
1 parent 465c961 commit 9673b8f
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions egui/src/widgets/slider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,10 +455,13 @@ impl<'a> Slider<'a> {

fn value_ui(&mut self, ui: &mut Ui, position_range: RangeInclusive<f32>) -> Response {
// If `DragValue` is controlled from the keyboard and `step` is defined, set speed to `step`
let change = ui.input().num_presses(Key::ArrowUp) as i32
+ ui.input().num_presses(Key::ArrowRight) as i32
- ui.input().num_presses(Key::ArrowDown) as i32
- ui.input().num_presses(Key::ArrowLeft) as i32;
let change = {
let input = ui.input();

input.num_presses(Key::ArrowUp) as i32 + input.num_presses(Key::ArrowRight) as i32
- input.num_presses(Key::ArrowDown) as i32
- input.num_presses(Key::ArrowLeft) as i32
};
let speed = match self.step {
Some(step) if change != 0 => step,
_ => self.current_gradient(&position_range),
Expand Down

0 comments on commit 9673b8f

Please sign in to comment.