From 5d537c7d551c02ed15c4f63439c9feda2090d841 Mon Sep 17 00:00:00 2001 From: zmdyy0318 Date: Mon, 9 Nov 2026 21:27:47 +0800 Subject: [PATCH] =?UTF-8?q?fix(logs):=20=E9=80=80=E5=87=BA=E6=B5=81?= =?UTF-8?q?=E7=A8=8B=E5=85=B3=E9=94=AE=E8=8A=82=E7=82=B9=E8=A1=A5=E5=85=85?= =?UTF-8?q?=E6=97=A5=E5=BF=97=EF=BC=8C=E4=BE=BF=E4=BA=8E=E5=AE=9A=E4=BD=8D?= =?UTF-8?q?=E9=80=80=E5=87=BA=E5=8D=A1=E9=A1=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src-tauri/src/commands/types.rs | 8 +++++++- src-tauri/src/lib.rs | 29 ++++++++++++++++++++++++++--- src/services/taskStopService.ts | 1 + 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src-tauri/src/commands/types.rs b/src-tauri/src/commands/types.rs index 9d7661be..ff32e2a1 100644 --- a/src-tauri/src/commands/types.rs +++ b/src-tauri/src/commands/types.rs @@ -801,7 +801,13 @@ pub struct MaaState { impl MaaState { /// 清理所有实例的 agent 子进程 pub fn cleanup_all_agent_children(&self) { - if let Ok(mut instances) = self.instances.lock() { + let lock_start = std::time::Instant::now(); + let instances = self.instances.lock(); + log::info!( + "[exit] Acquired instances lock for agent cleanup in {}ms", + lock_start.elapsed().as_millis() + ); + if let Ok(mut instances) = instances { for (id, instance) in instances.iter_mut() { for mut child in instance.agent_children.drain(..) { log::info!("Killing agent child process for instance: {}", id); diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 402baccd..25b0e01f 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -374,15 +374,31 @@ pub fn run() { } // 窗口关闭请求:检查是否最小化到托盘 tauri::WindowEvent::CloseRequested { api, .. } => { - if tray::handle_close_requested(window.app_handle()) { + let minimize_to_tray = tray::handle_close_requested(window.app_handle()); + log::info!( + "[exit] Window '{}' close requested, minimize_to_tray={}", + window.label(), + minimize_to_tray + ); + if minimize_to_tray { api.prevent_close(); } } // 窗口销毁时清理所有 agent 子进程 tauri::WindowEvent::Destroyed => { + log::info!( + "[exit] Window '{}' destroyed, cleaning up agent children", + window.label() + ); + let start = std::time::Instant::now(); if let Some(state) = window.try_state::>() { state.cleanup_all_agent_children(); } + log::info!( + "[exit] Window '{}' cleanup finished in {}ms", + window.label(), + start.elapsed().as_millis() + ); } _ => {} } @@ -396,13 +412,15 @@ pub fn run() { let final_exit_code = requested_exit_code.clone(); let on_event = move |_app_handle: &tauri::AppHandle, event: tauri::RunEvent| { - #[cfg(windows)] if let tauri::RunEvent::ExitRequested { code, .. } = &event { + log::info!("[exit] Exit requested, code={:?}", code); + #[cfg(windows)] requested_exit_code.set(*code); } // 退出前收尾遥测(结束 Session 与悬挂 Transaction 并 flush) if matches!(event, tauri::RunEvent::Exit) { + log::info!("[exit] Event loop exiting, running final cleanup"); commands::focus::cancel_all(); #[cfg(target_os = "macos")] commands::notification::shutdown_macos_notifications(); @@ -418,7 +436,12 @@ pub fn run() { // tauri-runtime-wry 2.9.3 的 ControlFlow::Exit 丢失请求的退出码。 // 先让事件循环完成退出回调和框架清理,再向操作系统报告该退出码。 let runtime_exit_code = app.run_return(on_event); - std::process::exit(final_exit_code.get().unwrap_or(runtime_exit_code)); + let exit_code = final_exit_code.get().unwrap_or(runtime_exit_code); + log::info!( + "[exit] Event loop returned, exiting process with code {}", + exit_code + ); + std::process::exit(exit_code); } #[cfg(not(windows))] app.run(on_event); diff --git a/src/services/taskStopService.ts b/src/services/taskStopService.ts index 0e5ea40c..0ebd9f87 100644 --- a/src/services/taskStopService.ts +++ b/src/services/taskStopService.ts @@ -95,6 +95,7 @@ export async function stopInstanceTasksAndExitApp(instanceId: string): Promise