@@ -8,8 +8,7 @@ use core::{
88} ;
99use reqwest:: Client ;
1010use std:: { collections:: HashMap , sync:: Arc } ;
11- use tauri:: { Emitter , Manager } ;
12-
11+ use tauri:: { Emitter , Manager , RunEvent } ;
1312use tokio:: sync:: Mutex ;
1413
1514#[ cfg_attr( mobile, tauri:: mobile_entry_point) ]
@@ -22,7 +21,8 @@ pub fn run() {
2221 // when defining deep link schemes at runtime, you must also check `argv` here
2322 } ) ) ;
2423 }
25- builder
24+
25+ let app = builder
2626 . plugin ( tauri_plugin_os:: init ( ) )
2727 . plugin ( tauri_plugin_deep_link:: init ( ) )
2828 . plugin ( tauri_plugin_dialog:: init ( ) )
@@ -143,12 +143,46 @@ pub fn run() {
143143 cleanup_processes ( state) . await ;
144144 } ) ;
145145 }
146+
146147 let client = Client :: new ( ) ;
147148 let url = "http://127.0.0.1:39291/processManager/destroy" ;
148149 let _ = client. delete ( url) . send ( ) ;
149150 }
150151 _ => { }
151152 } )
152- . run ( tauri:: generate_context!( ) )
153+ . build ( tauri:: generate_context!( ) )
153154 . expect ( "error while running tauri application" ) ;
155+
156+ // Handle app lifecycle events
157+ app. run ( |app, event| match event {
158+ RunEvent :: Exit => {
159+ // This is called when the app is actually exiting (e.g., macOS dock quit)
160+ // We can't prevent this, so run cleanup quickly
161+ let app_handle = app. clone ( ) ;
162+ tokio:: task:: block_in_place ( || {
163+ tauri:: async_runtime:: block_on ( async {
164+ let state = app_handle. state :: < AppState > ( ) ;
165+
166+ // Hide window immediately
167+ if let Some ( window) = app_handle. get_webview_window ( "main" ) {
168+ let _ = window. hide ( ) ;
169+ let _ = window. emit ( "kill-mcp-servers" , ( ) ) ;
170+ }
171+
172+ // Quick cleanup with shorter timeout
173+ cleanup_processes ( state) . await ;
174+
175+ // Stop HTTP server with shorter timeout
176+ let client = Client :: new ( ) ;
177+ let url = "http://127.0.0.1:39291/processManager/destroy" ;
178+ let _ = tokio:: time:: timeout (
179+ tokio:: time:: Duration :: from_secs ( 2 ) ,
180+ client. delete ( url) . send ( ) ,
181+ )
182+ . await ;
183+ } ) ;
184+ } ) ;
185+ }
186+ _ => { }
187+ } ) ;
154188}
0 commit comments