Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
fix(app): share the exit teardown and expose it as shutdown_for_update
thinhlpg7bdf0058e45e
diff --git a/src-tauri/src/core/system/mod.rs b/src-tauri/src/core/system/mod.rsindex 82b6da3c0..b3948e3c7 100644--- a/src-tauri/src/core/system/mod.rs+++ b/src-tauri/src/core/system/mod.rs@@ -1 +1,2 @@pub mod commands;+pub mod shutdown;diff --git a/src-tauri/src/core/system/shutdown.rs b/src-tauri/src/core/system/shutdown.rsnew file mode 100644index 000000000..a4c68b2e2--- /dev/null+++ b/src-tauri/src/core/system/shutdown.rs@@ -0,0 +1,72 @@+//! Teardown work that must run before the process goes away.+//!+//! `RunEvent::Exit` is the normal home for it, but the in-app updater leaves+//! the process without raising that event: `tauri-plugin-updater` calls+//! `std::process::exit(0)` itself after launching the Windows installer, and+//! `AppHandle::restart()` execs the new binary directly. Whatever only+//! `RunEvent::Exit` cleaned up (the engine, MCP servers, agent shells, pending+//! settings writes) was therefore left running or unwritten across an update.++use tauri::{AppHandle, Manager, Runtime};++use crate::core::mcp::helpers::background_cleanup_mcp_servers;+use crate::core::state::AppState;++/// How long MCP servers get to stop before we move on to the engine.+const MCP_CLEANUP_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10);++/// Stops everything the app started and flushes pending writes.+///+/// Safe to run more than once: each step is a no-op when there is nothing left+/// to stop, so the `RunEvent::Exit` handler can follow it.+pub async fn shutdown_cleanup<R: Runtime>(app: &AppHandle<R>) {+ // Drain debounced settings writes so the next version, and the jan CLI,+ // never read a stale settings.json.+ crate::core::app::settings_store::flush_settings();++ // No agent shell (or child it spawned) may outlive the app.+ tauri_plugin_agent_tools::tools::proc::kill_all();++ let state = app.state::<AppState>();+ match tokio::time::timeout(+ MCP_CLEANUP_TIMEOUT,+ background_cleanup_mcp_servers(app, &state),+ )+ .await+ {+ Ok(_) => log::info!("MCP cleanup completed successfully"),+ Err(_) => log::warn!(+ "MCP cleanup timed out after {} seconds",+ MCP_CLEANUP_TIMEOUT.as_secs()+ ),+ }++ if let Err(e) = tauri_plugin_llamacpp::cleanup_llama_processes(app.clone()).await {+ log::warn!("Failed to shut down the llama.cpp engine: {}", e);+ } else {+ log::info!("llama.cpp engine shut down successfully");+ }++ #[cfg(target_os = "macos")]+ {+ if let Err(e) = tauri_plugin_mlx::cleanup_mlx_processes(app.clone()).await {+ log::warn!("Failed to cleanup MLX processes: {}", e);+ } else {+ log::info!("MLX processes cleaned up successfully");+ }+ }++ log::info!("App cleanup completed");+}++/// Runs the exit-time teardown ahead of an in-app update.+///+/// The frontend calls this after the update has downloaded and verified, right+/// before the installer runs, so a failed download leaves the running app+/// untouched. On Windows `tauri-plugin-updater` exits the process as soon as+/// the installer is launched and offers no hook this crate can register, so+/// this has to happen before `install()`.+#[tauri::command]+pub async fn shutdown_for_update<R: Runtime>(app: AppHandle<R>) {+ shutdown_cleanup(&app).await;+}diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rsindex 49c5ee8a2..8a0be5a8b 100644--- a/src-tauri/src/lib.rs+++ b/src-tauri/src/lib.rs@@ -69,6 +69,7 @@ macro_rules! invoke_commands_with_extras {core::server::provider_secrets::get_secret,// System commandscore::system::commands::relaunch,+ core::system::shutdown::shutdown_for_update,core::system::commands::open_app_directory,core::system::commands::factory_reset,core::system::commands::take_pending_webdata_reset,@@ -499,10 +500,6 @@ pub fn run() {if let RunEvent::Exit = event {let app_handle = app.clone();- // Drain any debounced settings writes before the process dies so- // jan CLI never reads a stale settings.json.- core::app::settings_store::flush_settings();-#[cfg(not(any(target_os = "ios", target_os = "android")))]{if let Some(window) = app_handle.get_webview_window("main") {@@ -527,39 +524,9 @@ pub fn run() {// Run cleanup synchronously and WAIT for it to completetokio::task::block_in_place(|| {- tauri::async_runtime::block_on(async {- use crate::core::mcp::helpers::background_cleanup_mcp_servers;- use tauri_plugin_llamacpp::cleanup_llama_processes;-- let state = app_handle.state::<AppState>();-- // Increase timeout to 10 seconds and log if it times out- let cleanup_future = background_cleanup_mcp_servers(&app_handle, &state);- match tokio::time::timeout(tokio::time::Duration::from_secs(10), cleanup_future)- .await- {- Ok(_) => log::info!("MCP cleanup completed successfully"),- Err(_) => log::warn!("MCP cleanup timed out after 10 seconds"),- }-- if let Err(e) = cleanup_llama_processes(app_handle.clone()).await {- log::warn!("Failed to shut down the llama.cpp engine: {}", e);- } else {- log::info!("llama.cpp engine shut down successfully");- }-- #[cfg(target_os = "macos")]- {- use tauri_plugin_mlx::cleanup_mlx_processes;- if let Err(e) = cleanup_mlx_processes(app_handle.clone()).await {- log::warn!("Failed to cleanup MLX processes: {}", e);- } else {- log::info!("MLX processes cleaned up successfully");- }- }-- log::info!("App cleanup completed");- });+ tauri::async_runtime::block_on(core::system::shutdown::shutdown_cleanup(+ &app_handle,+ ))});}});