Skip to content

fix(local-models): share stop budget and retry failed quit stops on resume - #671

Open
oldwinter53 wants to merge 1 commit into
mainfrom
oldwinter53/fix-issue-664
Open

oldwinter53 wants to merge 1 commit into
mainfrom
oldwinter53/fix-issue-664

Conversation

@oldwinter53

Copy link
Copy Markdown

Summary

Fixes #664.

Root Cause

stopLocalmodels (packages/local-installation/src/localmodels-tool.ts) ran every localmodels command (library status, library cancel, library unload, foundations stop, stack down) with a hardcoded timeoutMs: 30_000. On a loaded machine (busy GPU or right after wake), individual steps exceed 30 s even though the detached quit helper already has a 150 s hard exit and resume/daemon lock waits allow 180 s. A timed-out library status skipped library unload, and a timed-out stack down left strata-poly-serve / strata-poly and Bifrost running in GPU memory with no retry until the next quit.

Changes

  • Shared stop budget with protected reserve (packages/local-installation/src/localmodels-tool.ts, apps/linger-installer/src/server/stop.ts, apps/linger-installer/src/main.ts):
    • Replaced the fixed 30 s per-command timeout in stopLocalmodels with a shared overall budget (QUIT_STOP_BUDGET_MS = 135_000 for --stop-local-models inside its 150 s hard exit; DEFAULT_STOP_BUDGET_MS = 120_000 for --stop), forwarded with remaining wall-clock time per home.
    • Reserved 45 s for stack down (plus 15 s for foundations stop on full stops), capped library status at 60 s, and shared pre-reserve time across multiple loading/loaded models so busy-machine commands finish without letting a wedged library step starve stack down.
    • Desktop Quit remains non-blocking because --stop-local-models runs detached.
  • Next-launch retry for failed or interrupted quit stops (packages/local-installation/src/local-models.ts, apps/linger-installer/src/server/stop.ts, apps/linger-installer/src/server/resume.ts):
    • Under withLocalModelsLock, stopDesktopLocalModels writes <home>/runtime/localmodels/stop-pending.json before stopping and removes it only when stopLocalmodels succeeds (a full uninstall --stop also clears it on success).
    • On the next launch, resumeSourceInstance calls retryPendingLocalModelsStop before warm restore or cold start. Normal launches see no marker and return immediately; launches racing an in-flight quit stop wait on the lock and skip retrying once the marker is cleared; failures are logged without blocking startup.
  • Docs & Tests:
    • Updated owning AGENTS.md files (packages/local-installation, apps/linger-installer, apps/desktop).
    • Added unit tests covering shared stop budgets, protected stack down/foundations stop reserves, multi-model sharing, stop-pending.json lifecycle, in-flight lock coordination, and pre-resume retry ordering.

Verification

  • pnpm --filter @linger/local-installation exec vitest run src/localmodels-tool.test.ts src/local-models.test.ts (all 122 tests passed)
  • pnpm --filter @linger/local-installation typecheck && pnpm --filter @linger/local-installation lint && pnpm --filter @linger/local-installation build (passed)
  • make check-installer (typecheck, eslint, and all 28 test suites passed)
  • pnpm --filter @linger/desktop typecheck and pnpm --filter @linger/linger-installer build (passed)
  • commitlint, pnpm env:inventory:check, changed-file Prettier/ESLint/English-only checks, and make check-docs (passed)

@linger-review linger-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approve with nits (2 P2, 1 P3)

Scope: base main, merge base 8b7fe39b. I reviewed 1 commit and 12 files. The coderbook, ponytail-review and karpathy-guidelines skills were not loaded, but I applied their checklists as the review recipe lists them. The review ran in a separate session at head a91ee4ee.

Findings

P2

1. The 15 s per-step floor lets library steps eat the stack down reserve. packages/local-installation/src/localmodels-tool.ts:1118

stepTimeoutMs returns max(1, min(remaining, max(minStepTimeoutMs, bounded))). The 15 s floor is capped only by remaining, not by remaining - reserveMs. Each wedged library step after the first can therefore cut into the protected tail.

Here is a concrete quit stop: 135 s budget, keepController, so the reserve is 45 s.

  • library status hangs for its 60 s cap. 75 s remain, 30 s of them above the reserve.
  • Three loaded models whose library unload hangs get max(15, 30/3), then max(15, 15/2), then max(15, 0). That is 15 s each.
  • stack down starts with 30 s, not the 45 s reserve.
  • With five hung unloads, or a hung cancel plus a hung poll library status (localmodels-tool.ts:1192-1197), the outer AbortSignal.timeout(QUIT_STOP_BUDGET_MS) fires first. Then options.signal.throwIfAborted() in step throws, and stack down never runs.

That is the busy-GPU case the PR is fixing. It also contradicts the PR's claim that a wedged library step can't starve stack down.

Fix: once a non-authoritative step is past the reserve, cap it at remaining - reserveMs. Skip the remaining library steps when that is ≤ 0, instead of flooring them at 15 s. Add a test with three or more hanging unloads that asserts the timeout stack down gets.

2. A persistently failing stop is retried, blocking, on every launch. apps/linger-installer/src/server/resume.ts:80, packages/local-installation/src/local-models.ts:826-858

retryPendingLocalModelsStop clears stop-pending.json only when stopLocalmodels succeeds. resumeSourceInstance awaits it before the warm probe and restore. If a step keeps failing or keeps hanging, the marker stays forever. Examples:

  • a model whose library unload always fails;
  • a present kit that fails verification (stopLocalmodels reports that as a stop failure);
  • a library status that keeps timing out.

Every later --resume then runs a full stop with the default 120 s budget before the app can come up. The quit-time stop re-creates the marker each time, so this never converges. The PR body says failures are logged "without blocking startup". They are not thrown, but startup still waits for the whole retry.

Fix: make the retry one-shot. Clear the marker, or record an attempt count in it, after one launch-time retry whatever its outcome. Alternatively, run it with a much smaller budget. Add a test for a retry that fails twice.

P3

3. The default timeoutMs parameter of step is dead code. packages/local-installation/src/localmodels-tool.ts:1127

All six step(...) calls now pass an explicit timeout, so timeoutMs = stepTimeoutMs(libraryReserveMs) never applies. Make the parameter required.

Smaller simplification in the same spot: Math.max(1, Math.max(minStepTimeoutMs, remaining)) in the authoritative branch could be one Math.max.

Verified as fine

  • Marker home matches the stop's home. The marker lives under the lock root (localModelsLockRoot, local-models.ts:137). Resume derives the home with path.resolve(instance.root, "../.."), the same derivation the existing restoreByok call uses (resume.ts:128).
  • Marker lifecycle in the quit stop. It is written and cleared inside withLocalModelsLock (stop.ts:146-150). A launch racing an in-flight quit stop waits on waitForLocalModelsLock (180 s, local-models.ts:124) and re-checks the marker under the lock (local-models.ts:833), so it does not stop twice.
  • Hard exit leaves the marker for retry. The quit helper's 150 s process.exit(1) (main.ts:384) leaves the marker behind, which is the intended retry trigger.
  • Retry abort handling. The retry rethrows only when the caller's signal aborted (local-models.ts:848). Otherwise it logs the failure and returns it. The resume signal is the existing long --resume timeout in main.ts.
  • Budget forwarding. The budget passes the remaining wall-clock time to each home (stop.ts:73-76, 124-127). QUIT_STOP_BUDGET_MS (135 s) stays under the 150 s hard exit.
  • Docs. All three owning AGENTS.md files are updated.
  • Language and history. Added lines and the commit message contain no CJK, and there are no merge commits.

Unverified: I did not confirm from the localmodels kit source whether library status and stack down succeed against a stack that is already down. That decides how often finding 2 triggers in practice.

Process notes

None.

CI

All checks that ran pass: check (installer), check (desktop), check (packages), check (web), repository-gate, CodeQL. The others were skipped as out of scope. I ran no tests locally; finding 1 comes from tracing stepTimeoutMs by hand.

Automated review by Claude Code


Automated review of a91ee4e by Claude Code.

@oldwinter53
oldwinter53 force-pushed the oldwinter53/fix-issue-664 branch from a91ee4e to 1207fd7 Compare November 25, 2026 04:52

@linger-review linger-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approve with nits (0 new findings; 3 previous P2/P3 findings still open)

Scope: follow-up on a91ee4e. Base main, merge base eafcfd41. Reviewed 1 commit (1207fd7b) and 12 files. Loaded the helper skills coderbook, ponytail-review and karpathy-guidelines. This review ran in a separate session.

The PR was force-pushed onto a newer main. git range-diff against a91ee4e shows only rebase context changes: an import line (isStoppedDownloadErrorCode) and the upstream unloadableLibraryModels call site. The PR's own logic did not change.

Previous findings

  • P2: the 15 s per-step floor can eat into the stack down reserve. Not resolved. packages/local-installation/src/localmodels-tool.ts:1117 still computes Math.max(minStepTimeoutMs, bounded) with remaining as the only cap, not remaining - reserveMs. So several slow library unload / cancel steps can still each take 15 s out of the 45 s stack down reserve.
  • P2: a stop that keeps failing makes every launch wait for a retry. Not resolved. apps/linger-installer/src/server/resume.ts:80 still runs retryPendingStop synchronously on every resume. retryPendingLocalModelsStop leaves the marker in place on failure (packages/local-installation/src/local-models.ts, the return { retried: true, failed } branch). A step that always fails therefore costs up to 120 s on every launch.
  • P3: the timeoutMs default on step has no caller left. Not resolved. packages/local-installation/src/localmodels-tool.ts:1127 still declares timeoutMs = stepTimeoutMs(libraryReserveMs), but every call site passes an explicit timeout.

Findings

No new findings. I re-checked the whole merge-base diff for P0/P1 problems and found none.

Verified as fine

  • Marker location and private writes. localModelsStopPendingFile lives under localModelsLockRoot(home) = <home>/runtime/localmodels (packages/local-installation/src/local-models.ts:138). It is written with privateDirectory + atomicPrivate. It is written only after the exists(runtime/localmodels) guard, so a home that never ran localmodels gets no folder created (apps/linger-installer/src/server/stop.ts, stopDesktopLocalModels).
  • Marker lifecycle under the lock. The marker is set and cleared inside withLocalModelsLock around stop(), so a failure or a hard exit at 150 s leaves it in place. stopDesktopInstallation clears it only after stopModels resolves.
  • No double stop when racing an in-flight quit. retryPendingLocalModelsStop checks the marker again after taking the lock through waitForLocalModelsLock (local-models.ts:749). That function rethrows only non-BUSY errors and gives up at LOCK_WAIT_MS. A quit stop that is still running therefore finishes first, and the retry sees the cleared marker.
  • Symlinked or foreign marker files are ignored. hasPendingLocalModelsStop uses lstat and accepts only a regular file owned by the current uid.
  • Budgets fit the process limits. The quit helper's signal is AbortSignal.timeout(QUIT_STOP_BUDGET_MS = 135_000), inside its 150 s process.exit (apps/linger-installer/src/main.ts:383-387). --stop uses the same 120 s for both signal and budget (main.ts:354-355). Resume runs under a 10-minute signal (main.ts:212), so a retry of up to 120 s fits.
  • stack down always gets the remaining time. In authoritative mode it gets max(minStep, remaining), and the outer signal still bounds it.
  • The retry targets the right home. It uses path.resolve(instance.root, "../.."), the same home restoreByok uses in the same function.
  • No CJK text in added lines or in the commit message. No new env vars, no contracts/generated edits, no cross-apps/* imports.

Process notes

  • The branch is 2 commits behind main, and main requires branches to be up to date, so a rebase is needed.
  • No merge commits and no fixup commits.

CI

8 checks pass, 7 are skipped, none fail. I ran no tests myself: the change touches no daemon concurrency code that CI skips.

Automated review by Claude Code


Automated review of 1207fd7 by Claude Code.

@oldwinter53
oldwinter53 force-pushed the oldwinter53/fix-issue-664 branch from 1207fd7 to 5035828 Compare November 26, 2026 19:23
@oldwinter53

Copy link
Copy Markdown
Author

Rebased onto latest main (6a511c15) and addressed all open review findings:

  1. Preserve stack down reserve under slow/hanging library steps (P2) — stepTimeoutMs in packages/local-installation/src/localmodels-tool.ts now caps non-authoritative steps at remaining - reserveMs instead of flooring them at 15 s past the reserve, skips remaining library/controller steps (recording a failure) once pre-reserve budget is exhausted, and stops cancel polling before encroaching on the reserve. Added unit tests covering multiple hanging unloads and cancel-poll exhaustion.
  2. Bound launch-time stop retries on persistent failure (P2) — stop-pending.json now records retry attempts (defaulting to 0 for legacy markers) and retryPendingLocalModelsStop retires the marker after one failed launch-time retry (unless caller-aborted). markLocalModelsStopPending preserves the retired attempt count across unbroken failed quits until a later stop succeeds and clears the marker, preventing repeated 120 s launch stalls while still recovering from transient quit failures. Added unit tests for transient recovery, repeated-launch bounding, repeated-quit failure streaks, reset on successful stop, and caller-abort preservation.
  3. Remove dead default parameters on step and simplify authoritative Math.max (P3) — Made step parameters required in stopLocalmodels and collapsed the nested Math.max.

@linger-review linger-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Request changes (1 P1, 1 P3)

Follow-up on 1207fd7. Scope: base main, merge base 6a511c15, 1 commit (50358288), 12 files. The PR was rebased since the previous review, so I compared the two versions with git range-diff and re-checked the whole merge-base diff. Helper skills: coderbook, ponytail-review and karpathy-guidelines were not loaded in this headless run; I applied the review checklist from the PR-review skill directly. The review ran in a separate session.

Previous findings

The previous review (1207fd7) had no findings. Changes since then: a failed launch-time retry now retires the marker (attempts counter), an aborted retry keeps the marker, and the docs were updated to match.

Findings

P1

  • apps/linger-installer/src/server/stop.ts:119 together with packages/local-installation/src/localmodels-tool.ts:1444 and :1583: during a full --stop (uninstall), the localmodels stage gets only the time left of the 120 s budget after the manager and services stages. stopLocalmodels then skips any step whose share would fall inside the reserve, rather than trying it.
    • Failure scenario: on a busy machine, the PR's own target case, the services stage stops five native services, and each can take up to the 12 s SIGINT grace (native-process.ts:586), plus the computer stop. If services take ≥ 60 s, remaining - libraryReserveMs (60 s) <= 0, so library status is skipped and nothing is unloaded. If services take ≥ 75 s, foundations stop gets stepTimeoutMs(45 s) = 0, so it is skipped and recorded as localmodels foundations stop: skipped to preserve stop budget. The uninstall then leaves the model controller running and reports a localmodels failure.
    • Before this PR, each of these commands ran with its own 30 s timeout under the 120 s signal, so a 1–2 s foundations stop still ran. This regression is limited to stopDesktopInstallation. The quit path calls only stopLocalmodels, so its budget starts fresh.
    • Fix: give the localmodels stage its own budget in stopDesktopInstallation instead of the remainder after earlier stages. Alternatively, when the budget above the reserve is spent, run non-final steps with the minimum step timeout (still bounded by signal) instead of skipping them. Add a stop.test.ts case where the services stage is slow.

P3

  • packages/local-installation/src/local-models.ts:849, :890, :957: the retry marker carries more than the design uses.
    • retryPendingLocalModelsStop's budgetMs option is never passed by its only caller (resume.ts:98).
    • attempts with MAX_PENDING_STOP_RETRY_ATTEMPTS = 1 is really a boolean "retired" flag.
    • The comment about a "legacy" marker refers to a file this PR introduces, so no legacy shape exists.
    • Simpler: a { retired: boolean } record (or two marker states), no budgetMs option, and no legacy wording.

Verified as fine

  • Quit marker lifecycle: the marker is written and cleared inside withLocalModelsLock (stop.ts:172-176), so a SIGKILL from the 150 s hard exit leaves it in place. A resume that races the in-flight quit waits on the same lock and re-reads the marker under it before stopping (local-models.ts:966-969).
  • Launch retry is bounded. It runs at most once per failure streak, and its timeout is AbortSignal.any([signal, timeout(120 s)]) (local-models.ts:975-978). A failure is logged and returned, not thrown. It rethrows only when the caller's own signal aborted (local-models.ts:987).
  • stack down always runs, with at least min(15 s, budget) and the full remainder (localmodels-tool.ts:1585-1590). The quit budget of 135 s sits inside the 150 s hard exit (main.ts:480-484).
  • No CJK in added lines or commit messages, and no merge commits.

Process notes

None.

CI

check (installer), check (web), repository-gate, CodeQL and Analyze passed. check (packages) and check (desktop) were still pending at review time. I ran no tests locally; the P1 follows from the budget arithmetic in the code cited above.

Automated review by Claude Code


Automated review of 5035828 by Claude Code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Desktop Quit: local model stop times out at 30 s per command on a busy machine and leaves the engine running

2 participants