feat: enhance exit handling to prevent initialization and event processing during application exit

This commit is contained in:
Tunglies
2025-10-05 18:07:47 +08:00
parent dbcad24093
commit a1b3f267de
2 changed files with 52 additions and 1 deletions

View File

@@ -277,6 +277,12 @@ impl Handle {
} }
pub fn init(&self, app_handle: AppHandle) { pub fn init(&self, app_handle: AppHandle) {
// 如果正在退出,不要重新初始化
if self.is_exiting() {
log::debug!("Handle::init called while exiting, skipping initialization");
return;
}
{ {
let mut handle = self.app_handle.write(); let mut handle = self.app_handle.write();
*handle = Some(app_handle); *handle = Some(app_handle);
@@ -284,7 +290,12 @@ impl Handle {
let mut system_opt = self.notification_system.write(); let mut system_opt = self.notification_system.write();
if let Some(system) = system_opt.as_mut() { if let Some(system) = system_opt.as_mut() {
system.start(); // 只在未运行时启动
if !system.is_running {
system.start();
} else {
log::debug!("NotificationSystem already running, skipping start");
}
} }
} }

View File

@@ -354,6 +354,17 @@ pub fn run() {
/// Handle application ready/resumed events /// Handle application ready/resumed events
pub fn handle_ready_resumed(app_handle: &AppHandle) { pub fn handle_ready_resumed(app_handle: &AppHandle) {
// 双重检查:确保不在退出状态
if handle::Handle::global().is_exiting() {
logging!(
debug,
Type::System,
true,
"handle_ready_resumed: 应用正在退出,跳过处理"
);
return;
}
logging!(info, Type::System, true, "应用就绪或恢复"); logging!(info, Type::System, true, "应用就绪或恢复");
handle::Handle::global().init(app_handle.clone()); handle::Handle::global().init(app_handle.clone());
@@ -535,6 +546,16 @@ pub fn run() {
app.run(|app_handle, e| { app.run(|app_handle, e| {
match e { match e {
tauri::RunEvent::Ready | tauri::RunEvent::Resumed => { tauri::RunEvent::Ready | tauri::RunEvent::Resumed => {
// 如果正在退出,忽略 Ready/Resumed 事件
if core::handle::Handle::global().is_exiting() {
logging!(
debug,
Type::System,
true,
"忽略 Ready/Resumed 事件,应用正在退出"
);
return;
}
event_handlers::handle_ready_resumed(app_handle); event_handlers::handle_ready_resumed(app_handle);
} }
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
@@ -542,13 +563,32 @@ pub fn run() {
has_visible_windows, has_visible_windows,
.. ..
} => { } => {
// 如果正在退出,忽略 Reopen 事件
if core::handle::Handle::global().is_exiting() {
logging!(debug, Type::System, true, "忽略 Reopen 事件,应用正在退出");
return;
}
let app_handle = app_handle.clone(); let app_handle = app_handle.clone();
AsyncHandler::spawn(move || async move { AsyncHandler::spawn(move || async move {
event_handlers::handle_reopen(&app_handle, has_visible_windows).await; event_handlers::handle_reopen(&app_handle, has_visible_windows).await;
}); });
} }
tauri::RunEvent::ExitRequested { api, code, .. } => { tauri::RunEvent::ExitRequested { api, code, .. } => {
// 如果已经在退出流程中,不要阻止退出
if core::handle::Handle::global().is_exiting() {
logging!(
info,
Type::System,
true,
"应用正在退出,允许 ExitRequested (code: {:?})",
code
);
return;
}
// 只阻止外部的无退出码请求(如用户取消系统关机)
if code.is_none() { if code.is_none() {
logging!(debug, Type::System, true, "阻止外部退出请求");
api.prevent_exit(); api.prevent_exit();
} }
} }