Skip to content

Commit 74ccaac

Browse files
Legend-MasterSir-Thom
authored andcommitted
fix(window-state): deadlock when trying to restore window states on initial load (tauri-apps#1787)
* Fix deadlock when trying to restore window the size on initial load * Typo
1 parent 748aa98 commit 74ccaac

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"window-state": patch
3+
---
4+
5+
Fix deadlock when trying to restore window states on initial load

plugins/window-state/src/lib.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ impl Default for WindowState {
105105
}
106106

107107
struct WindowStateCache(Arc<Mutex<HashMap<String, WindowState>>>);
108+
/// Used to prevent deadlocks from resize and position event listeners setting the cached state on restoring states
109+
struct RestoringWindowState(Mutex<()>);
108110
pub trait AppHandleExt {
109111
/// Saves all open windows state to disk
110112
fn save_window_state(&self, flags: StateFlags) -> Result<()>;
@@ -167,6 +169,8 @@ impl<R: Runtime> WindowExt for Window<R> {
167169
.map(|map| map(self.label()))
168170
.unwrap_or_else(|| self.label());
169171

172+
let restoring_window_state = self.state::<RestoringWindowState>();
173+
let _restoring_window_lock = restoring_window_state.0.lock().unwrap();
170174
let cache = self.state::<WindowStateCache>();
171175
let mut c = cache.0.lock().unwrap();
172176

@@ -396,6 +400,7 @@ impl Builder {
396400
Default::default()
397401
};
398402
app.manage(WindowStateCache(cache));
403+
app.manage(RestoringWindowState(Mutex::new(())));
399404
app.manage(PluginState {
400405
filename,
401406
map_label,
@@ -443,7 +448,13 @@ impl Builder {
443448
}
444449

445450
WindowEvent::Moved(position) if flags.contains(StateFlags::POSITION) => {
446-
if !window_clone.is_minimized().unwrap_or_default() {
451+
if window_clone
452+
.state::<RestoringWindowState>()
453+
.0
454+
.try_lock()
455+
.is_ok()
456+
&& !window_clone.is_minimized().unwrap_or_default()
457+
{
447458
let mut c = cache.lock().unwrap();
448459
if let Some(state) = c.get_mut(&label) {
449460
state.prev_x = state.x;
@@ -455,7 +466,12 @@ impl Builder {
455466
}
456467
}
457468
WindowEvent::Resized(size) if flags.contains(StateFlags::SIZE) => {
458-
if !window_clone.is_minimized().unwrap_or_default()
469+
if window_clone
470+
.state::<RestoringWindowState>()
471+
.0
472+
.try_lock()
473+
.is_ok()
474+
&& !window_clone.is_minimized().unwrap_or_default()
459475
&& !window_clone.is_maximized().unwrap_or_default()
460476
{
461477
let mut c = cache.lock().unwrap();

0 commit comments

Comments
 (0)