diff --git a/src-tauri/src/core/handle.rs b/src-tauri/src/core/handle.rs index c0f4eddbd..6ac0b4f7f 100644 --- a/src-tauri/src/core/handle.rs +++ b/src-tauri/src/core/handle.rs @@ -277,6 +277,12 @@ impl Handle { } 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(); *handle = Some(app_handle); @@ -284,7 +290,12 @@ impl Handle { let mut system_opt = self.notification_system.write(); if let Some(system) = system_opt.as_mut() { - system.start(); + // 只在未运行时启动 + if !system.is_running { + system.start(); + } else { + log::debug!("NotificationSystem already running, skipping start"); + } } } diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 44683d27d..f4e316783 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -354,6 +354,17 @@ pub fn run() { /// Handle application ready/resumed events 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, "应用就绪或恢复"); handle::Handle::global().init(app_handle.clone()); @@ -535,6 +546,16 @@ pub fn run() { app.run(|app_handle, e| { match e { 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); } #[cfg(target_os = "macos")] @@ -542,13 +563,32 @@ pub fn run() { has_visible_windows, .. } => { + // 如果正在退出,忽略 Reopen 事件 + if core::handle::Handle::global().is_exiting() { + logging!(debug, Type::System, true, "忽略 Reopen 事件,应用正在退出"); + return; + } let app_handle = app_handle.clone(); AsyncHandler::spawn(move || async move { event_handlers::handle_reopen(&app_handle, has_visible_windows).await; }); } 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() { + logging!(debug, Type::System, true, "阻止外部退出请求"); api.prevent_exit(); } }