Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Desktop: make the close button quit outside macOS (#7929)
Daniel Hanc7415479c356
diff --git a/studio/frontend/src/features/hub/download-manager/download-manager-state.ts b/studio/frontend/src/features/hub/download-manager/download-manager-state.tsindex 7dbca7f08e1..66c4ab2db0a 100644--- a/studio/frontend/src/features/hub/download-manager/download-manager-state.ts+++ b/studio/frontend/src/features/hub/download-manager/download-manager-state.ts@@ -1,6 +1,7 @@// SPDX-License-Identifier: AGPL-3.0-only// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0+import { isTauri } from "@/lib/api-base";import { create } from "zustand";import { createJSONStorage, persist } from "zustand/middleware";import { INVENTORY_HINT_KIND } from "../inventory/constants";@@ -254,6 +255,41 @@ export const useDownloadManagerStore = create<DownloadManagerState>()(export const setState = useDownloadManagerStore.setState;export const getState = useDownloadManagerStore.getState;+/**+ * Downloads run in the backend, which the quit path reaps, but only this store knows they+ * are in flight. A Tauri quit never fires beforeunload, so mirror it to Rust to ask first.+ */+export function hasActiveDownloadJob(+ jobs: Record<string, ManagedDownload>,+): boolean {+ // External jobs count too, unlike in `partialize`: their STT sidecars are reached+ // through the backend, so a quit kills those transfers as well.+ return Object.values(jobs).some((job) => ACTIVE_STATES.has(job.state));+}++function publishDownloadsActive(active: boolean): void {+ if (!isTauri) return;+ void import("@tauri-apps/api/core")+ .then(({ invoke }) =>+ invoke("set_renderer_activity", { kind: "downloads", active }),+ )+ .catch(() => {});+}++// Transitions only: the poll loop patches progress several times a second.+let lastPublishedDownloadsActive: boolean | null = null;++function syncDownloadsActivity(state: DownloadManagerState): void {+ const active = hasActiveDownloadJob(state.jobs);+ if (active === lastPublishedDownloadsActive) return;+ lastPublishedDownloadsActive = active;+ publishDownloadsActive(active);+}++// Once for whatever the persisted state restored, then on every change.+syncDownloadsActivity(getState());+useDownloadManagerStore.subscribe(syncDownloadsActivity);+function withCompletedHintSignature(state: DownloadManagerState,): DownloadManagerState {diff --git a/studio/frontend/src/features/training/hooks/use-training-unload-guard.ts b/studio/frontend/src/features/training/hooks/use-training-unload-guard.tsindex 1e7dc4117a0..6d92f17b7fa 100644--- a/studio/frontend/src/features/training/hooks/use-training-unload-guard.ts+++ b/studio/frontend/src/features/training/hooks/use-training-unload-guard.ts@@ -7,7 +7,7 @@ import { useTrainingRuntimeStore } from "../stores/training-runtime-store";let currentHandler: ((e: BeforeUnloadEvent) => void) | null = null;-// Desktop quit never fires beforeunload; Rust asks from the tray instead.+// Desktop quit never fires beforeunload; Rust asks instead, from the tray and (outside macOS) the close button.function publishTrainingActive(active: boolean): void {if (!isTauri) return;void import("@tauri-apps/api/core")diff --git a/studio/frontend/src/hooks/use-tauri-update.ts b/studio/frontend/src/hooks/use-tauri-update.tsindex a0b42996bfd..229e5438bef 100644--- a/studio/frontend/src/hooks/use-tauri-update.ts+++ b/studio/frontend/src/hooks/use-tauri-update.ts@@ -69,6 +69,16 @@ const DEFAULT_UPDATE_POLICY: DesktopUpdatePolicy = {releaseTagPrefix: "desktop-v",};+// Desktop quit never fires beforeunload, and only the renderer sees the shell installer.+function publishShellUpdateActive(active: boolean): void {+ if (!isTauri) return;+ void import("@tauri-apps/api/core")+ .then(({ invoke }) =>+ invoke("set_renderer_activity", { kind: "shell_update", active }),+ )+ .catch(() => {});+}+const UPDATE_VERSION_RE = /^v?\d+\.\d+\.\d+(?:(?:[-+][0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)|(?:\.(?:post|dev|rc)\d*)|(?:(?:post|dev|rc|a|b)\d*))?$/;function normalizeUpdateVersion(version: string): string | null {@@ -348,23 +358,33 @@ export function useTauriUpdate(isExternalServer = false) {let downloaded = 0;let contentLength = 0;- await update.downloadAndInstall((event) => {- switch (event.event) {- case "Started":- contentLength = event.data.contentLength ?? 0;- break;- case "Progress":- downloaded += event.data.chunkLength;- if (contentLength > 0) {- setUpdateProgress(Math.round((downloaded / contentLength) * 100));- }- break;- case "Finished":- setUpdatePhase("shell_install");- setStatus("installing");- break;- }- });+ // `update::is_update_running` is already false here, and quitting mid-install+ // leaves a half-updated app.+ publishShellUpdateActive(true);+ try {+ await update.downloadAndInstall((event) => {+ switch (event.event) {+ case "Started":+ contentLength = event.data.contentLength ?? 0;+ break;+ case "Progress":+ downloaded += event.data.chunkLength;+ if (contentLength > 0) {+ setUpdateProgress(+ Math.round((downloaded / contentLength) * 100),+ );+ }+ break;+ case "Finished":+ setUpdatePhase("shell_install");+ setStatus("installing");+ break;+ }+ });+ } finally {+ // Success, failure and cancel alike: the installer is no longer running.+ publishShellUpdateActive(false);+ }const { relaunch } = await import("@tauri-apps/plugin-process");await relaunch();diff --git a/studio/frontend/tests/download-transport-persistence.test.ts b/studio/frontend/tests/download-transport-persistence.test.tsindex d2362b0ba68..afd1cf638a5 100644--- a/studio/frontend/tests/download-transport-persistence.test.ts+++ b/studio/frontend/tests/download-transport-persistence.test.ts@@ -59,7 +59,7 @@ store.set(}),);-const { getState, jobKeyOf, putJob } = await import(+const { getState, hasActiveDownloadJob, jobKeyOf, putJob } = await import("../src/features/hub/download-manager/download-manager-state.ts");@@ -145,3 +145,54 @@ test("the cancel marker is written with the persisted job", () => {assert.equal(persisted.state.jobs[key].transport, "http");assert.equal(persisted.state.jobs[key].cancelTransport, "xet");});++test("a running job is the activity the desktop quit path asks about", () => {+ // What set_renderer_activity mirrors into Rust, so the close button warns first.+ assert.equal(hasActiveDownloadJob(getState().jobs), true);+});++test("an external job counts too, since a quit kills its transfer as well", () => {+ assert.equal(hasActiveDownloadJob({}), false);+ assert.equal(+ hasActiveDownloadJob({+ external: {+ key: "model:org/external",+ kind: "model",+ repoId: "org/external",+ variant: null,+ state: "running",+ downloadedBytes: 0,+ completedBytes: 0,+ completeOnDisk: false,+ expectedBytes: 0,+ fraction: 0,+ bytesPerSec: 0,+ error: null,+ startedAt: 4,+ external: true,+ },+ }),+ true,+ );+ // A settled job is not activity, whoever owns it.+ assert.equal(+ hasActiveDownloadJob({+ done: {+ key: "model:org/done",+ kind: "model",+ repoId: "org/done",+ variant: null,+ state: "complete",+ downloadedBytes: 0,+ completedBytes: 0,+ completeOnDisk: true,+ expectedBytes: 0,+ fraction: 1,+ bytesPerSec: 0,+ error: null,+ startedAt: 4,+ },+ }),+ false,+ );+});diff --git a/studio/frontend/tests/helpers/kit.ts b/studio/frontend/tests/helpers/kit.tsindex 40d3b0e151e..abfe3af14ce 100644--- a/studio/frontend/tests/helpers/kit.ts+++ b/studio/frontend/tests/helpers/kit.ts@@ -39,7 +39,8 @@ export function installLocalStorageFake(): {},};Object.assign(globalThis, {- window: { localStorage: storage },+ // A location too: lib/api-base reads its protocol, pulled in transitively.+ window: { localStorage: storage, location: { protocol: "http:" } },localStorage: storage,});return { store, storage };diff --git a/studio/src-tauri/src/main.rs b/studio/src-tauri/src/main.rsindex 6a1026130ff..6537884bcce 100644--- a/studio/src-tauri/src/main.rs+++ b/studio/src-tauri/src/main.rs@@ -25,16 +25,16 @@ use simplelog::{CombinedLogger, Config, LevelFilter, SharedLogger, TermLogger, TerminalMode, WriteLogger,};use std::fs;+use std::sync::atomic::{AtomicBool, Ordering};use std::sync::Once;use tauri::menu::{MenuBuilder, MenuItemBuilder};use tauri::tray::{MouseButton, MouseButtonState, TrayIconBuilder, TrayIconEvent};use tauri::{Emitter, Manager};use tauri_plugin_window_state::{AppHandleExt, StateFlags};-/// Serializes the exit paths that must reap the backend: the tray "Quit" item,-/// the Unix termination signal listener, and `RunEvent::Exit`. Exactly one path-/// runs cleanup; the others block until it is done, so the process never exits-/// out from under a cleanup that is still killing the backend tree.+/// Serializes the exit paths that reap the backend: `request_quit` (tray "Quit" and,+/// outside macOS, the close button), the Unix signal listener, and `RunEvent::Exit`.+/// Exactly one runs cleanup; the others block, so the process never exits mid-reap.static TERMINATION_CLEANUP: Once = Once::new();#[tauri::command]@@ -108,7 +108,7 @@ fn set_training_active(state: tauri::State<'_, TrainingActivityState>, active: b}}-/// Ask before quitting mid-training (true to proceed). Tray Quit only, as below.+/// Ask before quitting mid-training (true to proceed). request_quit only, as below.fn confirm_quit_during_training(app: &tauri::AppHandle) -> bool {use tauri_plugin_dialog::{DialogExt, MessageDialogButtons, MessageDialogKind};@@ -132,8 +132,96 @@ fn confirm_quit_during_training(app: &tauri::AppHandle) -> bool {.blocking_show()}+/// Renderer-only activity, mirrored here for the same reason: Hub downloads, which the+/// backend runs but only the frontend tracks, and the Tauri shell self-update, which+/// `update::is_update_running` stops covering once `downloadAndInstall` takes over.+#[derive(Default)]+pub struct RendererActivity {+ pub downloads: bool,+ pub shell_update: bool,+}++pub type RendererActivityState = std::sync::Arc<std::sync::Mutex<RendererActivity>>;++fn new_renderer_activity_state() -> RendererActivityState {+ std::sync::Arc::new(std::sync::Mutex::new(RendererActivity::default()))+}++/// The command body without the `tauri::State` wrapper, so tests can drive it directly.+fn apply_renderer_activity(state: &RendererActivityState, kind: &str, active: bool) {+ if let Ok(mut activity) = state.lock() {+ match kind {+ "downloads" => activity.downloads = active,+ "shell_update" => activity.shell_update = active,+ // An unknown kind is a renderer/Rust mismatch, never a reason to flip a flag.+ _ => {}+ }+ }+}++#[tauri::command]+fn set_renderer_activity(state: tauri::State<'_, RendererActivityState>, kind: &str, active: bool) {+ apply_renderer_activity(state.inner(), kind, active);+}++/// Ask before quitting mid shell update (true to proceed). request_quit only, as below.+fn confirm_quit_during_shell_update(app: &tauri::AppHandle) -> bool {+ use tauri_plugin_dialog::{DialogExt, MessageDialogButtons, MessageDialogKind};++ let Some(state) = app.try_state::<RendererActivityState>() else {+ return true;+ };+ if !state+ .lock()+ .map(|activity| activity.shell_update)+ .unwrap_or(false)+ {+ return true;+ }+ app.dialog()+ .message(+ "The app update is still installing. Quitting now interrupts the \+ installer part-way and can leave the app half-updated.",+ )+ .kind(MessageDialogKind::Warning)+ .title("Update in progress")+ .buttons(MessageDialogButtons::OkCancelCustom(+ "Quit anyway".to_string(),+ "Keep updating".to_string(),+ ))+ .blocking_show()+}++/// Ask before quitting mid-download (true to proceed). request_quit only, as below.+fn confirm_quit_during_downloads(app: &tauri::AppHandle) -> bool {+ use tauri_plugin_dialog::{DialogExt, MessageDialogButtons, MessageDialogKind};++ let Some(state) = app.try_state::<RendererActivityState>() else {+ return true;+ };+ if !state+ .lock()+ .map(|activity| activity.downloads)+ .unwrap_or(false)+ {+ return true;+ }+ app.dialog()+ .message(+ "Downloads are still running. Quitting now stops the backend and \+ cancels the downloads in progress.",+ )+ .kind(MessageDialogKind::Warning)+ .title("Downloads in progress")+ .buttons(MessageDialogButtons::OkCancelCustom(+ "Quit anyway".to_string(),+ "Keep downloading".to_string(),+ ))+ .blocking_show()+}+/// Ask before quitting mid-install (true to proceed): `cleanup_child_processes` SIGTERMs the-/// installer, leaving a venv that looks healthy but cannot start. Tray Quit only, since+/// installer, leaving a venv that looks healthy but cannot start. request_quit only, since/// RunEvent::Exit must never block on a dialog nobody can answer.fn confirm_quit_during_install(app: &tauri::AppHandle) -> bool {use tauri_plugin_dialog::{DialogExt, MessageDialogButtons, MessageDialogKind};@@ -285,6 +373,59 @@ fn show_main_window(app: &tauri::AppHandle) {}}+/// One quit confirmation at a time: the dialogs are unparented, so repeat close+/// presses would stack another one.+static QUIT_IN_PROGRESS: AtomicBool = AtomicBool::new(false);++/// Clears the flag on cancel or panic, so a quit cannot leave the close button inert.+struct QuitGuard;++impl Drop for QuitGuard {+ fn drop(&mut self) {+ QUIT_IN_PROGRESS.store(false, Ordering::SeqCst);+ }+}++fn begin_quit() -> Option<QuitGuard> {+ (!QUIT_IN_PROGRESS.swap(true, Ordering::SeqCst)).then_some(QuitGuard)+}++/// Confirm, reap the backend, then exit. Off the caller's thread because the confirmations+/// block, and never exit first: that would orphan the backend tree.+fn request_quit(app: &tauri::AppHandle) {+ let Some(guard) = begin_quit() else {+ info!("Quit already awaiting confirmation, ignoring the repeat request");+ return;+ };+ let app = app.clone();+ // The guard moves into the closure, so a failed spawn drops it and the next request retries.+ let spawned = std::thread::Builder::new()+ .name("request-quit".to_string())+ .spawn(move || {+ let _guard = guard;+ if !confirm_quit_during_install(&app) {+ return;+ }+ if !confirm_quit_during_update(&app) {+ return;+ }+ if !confirm_quit_during_shell_update(&app) {+ return;+ }+ if !confirm_quit_during_training(&app) {+ return;+ }+ if !confirm_quit_during_downloads(&app) {+ return;+ }+ cleanup_child_processes(&app);+ app.exit(0);+ });+ if let Err(error) = spawned {+ warn!("Could not spawn the quit thread: {error}");+ }+}+fn setup_tray(app: &tauri::App) -> Result<(), Box<dyn std::error::Error>> {let open = MenuItemBuilder::with_id("open", "Open Unsloth").build(app)?;let toggle = MenuItemBuilder::with_id("toggle", "Start/Stop Server").build(app)?;@@ -302,26 +443,7 @@ fn setup_tray(app: &tauri::App) -> Result<(), Box<dyn std::error::Error>> {"toggle" => {let _ = app.emit("tray-toggle-server", ());}- "quit" => {- // Run cleanup off the menu callback, but only exit after the- // backend tree has been reaped. Exiting first can terminate this- // process while a detached cleanup thread is still waiting,- // leaving the backend orphaned.- let app_handle = app.clone();- std::thread::spawn(move || {- if !confirm_quit_during_install(&app_handle) {- return;- }- if !confirm_quit_during_update(&app_handle) {- return;- }- if !confirm_quit_during_training(&app_handle) {- return;- }- cleanup_child_processes(&app_handle);- app_handle.exit(0);- });- }+ "quit" => request_quit(app),_ => {}}).on_tray_icon_event(|tray, event| {@@ -369,12 +491,14 @@ fn main() {.manage(diagnostics::new_diagnostics_state()).manage(install::new_install_state()).manage(new_training_activity_state())+ .manage(new_renderer_activity_state()).manage(native_intents::new_native_intake_state()).manage(new_backend_state()).manage(process::new_shutdown_flag()).manage(update::new_update_state()).invoke_handler(tauri::generate_handler![set_training_active,+ set_renderer_activity,app_layout::has_initialized_app_window_layout,app_layout::mark_app_window_layout_initialized,app_layout::reset_app_window_layout_initialized,@@ -436,14 +560,16 @@ fn main() {.note_dropped_paths(paths);}if let tauri::WindowEvent::CloseRequested { api, .. } = event {- // Hide window instead of closing, because this is a tray app.- // Processes keep running so the backend stays available.- // Full cleanup happens via:- // - Tray "Quit" menu item (explicit user action)- // - The Unix termination signal listener (logout, kill, Ctrl-C)- // - RunEvent::Exit, for framework-driven exits- let _ = window.hide();+ // Never close directly: the only window, so closing exits before the reap.api.prevent_close();+ if cfg!(target_os = "macos") {+ // Closing a window leaves the app in the Dock; Reopen restores it.+ let _ = window.hide();+ } else {+ // Elsewhere the close button means quit, not hiding what the user+ // believes they just closed.+ request_quit(window.app_handle());+ }}}).build(tauri::generate_context!())@@ -464,3 +590,83 @@ fn main() {_ => {}});}++#[cfg(test)]+mod tests {+ use super::*;++ // One test, not three: `QUIT_IN_PROGRESS` is process-global and cargo tests run in parallel.+ #[test]+ fn quit_guard_admits_one_quit_and_always_re_arms() {+ assert!(!QUIT_IN_PROGRESS.load(Ordering::SeqCst));++ {+ let _first = begin_quit().expect("the first quit must be admitted");+ assert!(+ begin_quit().is_none(),+ "a repeat close must not stack a second confirmation"+ );+ }++ // Cancelling drops the guard, which re-arms the close button.+ {+ let _second = begin_quit().expect("cancelling must re-arm the close button");+ }++ let unwound = std::panic::catch_unwind(|| {+ let _guard = begin_quit().expect("admitted");+ panic!("quit thread unwound");+ });+ assert!(unwound.is_err());+ assert!(+ begin_quit().is_some(),+ "a panicking quit must release the guard"+ );+ }++ fn renderer_activity(state: &RendererActivityState) -> (bool, bool) {+ let activity = state.lock().expect("the activity mutex must not be poisoned");+ (activity.downloads, activity.shell_update)+ }++ #[test]+ fn renderer_activity_starts_clear_and_round_trips_each_kind() {+ let state = new_renderer_activity_state();+ assert_eq!(renderer_activity(&state), (false, false));++ apply_renderer_activity(&state, "downloads", true);+ assert_eq!(renderer_activity(&state), (true, false));+ apply_renderer_activity(&state, "downloads", false);+ assert_eq!(renderer_activity(&state), (false, false));++ apply_renderer_activity(&state, "shell_update", true);+ assert_eq!(renderer_activity(&state), (false, true));+ apply_renderer_activity(&state, "shell_update", false);+ assert_eq!(renderer_activity(&state), (false, false));+ }++ #[test]+ fn renderer_activity_kinds_are_independent() {+ let state = new_renderer_activity_state();++ apply_renderer_activity(&state, "downloads", true);+ apply_renderer_activity(&state, "shell_update", true);+ assert_eq!(renderer_activity(&state), (true, true));++ // Downloads finishing must not clear an update that is still installing.+ apply_renderer_activity(&state, "downloads", false);+ assert_eq!(renderer_activity(&state), (false, true));+ }++ #[test]+ fn renderer_activity_ignores_an_unknown_kind() {+ let state = new_renderer_activity_state();+ apply_renderer_activity(&state, "shell_update", true);++ // A renderer/Rust mismatch must never flip a flag it did not name.+ apply_renderer_activity(&state, "training", true);+ apply_renderer_activity(&state, "", false);+ apply_renderer_activity(&state, "Downloads", true);+ assert_eq!(renderer_activity(&state), (false, true));+ }+}