mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-01-29 00:35:38 +08:00
feat: enhance exit handling to prevent initialization and event processing during application exit
This commit is contained in:
@@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user