Skip to content

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.

Studio: show the eject toast before the running-chats check (#10469)

Chaitanya Laxmane157f0973571
diff --git a/studio/frontend/src/features/chat/hooks/use-chat-model-runtime.ts b/studio/frontend/src/features/chat/hooks/use-chat-model-runtime.ts
index 12fd7930ecc..1165264ea26 100644
--- a/studio/frontend/src/features/chat/hooks/use-chat-model-runtime.ts
+++ b/studio/frontend/src/features/chat/hooks/use-chat-model-runtime.ts
@@ -3766,14 +3766,19 @@ export function useChatModelRuntime() {
confirmed?: StopRunningChatsDecision,
): Promise<boolean> => {
if (modelId && modelId !== params.checkpoint) {
+ const toastId = toast.loading("Unloading model");
try {
- if (!(await unloadKeptModel(modelId))) return false;
+ if (!(await unloadKeptModel(modelId))) {
+ toast.dismiss(toastId);
+ return false;
+ }
await refresh();
- toast.success("Model unloaded", { duration: 1200 });
+ toast.success("Model unloaded", { id: toastId, duration: 1200 });
return true;
} catch (err) {
toast.error(
err instanceof Error ? err.message : "Failed to unload model",
+ { id: toastId },
);
return false;
}
@@ -3784,6 +3789,10 @@ export function useChatModelRuntime() {
const bailIfLoading = (): boolean => {
const runtime = useChatRuntimeStore.getState();
if (!runtime.modelLoading && !runtime.loadingModelPick) return false;
+ if (chatModelLifecycleGate.currentPhase() === "unloading") {
+ toast.info("Wait for the model to finish unloading.");
+ return true;
+ }
toast.info("A model is loading", {
description: "Wait for it to finish or cancel it first.",
});
@@ -3805,6 +3814,10 @@ export function useChatModelRuntime() {
if (lifecycleLease === null) {
return false;
}
+ // Before the running-chats check, which open chat streams can queue in the browser (#10339).
+ const toastId = toast.loading("Unloading model", {
+ description: "Checking for running chats.",
+ });
// Ejecting tears down llama-server, so every chat stops. Same prompt, but it leaves no model
// loaded, so it must not be worded as a reload. With several loaded only this one's chats stop.
const scope =
@@ -3818,7 +3831,10 @@ export function useChatModelRuntime() {
"unload",
scope,
));
- if (!stopDecision.proceed) return false;
+ if (!stopDecision.proceed) {
+ toast.dismiss(toastId);
+ return false;
+ }
async function performUnload(): Promise<void> {
stopQueuedRuns(stopDecision, Boolean(scope));
@@ -3835,6 +3851,7 @@ export function useChatModelRuntime() {
const unloadPromise = performUnload();
toast.promise(unloadPromise, {
+ id: toastId,
loading: "Unloading model",
success: { message: "Model unloaded", duration: 1200 },
error: (err) =>
diff --git a/studio/frontend/src/features/chat/utils/model-lifecycle-gate.ts b/studio/frontend/src/features/chat/utils/model-lifecycle-gate.ts
index 8a14f1a8ba7..89580e0551d 100644
--- a/studio/frontend/src/features/chat/utils/model-lifecycle-gate.ts
+++ b/studio/frontend/src/features/chat/utils/model-lifecycle-gate.ts
@@ -21,6 +21,10 @@ export class ModelLifecycleGate {
return lease;
}
+ currentPhase(): ModelLifecyclePhase | null {
+ return this.phase;
+ }
+
canQueue(): boolean {
return this.activeLease === null || this.phase === "loading";
}
diff --git a/studio/frontend/tests/model-lifecycle-gate.test.ts b/studio/frontend/tests/model-lifecycle-gate.test.ts
index b95d4a47e07..e3cb4516979 100644
--- a/studio/frontend/tests/model-lifecycle-gate.test.ts
+++ b/studio/frontend/tests/model-lifecycle-gate.test.ts
@@ -25,7 +25,9 @@ test("queue acceptance follows the owning lifecycle phase", () => {
for (const phase of ["preparing", "loading", "unloading"] as const) {
const gate = new ModelLifecycleGate();
assert.equal(gate.canQueue(), true);
+ assert.equal(gate.currentPhase(), null);
const lease = gate.tryAcquire(phase)!;
+ assert.equal(gate.currentPhase(), phase);
assert.equal(gate.canQueue(), phase === "loading");
assert.equal(gate.tryAcquire("loading"), null);
assert.equal(gate.markLoading(lease + 1), false);
@@ -33,6 +35,7 @@ test("queue acceptance follows the owning lifecycle phase", () => {
assert.equal(gate.markLoading(lease), phase === "preparing");
assert.equal(gate.canQueue(), phase !== "unloading");
assert.equal(gate.release(lease), true);
+ assert.equal(gate.currentPhase(), null);
assert.equal(gate.canQueue(), true);
const next = gate.tryAcquire("preparing")!;
assert.equal(gate.markLoading(lease), false);
diff --git a/tests/studio/test_chat_eject_in_flight.py b/tests/studio/test_chat_eject_in_flight.py
new file mode 100644
index 00000000000..4e7b69c31ef
--- /dev/null
+++ b/tests/studio/test_chat_eject_in_flight.py
@@ -0,0 +1,96 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""Chat eject toasts before the running-chats check and names a second click as an unload (#10339)."""
+
+from __future__ import annotations
+
+import textwrap
+
+from _node_harness import WORKDIR, read, require_node, run_harness, slice_between, source_path
+
+HOOK = source_path("studio/frontend/src/features/chat/hooks/use-chat-model-runtime.ts")
+
+HARNESS = """
+// @ts-nocheck
+export const world: any = { toasts: [], unloads: 0 };
+
+let phase: string | null = null;
+const chatModelLifecycleGate = { currentPhase: () => phase };
+const params = { checkpoint: "org/model" };
+function setModelsError(_message: string | null): void {}
+function clearCheckpoint(): void {}
+async function refresh(): Promise<void> {}
+function isExternalModelId(_id: string): boolean { return false; }
+function stopQueuedRuns(_decision: unknown, _scoped: boolean): void {}
+function requestLocalPromptQueueStop(): void {}
+async function unloadModel(_payload: unknown): Promise<void> { world.unloads += 1; }
+
+// The running-chats check queued behind open streams: it answers only when released.
+let release: () => void = () => {};
+const checked = new Promise<void>((resolve) => { release = resolve; });
+export function releaseCheck(): void { release(); }
+async function confirmStopRunningChatsIfNeeded(): Promise<any> {
+ await checked;
+ return { proceed: true, forceCancelActive: false, promptQueueThreadIds: [], preStreamRunTokens: [] };
+}
+
+const toast = {
+ info: (title: string) => world.toasts.push(["info", title]),
+ loading: (title: string) => { world.toasts.push(["loading", title]); return "t1"; },
+ dismiss: () => world.toasts.push(["dismiss", ""]),
+ promise: (pending: Promise<unknown>, data: any) => world.toasts.push(["promise", data.id]),
+};
+
+const useChatRuntimeStore = {
+ getState: () => ({
+ get modelLoading() { return phase !== null; },
+ loadingModelPick: null,
+ loadedModels: [{ checkpoint: "org/model" }],
+ beginModelLoading(next: string) {
+ if (phase !== null) return null;
+ phase = next;
+ return 1;
+ },
+ endModelLoading() { phase = null; },
+ }),
+};
+
+export async function ejectModel(confirmed?: any): Promise<boolean> {
+__BODY__
+}
+"""
+
+
+def test_eject_toasts_before_the_check_and_refuses_a_second_click_as_unloading():
+ require_node((HOOK,))
+ body = slice_between(
+ read(HOOK),
+ " if (!params.checkpoint) {\n return false;\n }",
+ " }, [clearCheckpoint, params.checkpoint, refresh, setModelsError]);",
+ )
+ out = run_harness(
+ WORKDIR / "temp" / "chat_eject_in_flight",
+ HARNESS.replace("__BODY__", body),
+ textwrap.dedent(
+ """
+ // @ts-nocheck
+ import { ejectModel, releaseCheck, world } from "./harness.ts";
+ const first = ejectModel();
+ await new Promise((resolve) => setTimeout(resolve, 20));
+ const beforeCheck = [...world.toasts];
+ const second = await ejectModel();
+ releaseCheck();
+ console.log(JSON.stringify({ beforeCheck, second, first: await first,
+ toasts: world.toasts, unloads: world.unloads }));
+ """
+ ),
+ sources = (),
+ )
+ assert out["beforeCheck"] == [["loading", "Unloading model"]]
+ assert out["second"] is False
+ assert ["info", "Wait for the model to finish unloading."] in out["toasts"]
+ assert not any("A model is loading" in t for _, t in out["toasts"])
+ assert out["first"] is True
+ assert out["unloads"] == 1
+ assert ["promise", "t1"] in out["toasts"], "the unload must reuse the click's toast"