Skip to content

Commit 056069a

Browse files
authored
fix(runtime-wry): Fix mobile crash: RefCell already borrowed (#15502)
* fix(runtime-wry): RefCell borrow held across callbacks on mobile Resumed/Suspended * Add .changes * Rename mobile-resume-suspend-deadlock to mobile-resume-suspend-deadlock.md
1 parent 63a3415 commit 056069a

2 files changed

Lines changed: 20 additions & 7 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri-runtime-wry": "patch:bug"
3+
---
4+
5+
Fix a `RefCell` `BorrowMutError` panic on mobile: the `Resumed`/`Suspended` event branch held a `windows` borrow across the window-event handlers and the `RunEvent` callback, so any of them that created or closed a window (e.g. from a resume/suspend handler) panicked.

crates/tauri-runtime-wry/src/lib.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4412,10 +4412,20 @@ fn handle_event_loop<T: UserEvent>(
44124412
_ => unreachable!(),
44134413
};
44144414

4415-
let windows_ref = windows.0.borrow();
4416-
windows_ref.values().for_each(|window| {
4417-
let label = window.label.clone();
4418-
let window_event_listeners = window.window_event_listeners.clone();
4415+
// Collect the per-window listener handles and release the `windows`
4416+
// borrow before dispatching: handlers and the `RunEvent` callback may
4417+
// create or close windows (`windows.0.borrow_mut()`), which would panic
4418+
// the `RefCell` if we held the borrow across them. The desktop
4419+
// `WindowEvent` branches drop the borrow before dispatching for the same
4420+
// reason; this mobile `Resumed`/`Suspended` branch was the exception.
4421+
let targets = windows
4422+
.0
4423+
.borrow()
4424+
.values()
4425+
.map(|w| (w.label.clone(), w.window_event_listeners.clone()))
4426+
.collect::<Vec<_>>();
4427+
4428+
for (label, window_event_listeners) in targets {
44194429
let listeners = window_event_listeners.lock().unwrap();
44204430
for handler in listeners.values() {
44214431
handler(&event);
@@ -4425,9 +4435,7 @@ fn handle_event_loop<T: UserEvent>(
44254435
label,
44264436
event: event.clone(),
44274437
});
4428-
});
4429-
4430-
drop(windows_ref);
4438+
}
44314439
}
44324440
_ => (),
44334441
}

0 commit comments

Comments
 (0)