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

fix: AppHandle::restart() may not send RunEvent::Exit event #12313

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/restart-may-not-publish-exit-event.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri": patch:bug
---

`AppHandle::restart()` may not send `RunEvent::Exit` event before exiting the application.
2 changes: 2 additions & 0 deletions crates/tauri/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ impl<R: Runtime> AppHandle<R> {
if self.runtime_handle.request_exit(RESTART_EXIT_CODE).is_err() {
self.cleanup_before_exit();
}
let _impede = self.manager.wait_for_event_loop_exit();
crate::process::restart(&self.env());
}

Expand Down Expand Up @@ -1155,6 +1156,7 @@ impl<R: Runtime> App<R> {
let event = on_event_loop_event(&app_handle, RuntimeRunEvent::Exit, &manager);
callback(&app_handle, event);
app_handle.cleanup_before_exit();
manager.notify_event_loop_exit();
}
_ => {
let event = on_event_loop_event(&app_handle, event, &manager);
Expand Down
60 changes: 60 additions & 0 deletions crates/tauri/src/manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,13 @@ pub struct AppManager<R: Runtime> {
pub(crate) invoke_key: String,

pub(crate) channel_interceptor: Option<ChannelInterceptor<R>>,

// the value is set to true when the event loop is already exited
event_loop_exit_mutex: Mutex<bool>,
event_loop_exit_condvar: std::sync::Condvar,
// number of threads that request to NOT exit process
impede_exit_count: Mutex<usize>,
impede_exit_condvar: std::sync::Condvar,
}

impl<R: Runtime> fmt::Debug for AppManager<R> {
Expand Down Expand Up @@ -315,6 +322,10 @@ impl<R: Runtime> AppManager<R> {
pattern: Arc::new(context.pattern),
plugin_global_api_scripts: Arc::new(context.plugin_global_api_scripts),
resources_table: Arc::default(),
event_loop_exit_mutex: Mutex::new(false),
event_loop_exit_condvar: std::sync::Condvar::new(),
impede_exit_count: Mutex::new(0),
impede_exit_condvar: std::sync::Condvar::new(),
invoke_key,
channel_interceptor,
}
Expand Down Expand Up @@ -690,6 +701,55 @@ impl<R: Runtime> AppManager<R> {
pub(crate) fn invoke_key(&self) -> &str {
&self.invoke_key
}

pub(crate) fn notify_event_loop_exit(&self) {
let mut exit = self.event_loop_exit_mutex.lock().unwrap();
*exit = true;
self.event_loop_exit_condvar.notify_all();
drop(exit);

self.wait_impede();
}

pub(crate) fn wait_for_event_loop_exit(self: &Arc<Self>) -> ImpedeScope<R> {
let impede = self.impede_quit();
let mut exit = self.event_loop_exit_mutex.lock().unwrap();
while !*exit {
exit = self.event_loop_exit_condvar.wait(exit).unwrap();
}
impede
}

/// When the main loop exits, most runtime would exit the process, so we need to impede the exit
/// if we're going to restart the application.
fn impede_quit(self: &Arc<Self>) -> ImpedeScope<R> {
let mut pend_exit_threads = self.impede_exit_count.lock().unwrap();
*pend_exit_threads += 1;
ImpedeScope {
app_manager: self.clone(),
}
}

fn wait_impede(&self) {
let mut pend_exit_threads = self.impede_exit_count.lock().unwrap();
while *pend_exit_threads > 0 {
pend_exit_threads = self.impede_exit_condvar.wait(pend_exit_threads).unwrap();
}
}
}

pub struct ImpedeScope<R: Runtime> {
app_manager: Arc<AppManager<R>>,
}

impl<R: Runtime> Drop for ImpedeScope<R> {
fn drop(&mut self) {
let mut pend_exit_threads = self.app_manager.impede_exit_count.lock().unwrap();
*pend_exit_threads -= 1;
if *pend_exit_threads == 0 {
self.app_manager.impede_exit_condvar.notify_all();
}
}
}

#[cfg(desktop)]
Expand Down
Loading