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
Studio: run one repair at a time, however many times Retry is pressed (#11249)
Leo Borcherding58339d59093e
diff --git a/studio/frontend/src/hooks/use-tauri-backend.ts b/studio/frontend/src/hooks/use-tauri-backend.tsindex 2efe6053e18..1ec8849932a 100644--- a/studio/frontend/src/hooks/use-tauri-backend.ts+++ b/studio/frontend/src/hooks/use-tauri-backend.ts@@ -178,6 +178,11 @@ export function useTauriBackend() {// Whether the repair in flight was asked to skip straight to the installer. Read back by// approveElevation, which restarts the repair after the system packages land.const forcedRepairRef = useRef(false);+ // One preflight and one repair at a time. Retry runs the preflight, a stale verdict starts a+ // repair, and five clicks two seconds apart used to fan out into five of each: the Rust side+ // saw them as five repairs racing for one installer.+ const preflightInFlightRef = useRef(false);+ const repairInFlightRef = useRef(false);const [tauriEventsReady, setTauriEventsReady] = useState(!isTauri);// Read through rather than mirrored into state: the app-closing listener is registered// inside the long event effect below, which cannot reach a setState from this render.@@ -303,10 +308,23 @@ export function useTauriBackend() {setBackendStatus("stopped");return;}+ if (preflightInFlightRef.current) return;+ preflightInFlightRef.current = true;+ // Released below and again in the finally; by then a later call may hold the flag, and+ // clearing it unowned would let a third preflight through.+ let ownsPreflight = true;+ const releasePreflight = () => {+ if (!ownsPreflight) return;+ ownsPreflight = false;+ preflightInFlightRef.current = false;+ };try {const { invoke } = await import("@tauri-apps/api/core");const preflight = await invoke<DesktopPreflightResult>("desktop_preflight");+ // The probe is what this guards; the arms below are re-entrant already. Held longer, it+ // swallows the Retry that server-start-timeout offers from inside the port poll.+ releasePreflight();switch (preflight.disposition) {case "attached_ready": {if (!preflight.port) {@@ -369,6 +387,8 @@ export function useTauriBackend() {}} catch (e) {setBackendError(String(e));+ } finally {+ releasePreflight();}}@@ -427,6 +447,27 @@ export function useTauriBackend() {// manual repair turns it on: an update reuses the environment it finds, so a venv whose// PyTorch was replaced by a CPU-only wheel comes back from one still CPU-only.async function startRepair(options?: { forceInstaller?: boolean }) {+ if (repairInFlightRef.current) return;+ repairInFlightRef.current = true;+ // Same ownership rule as the preflight flag, handed to runRepair so it can release as soon+ // as the native repair returns.+ let ownsRepair = true;+ const releaseRepair = () => {+ if (!ownsRepair) return;+ ownsRepair = false;+ repairInFlightRef.current = false;+ };+ try {+ await runRepair(options, releaseRepair);+ } finally {+ releaseRepair();+ }+ }++ async function runRepair(+ options?: { forceInstaller?: boolean },+ releaseRepair: () => void = () => {},+ ) {const forceInstaller = options?.forceInstaller ?? false;// Survives the elevation round trip: approveElevation resumes by calling this again.forcedRepairRef.current = forceInstaller;@@ -445,6 +486,9 @@ export function useTauriBackend() {const { invoke } = await import("@tauri-apps/api/core");try {await invoke("start_managed_repair", { forceInstaller });+ // The repair ends here; what follows is an ordinary start, and holding the flag across it+ // swallows the Retry that server-start-timeout offers.+ releaseRepair();setBackendStatus("starting");elevationResumeRef.current = null;diff --git a/studio/frontend/tests/managed-environment-wait.test.ts b/studio/frontend/tests/managed-environment-wait.test.tsindex 8efa5816ed2..c38bad2fd85 100644--- a/studio/frontend/tests/managed-environment-wait.test.ts+++ b/studio/frontend/tests/managed-environment-wait.test.ts@@ -81,6 +81,8 @@ function harness(preflight: Preflight, authFailure: string | null = null) {MANAGED_ENVIRONMENT_WAIT_POLLS: POLL_LIMIT,SERVER_STARTUP_MESSAGE: "Nearly done...",authFailureRef: { current: authFailure },+ // Fresh per harness: left true, the hook's single-flight guard returns before the probe.+ preflightInFlightRef: { current: false },environmentWaitRef: { current: null as unknown },environmentWaitPollsRef,statusRef: { current: "checking" },diff --git a/studio/frontend/tests/repair-single-flight.test.ts b/studio/frontend/tests/repair-single-flight.test.tsnew file mode 100644index 00000000000..de6ed653064--- /dev/null+++ b/studio/frontend/tests/repair-single-flight.test.ts@@ -0,0 +1,103 @@+// SPDX-License-Identifier: AGPL-3.0-only+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++// One preflight and one repair at a time. Five Retry clicks after a crash ran five preflights,+// each answering managed_stale with can_auto_repair, so five start_managed_repair calls raced+// for one installer and "Installation is already running." landed over the winner's progress.+//+// The hook cannot be rendered here, so the guards are read off the shipped source the way+// desktop-stop-intent.test.ts does beside it.++import assert from "node:assert/strict";+import test from "node:test";++import { readSrcAsync } from "./helpers/kit.ts";++const hook = await readSrcAsync("hooks/use-tauri-backend.ts");++function section(from: string, to: string): string {+ const start = hook.indexOf(from);+ const end = hook.indexOf(to, start);+ assert.ok(start >= 0 && end > start, `could not find ${from} .. ${to}`);+ return hook.slice(start, end);+}++test("a preflight already in flight is not run again", () => {+ const body = section(+ "async function checkInstallAndStart()",+ "async function startManagedServer()",+ );+ const guard = body.indexOf("if (preflightInFlightRef.current) return;");+ const arm = body.indexOf("preflightInFlightRef.current = true;");+ const preflight = body.indexOf('invoke<DesktopPreflightResult>("desktop_preflight")');+ assert.ok(guard > 0 && arm > guard && preflight > arm, "the guard must sit ahead of the preflight");+ // The persisted stop still wins: it must not arm a flag it never releases.+ assert.ok(+ body.indexOf("hasServerStopIntent()") < guard,+ "the stop-intent check runs before the in-flight guard",+ );+ assert.match(+ body,+ /finally \{\s*releasePreflight\(\);\s*\}/,+ "a failed preflight must release the flag or Retry is dead for the session",+ );+ // Held past the probe, the Retry that server-start-timeout offers from inside the port poll is+ // swallowed after clearing the error: error screen, nothing running.+ const release = body.indexOf("releasePreflight();");+ const dispatch = body.indexOf("switch (preflight.disposition)");+ assert.ok(+ release > preflight && release < dispatch,+ "the flag must be released once the probe returns, before any disposition is acted on",+ );+ assert.ok(+ body.indexOf("await startManagedServer()") > release,+ "no long await may run while the flag is held",+ );+ assert.ok(body.indexOf("await startRepair()") > release);+ // Released twice, and by then a later call may hold the flag: an unowned clear would let a+ // third preflight through.+ assert.match(+ body,+ /const releasePreflight = \(\) => \{\s*if \(!ownsPreflight\) return;\s*ownsPreflight = false;\s*preflightInFlightRef\.current = false;\s*\};/,+ "the release must be a no-op once this call has handed the flag on",+ );+ assert.ok(+ !/preflightInFlightRef\.current = false;[\s\S]*preflightInFlightRef\.current = false;/.test(body),+ "only releasePreflight may clear the flag",+ );+});++test("a repair already in flight is not started again", () => {+ const body = section("async function startRepair(", "async function runRepair(");+ assert.ok(+ body.indexOf("if (repairInFlightRef.current) return;") > 0,+ "startRepair must refuse re-entry",+ );+ assert.match(+ body,+ /const releaseRepair = \(\) => \{\s*if \(!ownsRepair\) return;\s*ownsRepair = false;\s*repairInFlightRef\.current = false;\s*\};/,+ "the release must be a no-op once this call has handed the flag on",+ );+ assert.match(+ body,+ /try \{\s*await runRepair\(options, releaseRepair\);\s*\} finally \{\s*releaseRepair\(\);\s*\}/,+ "the flag must be released on every exit, including an early return",+ );+ // The refused call returns without touching state: the running repair owns the screen.+ assert.ok(+ body.indexOf("if (repairInFlightRef.current) return;") <+ body.indexOf("repairInFlightRef.current = true;"),+ );+});++test("the repair body itself is unchanged in what it invokes", () => {+ const body = section("async function runRepair(", "async function startServer()");+ assert.match(body, /invoke\("start_managed_repair", \{ forceInstaller \}\)/);+ assert.match(body, /forcedRepairRef\.current = forceInstaller;/);+ // The repair ends with the native call; holding the flag across the start that follows+ // swallows the Retry server-start-timeout offers.+ const native = body.indexOf('invoke("start_managed_repair"');+ const release = body.indexOf("releaseRepair();");+ const start = body.indexOf("await startManagedServer();");+ assert.ok(native < release && release < start, "release once the native repair returns");+});diff --git a/studio/src-tauri/src/commands.rs b/studio/src-tauri/src/commands.rsindex cc2ff7f65c8..beff9a53c5d 100644--- a/studio/src-tauri/src/commands.rs+++ b/studio/src-tauri/src/commands.rs@@ -794,6 +794,7 @@ pub async fn start_install(app: AppHandle,state: tauri::State<'_, install::InstallState>,backend_state: tauri::State<'_, BackendState>,+ update_state: tauri::State<'_, update::UpdateState>,diagnostics: tauri::State<'_, DiagnosticsState>,) -> Result<(), String> {if has_owned_backend(&backend_state)? {@@ -802,6 +803,10 @@ pub async fn start_install(.to_string(),);}+ // A repair's installer phase runs through run_install_for_repair, so one started here races it.+ if update::is_repair_running(update_state.inner()) {+ return Err("Cannot install while a repair is in progress.".to_string());+ }block_external_conflict(&[]).await?;let state = state.inner().clone();@@ -878,6 +883,10 @@ pub async fn start_backend_update({return Err("Cannot update while installation is in progress.".to_string());}+ // A repair holds no child handle between its update and its installer: invisible to the above.+ if update::is_repair_running(update_state.inner()) {+ return Err("Cannot update while a repair is in progress.".to_string());+ }if update_state.lock()@@ -960,6 +969,9 @@ pub async fn start_managed_repair(return Err("Cannot repair while installation is in progress.".to_string());}+ // Taken before anything else and held to the end: the process handles below are empty+ // while the backend stops and between the update child and the installer, and every+ // duplicate call that slipped through there ran its own update and raced for the installer.let _repair = update::RepairInFlight::claim(update_state.inner())?;let diagnostics_state = diagnostics.inner().clone();