Skip to content

Studio: run one repair at a time, however many times Retry is pressed - #11249

Merged
danielhanchen merged 9 commits into
unslothai:mainfrom
LeoBorcherding:fix/desktop-repair-single-flight
Sep 18, 2026
Merged

danielhanchen merged 9 commits into
unslothai:mainfrom
LeoBorcherding:fix/desktop-repair-single-flight

Conversation

@LeoBorcherding

Copy link
Copy Markdown
Collaborator

Fixes the duplicate-repair half of #11247. The half-deleted venv that caused the crash in the first place is #11248.

What this fixes

start_managed_repair guarded itself with the install and update child handles. Those are empty exactly when a repair is busy doing something else: stopping the backend, sitting in block_external_conflict, or in the gap between the update child exiting and the installer spawning. Repeated calls in those windows all pass the guards, each runs its own studio update, and they race for the bundled installer. One wins, the rest fail with "Installation is already running." and that is what the user sees.

How I ran into it

Same session as the companion PR. After swapping an RTX 5060 Ti for an RX 6500 XT the update left the venv without unsloth_cli, the backend crashed on start, and the startup screen offered Retry. I pressed it a few times, as you do. Each press ran the preflight, each preflight said managed_stale with can_auto_repair, and each started a repair:

19:08:50 desktop_preflight completed disposition=ManagedStale port=None in 2017ms
19:08:50 start_managed_repair command called (force_installer=false)
19:08:51 desktop_preflight completed disposition=ManagedStale port=None in 2007ms
19:08:51 start_managed_repair command called (force_installer=false)
19:08:52 desktop_preflight completed disposition=ManagedStale port=None in 2016ms
19:08:52 start_managed_repair command called (force_installer=false)
19:08:52 desktop_preflight completed disposition=ManagedStale port=None in 2007ms
19:08:52 start_managed_repair command called (force_installer=false)
19:08:52 desktop_preflight completed disposition=ManagedStale port=None in 2011ms
19:08:52 start_managed_repair command called (force_installer=false)
19:08:52 [update] Starting backend update via "...\Scripts\unsloth.exe"
19:08:52 [ERROR] [update] Update exited with code 1
19:08:52 [WARN] Managed repair update failed, falling back to bundled installer: Update exited with code 1
19:08:53 [update] Starting backend update via "...\Scripts\unsloth.exe"
...
19:08:54 [install] Starting installation...
19:08:55 [ERROR] Managed repair installer failed: Installation is already running.
19:08:56 [ERROR] Managed repair installer failed: Installation is already running.
19:08:56 [ERROR] Managed repair installer failed: Installation is already running.
19:08:56 [ERROR] Managed repair installer failed: Installation is already running.

The support diagnostics ended up with five repair groups: one still running the installer, which finished successfully about four minutes later, and four with final_status=failed last_error=Installation is already running. The UI surfaced the failure, so the repair looked broken while it was quietly working.

What changes

studio/src-tauri/src/install.rs

  • InstallProcess.repair_in_flight, plus try_begin_repair, which takes it under the install lock and refuses if an installer child exists or a repair is already in flight. It hands back a RepairInFlight guard that clears the flag on drop, poisoned lock included, so every early return and ? releases it.
  • Two unit tests: a second repair is refused until the first releases, and a poisoned lock still releases.

studio/src-tauri/src/commands.rs

  • start_managed_repair takes the guard as its first act and holds it to the end of the command. start_backend_update and start_install refuse while a repair is in flight.

studio/frontend/src/hooks/use-tauri-backend.ts

  • checkInstallAndStart runs one preflight at a time. The persisted stop-intent check still runs ahead of the guard, so a stopped screen never arms a flag it does not release.
  • startRepair runs one repair at a time. The body moved unchanged into runRepair.

studio/frontend/tests/repair-single-flight.test.ts

  • Shape test for both guards, in the style of desktop-stop-intent.test.ts next to it.

Verification

  • cargo test in studio/src-tauri: 520 passed.
  • node --experimental-strip-types --test on repair-single-flight, forced-repair-retry, desktop-stop-intent, gpu-torch-mismatch, tray-server-status and linked-folders-lease-gate: all pass.

start_managed_repair guarded itself with the install and update child handles,
which are empty while it stops the backend and between the update child and
the installer. Five Retry clicks two seconds apart after a backend crash each
passed the guards, each ran their own `studio update`, and then raced for the
bundled installer: one won and the other four surfaced "Installation is
already running." over its progress.

The install state now carries a repair_in_flight flag taken at the top of the
command and released by a guard on every exit; update and install refuse while
it is set. The frontend stops the fan-out at its source: one preflight and one
repair in flight at a time.
@danielhanchen

Copy link
Copy Markdown
Member

Confirmed the guard in studio/src-tauri/src/commands.rs only checks the install child, so repeated Retry presses do get through to start_managed_repair. This is still marked draft, so let me know when you want it looked at properly.

danielhanchen and others added 3 commits September 18, 2026 16:55
unslothai#11226 landed the same single-flight while this was in review: UpdateProcess.repair_in_flight, a
RepairInFlight guard with the same RAII shape and the same poisoned-lock release, claimed at the
top of start_managed_repair. Rebasing onto main left two flags for one invariant, so this drops
the copy in InstallProcess and keeps main's.

What main does not have, and this keeps: start_install and start_backend_update refuse while a
repair is in flight. They now ask update::is_repair_running, which is the flag main already keeps,
rather than a second one. start_install gains the UpdateState parameter that needs; Tauri injects
state by type, so no caller changes.

start_managed_repair keeps its original "installation is in progress" refusal ahead of the claim,
which is where main has it.

The two unit tests that covered the removed flag are dropped: update.rs already tests the guard it
kept, including the poisoned-lock release, and duplicating that against a deleted type is not
coverage. The frontend single-flight and its test are untouched.
The guard spanned the whole of checkInstallAndStart, including the managed_ready arm's
await startManagedServer(), which parks in waitForManagedServerPort's 500 ms poll. The
server-start-timeout handler clears startingRef and shows Retry from inside that wait, so a click
arriving before the poll next wakes cleared the error and then returned on the flag, leaving the
screen in error with nothing running until the user clicked again.

Released as soon as desktop_preflight returns. The dispositions it leads to are re-entrant on
their own account: startManagedServer takes startingRef, startRepair takes repairInFlightRef.
What the flag exists to stop, five Retry clicks fanning out into five preflights and five repairs,
is unchanged.

The test now pins the release point between the probe and the switch, and that no long await runs
while the flag is held. It fails on the previous shape.
@danielhanchen

Copy link
Copy Markdown
Member

Reviewed this alongside #11247 and pushed three things to the branch, all explained in their commit messages.

Rebased onto current main. The branch was cut before #11226, which landed the same single-flight from the other side: UpdateProcess.repair_in_flight, a RepairInFlight guard with the same RAII shape and the same poisoned-lock release, claimed at the top of start_managed_repair. Merged as it stood, the two would have sat back to back for one invariant, so the copy in InstallProcess is gone and main's is kept. What main does not have, and this keeps, is start_install and start_backend_update refusing while a repair is in flight; they now read update::is_repair_running, the flag main already maintains. start_install gains an UpdateState parameter for that, which Tauri injects by type, so no caller changes.

One real bug in the frontend guard, found in review and fixed. preflightInFlightRef spanned the whole of checkInstallAndStart, so the managed_ready arm held it across await startManagedServer(), which parks in waitForManagedServerPort's 500 ms poll. server-start-timeout clears startingRef and offers Retry from inside that wait, so a click landing before the poll next woke cleared the error and then returned on the flag: error screen, nothing running, until the user clicked again. The flag is now released as soon as desktop_preflight returns. The arms it leads to are re-entrant on their own account (startingRef, repairInFlightRef), so the fan-out the guard exists to stop is unchanged. The test pins the release point and fails on the previous shape.

For the record on the issue's second half: the five-installer race itself can no longer happen on main, because #11226's claim refuses the duplicate calls. What was left, and what this PR fixes, is that those refusals still reached the user as "An update or repair is already running." over a repair that was working.

danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Sep 18, 2026
managed-environment-wait.test.ts evals checkInstallAndStart's body in a scope it builds by hand,
so the preflight guard this PR adds resolved to nothing and all six cases died with
ReferenceError: preflightInFlightRef is not defined. Landed on main after this branch was cut,
which is why the branch was green when it was opened.
Releasing early at the probe left the finally clearing the flag unconditionally, and by then it
may belong to someone else: A releases, A is still awaiting startManagedServer, Retry starts B
which arms the flag, A finishes and its finally clears B's, and a third Retry runs a preflight
concurrently with B. That is the fan-out the guard exists to stop.

releasePreflight() is a no-op once this call has handed the flag on, and both the early release
and the finally go through it. The test pins that shape and fails on an unconditional clear.
Same window as the preflight one, one flag along. runRepair awaits startManagedServer after the
native repair returns, and repairInFlightRef covered that too: server-start-timeout offers Retry
from inside the port poll, preflight comes back managed_stale, and the startRepair it leads to is
dropped on the flag after Retry has already cleared the error.

The flag is released the moment start_managed_repair returns, through the same ownership-aware
release as the preflight guard, so a call that has handed it on cannot clear a later one's. What
follows the native call is an ordinary server start, which startingRef already guards.
@danielhanchen

Copy link
Copy Markdown
Member

On before/after screenshots for this one: there is no pair to take.

The changed surface is the desktop startup screen, and the hook that drives it does nothing outside the desktop app. provider.tsx only calls useTauriBackend behind isTauri, and checkInstallAndStart returns on line 60 when isTauri is false. Our screenshot driver builds and drives the browser Studio through install.sh --local, where detectTauri() is false, so it would photograph a screen this code never reaches. Reproducing the real thing needs a packaged desktop build plus a managed environment broken in the specific way that makes preflight answer managed_stale.

So the evidence for this change stays where it can be checked: repair-single-flight.test.ts pins both release points and both ownership closures against the shipped source, managed-environment-wait.test.ts drives checkInstallAndStart itself and counts repairs, and cargo test repair covers the Rust refusals. Posting a browser pair here would show two identical screenshots of an unrelated screen.

danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Sep 18, 2026
@danielhanchen

Copy link
Copy Markdown
Member

Cross-platform run for this branch, off the org queue on a staging repo so all three ran in parallel.

runner node tests result
ubuntu-latest 9 pass 9, fail 0
macos-15 (Apple Silicon) 9 pass 9, fail 0
windows-latest 9 pass 9, fail 0

That is managed-environment-wait.test.ts plus repair-single-flight.test.ts on each OS, including the two the change does not target. cargo test repair covers the Rust refusals locally, 9 of 9.

Also pushed a comment-only pass over the hunks this PR adds: the ten line header on repair-single-flight.test.ts is now six, and five multi-line notes in the hook and the tests are collapsed to two lines each. Verified comment-only by AST before pushing, and the two test files still pass 9 of 9 afterwards.

@danielhanchen
danielhanchen marked this pull request as ready for review September 18, 2026 22:07
@danielhanchen
danielhanchen self-requested a review as a code owner September 18, 2026 22:07
@danielhanchen
danielhanchen merged commit 58339d5 into unslothai:main Sep 18, 2026
51 of 57 checks passed
shaojun pushed a commit to shaojun/unsloth that referenced this pull request Sep 29, 2026
…unslothai#11249)

* Desktop: run one managed repair at a time

start_managed_repair guarded itself with the install and update child handles,
which are empty while it stops the backend and between the update child and
the installer. Five Retry clicks two seconds apart after a backend crash each
passed the guards, each ran their own `studio update`, and then raced for the
bundled installer: one won and the other four surfaced "Installation is
already running." over its progress.

The install state now carries a repair_in_flight flag taken at the top of the
command and released by a guard on every exit; update and install refuse while
it is set. The frontend stops the fan-out at its source: one preflight and one
repair in flight at a time.

* Keep one repair flag, the one already on main

unslothai#11226 landed the same single-flight while this was in review: UpdateProcess.repair_in_flight, a
RepairInFlight guard with the same RAII shape and the same poisoned-lock release, claimed at the
top of start_managed_repair. Rebasing onto main left two flags for one invariant, so this drops
the copy in InstallProcess and keeps main's.

What main does not have, and this keeps: start_install and start_backend_update refuse while a
repair is in flight. They now ask update::is_repair_running, which is the flag main already keeps,
rather than a second one. start_install gains the UpdateState parameter that needs; Tauri injects
state by type, so no caller changes.

start_managed_repair keeps its original "installation is in progress" refusal ahead of the claim,
which is where main has it.

The two unit tests that covered the removed flag are dropped: update.rs already tests the guard it
kept, including the poisoned-lock release, and duplicating that against a deleted type is not
coverage. The frontend single-flight and its test are untouched.

* Hold the preflight flag over the probe, not over what the probe starts

The guard spanned the whole of checkInstallAndStart, including the managed_ready arm's
await startManagedServer(), which parks in waitForManagedServerPort's 500 ms poll. The
server-start-timeout handler clears startingRef and shows Retry from inside that wait, so a click
arriving before the poll next wakes cleared the error and then returned on the flag, leaving the
screen in error with nothing running until the user clicked again.

Released as soon as desktop_preflight returns. The dispositions it leads to are re-entrant on
their own account: startManagedServer takes startingRef, startRepair takes repairInFlightRef.
What the flag exists to stop, five Retry clicks fanning out into five preflights and five repairs,
is unchanged.

The test now pins the release point between the probe and the switch, and that no long await runs
while the flag is held. It fails on the previous shape.

* Tighten the comments this change added

* Give the managed-environment harness the flag the hook now reads

managed-environment-wait.test.ts evals checkInstallAndStart's body in a scope it builds by hand,
so the preflight guard this PR adds resolved to nothing and all six cases died with
ReferenceError: preflightInFlightRef is not defined. Landed on main after this branch was cut,
which is why the branch was green when it was opened.

* Release the preflight flag only while this call still owns it

Releasing early at the probe left the finally clearing the flag unconditionally, and by then it
may belong to someone else: A releases, A is still awaiting startManagedServer, Retry starts B
which arms the flag, A finishes and its finally clears B's, and a third Retry runs a preflight
concurrently with B. That is the fan-out the guard exists to stop.

releasePreflight() is a no-op once this call has handed the flag on, and both the early release
and the finally go through it. The test pins that shape and fails on an unconditional clear.

* Release the repair flag when the repair ends, not when the server starts

Same window as the preflight one, one flag along. runRepair awaits startManagedServer after the
native repair returns, and repairInFlightRef covered that too: server-start-timeout offers Retry
from inside the port poll, preflight comes back managed_stale, and the startRepair it leads to is
dropped on the flag after Retry has already cleared the error.

The flag is released the moment start_managed_repair returns, through the same ownership-aware
release as the preflight guard, so a call that has handed it on cannot clear a later one's. What
follows the native call is an ordinary server start, which startingRef already guards.

* Shorten the comments this change added

---------

Co-authored-by: Daniel Han <[email protected]>
Co-authored-by: Daniel Han <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants