Repository navigation
Studio: let a model load be cancelled while it is still loading - #8243
Conversation
Picking an image or video model starts a load that can pull tens of gigabytes, and until it finished there was no way out short of quitting Studio. The control that would have stopped it already existed. The backend's unload is also the load's cancel: it sets the running load's cancel event, bumps the load token so the worker can never commit, and drops the load marker. The Images and Video pages wire that to the model selector's eject, but only when a model is resident, and nothing is resident until the load commits, so the button was hidden for exactly the span it was needed. A replacement load keeps the previous pipeline resident and so did have an eject; a first load, which is the case the user hits, had nothing. Expose it two ways, both on Images and Video: - The persistent load toast now carries a Cancel action, the pattern the chat page already uses for its own loads. - A "Cancel load" button sits beside the model selector while a load is in flight. It survives the toast being dismissed and is a real button, so it is reachable by keyboard and a screen reader, unlike the trigger's eject hit area, which is an aria-hidden span by design. Cancelling leaves only cache behind: bytes already fetched stay in the HF cache so loading the same model again resumes rather than restarting, no half-built pipeline survives, and the page's existing idle-progress path clears the toast, the poll and the busy state, so the next pick loads normally. The wording says "load", never "download": the download manager keeps its own "Cancel download" for a staged pull, which is a different job.
|
@codex review |
1 similar comment
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 6aadac0373
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| resident model, so it is hidden for exactly the span a first load runs. A real button, not the | ||
| trigger's aria-hidden eject hit area, so it is reachable by keyboard and a screen reader. Says | ||
| "load", never "download": the download manager's own Cancel stops a staged pull, a different job. */} | ||
| {pageMode !== "train" && busy === "loading" && ( |
There was a problem hiding this comment.
Keep the cancel control visible in Train mode
If a user starts a model load in Create, closes the persistent toast, and then switches to Train, this condition removes the only remaining cancellation control even though busy is still "loading" and the backend continues downloading or materializing the model. The user must know to switch back to Create to recover the button, contradicting the intended fallback when the toast is dismissed; render the control whenever an image load is active, including beside the Train selector.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Leaving this. The toast is the primary control and it is persistent with duration Infinity; the button is the backstop for a user who dismissed it, and the path described needs both that dismissal and a tab switch during the load. The recovery is one click back to Create, with the model selector right there. Against that, a 'Cancel load' button rendered beside the Train base selector reads as cancelling the training run, which is a worse failure than the gap it closes: Train has its own long-running job with its own stop control, and two differently-scoped cancels in one header is exactly the ambiguity the download-manager wording note in this PR was avoiding.
| setBusy("loading"); | ||
| lastLoadSig.current = null; | ||
| loadToastId.current = toast(null, loadToastArgs(IDLE_PROGRESS, undefined, cancelLoadFromToast)); | ||
| void pollLoadProgress(); |
There was a problem hiding this comment.
Restore the reapply target after a failed cancellation
When the unload request fails but the background load remains alive, dropResidentState() has already cleared lastLoad and canReapply, while this recovery restores only the busy state, toast, and poll. If the load subsequently reaches ready, the success branch clears lastLoadRevert without restoring that target, so Video permanently hides “Reapply to loaded model” for the resulting resident model; Images has the same loss for GGUF and single-file loads. The revised failure branch is fresh evidence beyond the earlier tracking fix; restore the saved reapply state when resuming the live load.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Leaving this too. It needs the unload request itself to fail AND the load it could not stop to then succeed, and the cost is that Reapply is missing for that model until the next load. Reapply is a convenience that re-runs the same pick with new advanced options; the model is loaded, usable, and re-selectable from the picker. Restoring it correctly means capturing the in-flight target before dropResidentState clears it and threading it back through the failure path, which is new state carried for the whole load to serve one button on a doubly-degraded path. Not worth the surface.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 54ec52e2bd
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| // A deploy that was cancelled must not resurface: this ref outlives the load and is applied | ||
| // to whatever LoRA-capable model becomes resident next, which would silently mix a discarded | ||
| // adapter into an unrelated model's output. | ||
| pendingDeploy.current = null; |
There was a problem hiding this comment.
Restore the queued adapter when cancellation fails
When Cancel is pressed during handleDeployAdapter and the unload request fails before stopping the backend load, this unconditional clear discards the trained adapter even though handleCancelLoad restores the toast and progress poll so the base load can continue. If that load reaches ready, the LoRA discovery effect now finds pendingDeploy.current null, so the deployment silently finishes as a plain base-model load. Preserve the pending deployment across a failed unload and clear it only once cancellation is confirmed (or once the recovered poll observes a cancelled load).
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Leaving this. It needs a deploy, a cancel, and an unload request that fails, and the outcome is that the base model loads with the LoRA selection empty rather than with the adapter pre-filled. The adapter is in the catalog and one pick away in the LoRA row, and the user can see it is not applied. Weigh that against the alternative: pendingDeploy is applied to whatever LoRA-capable model becomes resident next, which is the bug the round-2 item flagged, and preserving it across a failed cancel re-opens exactly that window on the same degraded path. Clearing eagerly is the direction that fails safe, so the ref stays cleared in dropResidentState.
|
@codex review |
1 similar comment
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 6b53b12376
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| // half-built pipeline survives (the worker's commit is token-gated and unload clears the GPU state). | ||
| const handleCancelLoad = useCallback(async () => { | ||
| const wasLoading = busy === "loading"; | ||
| if (await handleUnload()) { |
There was a problem hiding this comment.
Preserve the resident model when cancelling a replacement
When model B is downloading as a replacement for resident model A, the backend deliberately keeps A resident until B enters assembly, but this Cancel action calls the full unload path. That unload cancels B and also tears down A, so clicking “Cancel load” unexpectedly leaves no model loaded instead of merely abandoning the incoming model; the Video path behaves the same way. Use a load-specific cancellation operation that fences the worker without clearing the existing pipeline, reserving handleUnload for the explicit eject control.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Correct about the behaviour, but the remedy is a backend change this PR cannot make. There is no load-only cancellation on the diffusion or video backends: unload() is the single lever that reaches a load in flight (it sets the running worker's cancel event and bumps _load_token), and it ends in _unload_locked(), which frees whatever is resident. That is exactly why this control is possible at all without new API. A POST that fences the worker without tearing down the pipeline means a new route, a new backend method, its own status semantics for 'cancelled a replacement, kept the old model', and tests on both loaders; landing that inside a frontend-only PR would ship an untested backend surface under a UI fix. Filing it as the follow-up it is, and leaving the destructive-but-honest behaviour here: the toast already says the load stopped, and the previous model is one pick away.
|
@codex review |
1 similar comment
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 56cec464c0
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| // once more -- that one cannot miss it. Unless a NEWER load has since taken the page: | ||
| // this unload names nothing, so firing it then would cancel that load instead of this | ||
| // one, and the newer start has already superseded this load's token on the backend. | ||
| if (startLoad === loadSeq.current) { |
There was a problem hiding this comment.
Keep external ejects fenced until pending starts settle
When the loaded-models card ejects a resident model while its replacement start request is still pending, the subscribeModelEjected handler clears busy, allowing another pick to increment loadSeq; that client-side ordering does not prove the newer request registered first on the backend. If the older request registers first, the newer one is rejected as “already in progress,” while this condition skips the compensating unload and the older handler returns without starting its poll, leaving a potentially multi-gigabyte load running with no toast or cancellation control. I checked the same sequence in both Images and Video; external ejects need to retain the pending-start fence rather than relying solely on loadSeq.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Correct, and already fixed on this branch in 93e9f78: the subscribeModelEjected handler now holds busy at 'unloading' until pendingStart settles instead of clearing it, so the window where a second pick could increment loadSeq is shut. Your follow-up on the selector path (the eject wired straight to handleUnload) was the half still open; that one is fixed now too.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: b7b8fd96fd
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| try { | ||
| setStatusIfNewest(++statusTicket.current, await unloadDiffusionModel()); | ||
| setQuant(null); | ||
| return true; |
There was a problem hiding this comment.
Fence selector ejects until pending starts settle
When resident model A is being replaced by B, the selector still routes its eject directly to handleUnload, so if the eject lands before B's start request registers, this path returns success and clears busy without awaiting pendingStart. The user can then pick C; if B registers first, C is rejected as already in progress, while B sees the newer loadSeq, skips its compensating unload, and returns without restarting its poll, leaving B loading with no toast or cancellation control. The external-eject listener now handles this exact pending-start window, but the selector wiring in both Images and Video bypasses that fence; keep this unload path busy until pendingStart settles as well.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Confirmed and fixed in 5a63a9d. onEject is wired straight to handleUnload and stays offered through a replacement load (the outgoing model is still resident), so that path was clearing busy before the start had registered. The fence now lives in handleUnload itself rather than in handleCancelLoad, so every caller of it gets it, and handleCancelLoad's duplicate wait is gone. Same change on both Images and Video; the source-shape tests were moved onto handleUnload.
| if (pending) { | ||
| setBusy("unloading"); | ||
| try { | ||
| await pending; |
There was a problem hiding this comment.
Bound the wait for a lost start response
If the model-start POST remains pending while the unload request succeeds—for example, the server accepted the start but its response is stuck in the transport—this await never completes because loadDiffusionModel/authFetch supplies neither an abort signal nor a response timeout. The page stays in "unloading" indefinitely with the toast dismissed and both the picker and cancel control unavailable, even though the backend cancellation has already returned. The Video page has the same unbounded wait; settle this fence from a bounded progress/status check or otherwise provide a recoverable timeout without allowing a still-live start to escape.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Rejecting. The premise is right -- authFetch passes no signal and no timeout -- but the trigger is a start POST to localhost that never settles, which is a transport pathology rather than anything a real session produces, and I could not reproduce it. The remedy is also the risk: a bounded wait that lets a still-live start escape reopens the exact race the fence closes (you note that yourself), and settling it from a progress/status probe is a new design rather than a fix. Leaving it.
onEject routes straight to handleUnload, and it is offered throughout a replacement load because the outgoing model is still resident. Fencing only handleCancelLoad left that path clearing busy before the start request had registered: the user picks another model, the backend refuses it because the older start won the race, and the older handler skips its compensating unload on the newer loadSeq and returns without restarting its poll, leaving a multi-gigabyte load running with no toast and no cancel control. Move the wait into handleUnload, so every caller of it gets the fence, and drop the copy handleCancelLoad was carrying.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 5a63a9dd68
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| void refreshStatus(); | ||
| return false; | ||
| } finally { | ||
| setBusy(null); |
There was a problem hiding this comment.
Preserve tracking when the compensating unload fails
When Cancel reaches the backend before the pending start registers and the compensating unload later fails, handleLoad calls restoreLoadTracking() and settles pendingStart, but this unconditional reset immediately overwrites the restored "loading" state. handleUnload then returns success, so the UI claims the model stopped and enables the picker even though the original load is still running; choosing another model dismisses the recovered toast/poll before that request is rejected as already in progress. The same sequence exists in Video, so the pending operation needs to propagate this failure or the finalizer must preserve the restored loading state.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Correct, and it was mine from the previous commit: moving the fence into handleUnload left its finally clearing busy unconditionally after the wait, so a restore that happened during that wait was wiped. Fixed in a593ca0 on both pages -- the finally now clears only an 'unloading' state, handleUnload returns false when the tracking came back (the load is still running, so the eject stopped nothing and the caller must not toast 'stopped loading'), and handleCancelLoad skips its own restore in that case so there is no duplicate toast or second poll loop.
Moving the pending-start fence into handleUnload left its finally clearing busy unconditionally after the wait. If handleLoad's compensating unload failed during that wait it had already called restoreLoadTracking and put the page back to 'loading' on purpose, and the clear wiped it: the toast's Cancel and the Cancel load button disappeared and the picker was re-enabled over a load that is still running, while handleUnload reported success so the page also claimed the model had stopped. Clear only an 'unloading' state, report the restore as a failed cancel, and stop handleCancelLoad restoring a second time (which would raise a duplicate toast and start a second poll loop).
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Another round soon, please! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
The complaint
Pick an image or video model and Studio starts pulling tens of gigabytes and building a pipeline. Until that finished there was no way out short of quitting Studio.
What was actually missing
Only the exposure. The backend's unload already is the load's cancel: it sets the running load's cancel event, bumps the load token so the worker can never commit, and drops the load marker.
The Images and Video pages wire that to the model selector's eject, but only when a model is resident:
Nothing is resident until the load commits, so the one control that would have stopped the load was hidden for exactly the span it was needed. A replacement load keeps the previous pipeline resident and so did have an eject; a first load, which is the case a new user hits, had nothing.
The change
Two exposures, on both pages:
aria-hiddenspan by design.The eject wiring is untouched: this adds a second control rather than widening that one, because "Eject" is the wrong word for stopping a load that never produced anything. The wording says "load", never "download" -- the download manager keeps its own Cancel download for a staged pull, which stops a different job.
What a cancelled load leaves behind
Only cache. Bytes already fetched stay in the HF cache, so loading the same model again resumes rather than restarting. Nothing stays resident and the page's existing idle-progress path clears the toast, the poll and the busy state, so the next pick loads normally.
One honest detail about timing: the cancel event preempts the download promptly, but the GPU materialisation step holds the backend lock, so a cancel pressed during it waits for the in-progress materialisation and then tears the result down. Either way the user gets out, nothing is left loaded and the VRAM comes back.
Checked in a running Studio
Images page, a 53 GB pipeline:
Video page: same, on a 34 GB pipeline. The button appears during the load and the press stops it.
Also: frontend suite (1395 tests) and typecheck green, and every new assertion mutation-tested (16 mutations, 0 survivors, suite green after restoring). Backend and hub test line sets are identical before and after the change.