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 desktop: leave the startup screen on server-port instead of at the next 500 ms poll (#15673)

Daniel Han31665e747f56
diff --git a/studio/frontend/src/hooks/managed-server-port.ts b/studio/frontend/src/hooks/managed-server-port.ts
new file mode 100644
index 00000000000..e07c7f3b25b
--- /dev/null
+++ b/studio/frontend/src/hooks/managed-server-port.ts
@@ -0,0 +1,60 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+export const MANAGED_STARTUP_POLL_MS = 500;
+
+export type ManagedStartupResult =
+ | { status: "ready"; port: number }
+ | { status: "aborted" };
+
+/** A poll interval that `notify` can cut short, so a server-port event is acted on at once. */
+export interface PortWakeSignal {
+ notify: () => void;
+ wait: (ms: number) => Promise<void>;
+}
+
+export function createPortWakeSignal(): PortWakeSignal {
+ let wake: (() => void) | null = null;
+ return {
+ notify() {
+ const current = wake;
+ wake = null;
+ current?.();
+ },
+ wait(ms: number) {
+ return new Promise<void>((resolve) => {
+ const done = () => {
+ clearTimeout(timer);
+ if (wake === done) wake = null;
+ resolve();
+ };
+ const timer = setTimeout(done, ms);
+ wake = done;
+ });
+ },
+ };
+}
+
+function sleep(ms: number): Promise<void> {
+ return new Promise((resolve) => setTimeout(resolve, ms));
+}
+
+export async function waitForManagedServerPort(
+ getPort: () => number | null,
+ shouldContinue: () => boolean,
+ wait: (ms: number) => Promise<void> = sleep,
+): Promise<ManagedStartupResult> {
+ while (true) {
+ if (!shouldContinue()) {
+ return { status: "aborted" };
+ }
+
+ const port = getPort();
+ if (port === null) {
+ await wait(MANAGED_STARTUP_POLL_MS);
+ continue;
+ }
+
+ return { status: "ready", port };
+ }
+}
diff --git a/studio/frontend/src/hooks/use-tauri-backend.ts b/studio/frontend/src/hooks/use-tauri-backend.ts
index 284d0f3af1b..b2d85612d84 100644
--- a/studio/frontend/src/hooks/use-tauri-backend.ts
+++ b/studio/frontend/src/hooks/use-tauri-backend.ts
@@ -50,6 +50,10 @@ import {
hasServerStopIntent,
markServerStopIntent,
} from "./server-stop-intent";
+import {
+ createPortWakeSignal,
+ waitForManagedServerPort,
+} from "./managed-server-port";
export type BackendStatus =
| "checking"
@@ -88,19 +92,11 @@ interface DesktopPreflightResult {
managed_bin: string | null;
}
-const MANAGED_STARTUP_POLL_MS = 500;
const MANAGED_ENVIRONMENT_POLL_MS = 5_000;
// Five minutes; bounds only a gate held outside this app, e.g. a terminal update at a prompt.
const MANAGED_ENVIRONMENT_WAIT_POLLS = 60;
type TauriInvoke = typeof import("@tauri-apps/api/core").invoke;
-type ManagedStartupResult =
- | { status: "ready"; port: number }
- | { status: "aborted" };
-
-function wait(ms: number) {
- return new Promise((resolve) => setTimeout(resolve, ms));
-}
function externalConflictMessage(preflight: DesktopPreflightResult) {
if (preflight.reason === "desktop_owned_backend_active") {
@@ -127,25 +123,6 @@ function externalConflictMessage(preflight: DesktopPreflightResult) {
: "An Unsloth server for this install is already running from a terminal. Stop that server, or run `unsloth studio update` from that terminal before using the desktop app.";
}
-async function waitForManagedServerPort(
- getPort: () => number | null,
- shouldContinue: () => boolean,
-): Promise<ManagedStartupResult> {
- while (true) {
- if (!shouldContinue()) {
- return { status: "aborted" };
- }
-
- const port = getPort();
- if (port === null) {
- await wait(MANAGED_STARTUP_POLL_MS);
- continue;
- }
-
- return { status: "ready", port };
- }
-}
-
export function useTauriBackend() {
const [status, setStatus] = useState<BackendStatus>("checking");
const statusRef = useRef<BackendStatus>(status);
@@ -157,6 +134,7 @@ export function useTauriBackend() {
const stoppingRef = useRef(false);
const mountedRef = useRef(false);
const portRef = useRef<number | null>(null);
+ const portWakeRef = useRef(createPortWakeSignal());
// commands.rs's watchdog kills a timed-out backend ~30 s later with a payload-free server-crashed;
// this keeps the timeout's log tail on screen.
const startTimedOutRef = useRef(false);
@@ -418,6 +396,7 @@ export function useTauriBackend() {
const startupResult = await waitForManagedServerPort(
() => portRef.current,
() => startingRef.current,
+ portWakeRef.current.wait,
);
if (startupResult.status === "ready") {
@@ -771,6 +750,7 @@ export function useTauriBackend() {
// A validated port means startup finished, so later crashes get the generic message.
startTimedOutRef.current = false;
setApiBase(e.payload);
+ portWakeRef.current.notify();
});
register<void>("server-crashed", () => {
diff --git a/studio/frontend/tests/managed-server-port-wake.test.ts b/studio/frontend/tests/managed-server-port-wake.test.ts
new file mode 100644
index 00000000000..48da93e8da0
--- /dev/null
+++ b/studio/frontend/tests/managed-server-port-wake.test.ts
@@ -0,0 +1,87 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+import assert from "node:assert/strict";
+import test from "node:test";
+
+import {
+ MANAGED_STARTUP_POLL_MS,
+ createPortWakeSignal,
+ waitForManagedServerPort,
+} from "../src/hooks/managed-server-port.ts";
+
+import { readSrc } from "./helpers/kit.ts";
+
+const USE_TAURI_BACKEND = readSrc("hooks/use-tauri-backend.ts");
+
+test("server-port ends the startup wait at once, not at the next poll tick", async () => {
+ const signal = createPortWakeSignal();
+ let port: number | null = null;
+ const started = performance.now();
+ const result = waitForManagedServerPort(
+ () => port,
+ () => true,
+ signal.wait,
+ );
+ setTimeout(() => {
+ port = 8888;
+ signal.notify();
+ }, 20);
+ assert.deepEqual(await result, { status: "ready", port: 8888 });
+ assert.ok(
+ performance.now() - started < MANAGED_STARTUP_POLL_MS - 200,
+ "the wait should not run out the poll interval",
+ );
+});
+
+test("without a notify the wait still polls, so a missed event cannot hang startup", async () => {
+ const signal = createPortWakeSignal();
+ let port: number | null = null;
+ let checks = 0;
+ const result = await waitForManagedServerPort(
+ () => {
+ checks += 1;
+ if (checks === 2) port = 8890;
+ return port;
+ },
+ () => true,
+ (ms) => signal.wait(Math.min(ms, 5)),
+ );
+ assert.deepEqual(result, { status: "ready", port: 8890 });
+ assert.equal(checks, 2);
+});
+
+test("a port already known skips the wait, and an aborted start reports aborted", async () => {
+ let waited = false;
+ const wait = async () => {
+ waited = true;
+ };
+ assert.deepEqual(await waitForManagedServerPort(() => 8888, () => true, wait), {
+ status: "ready",
+ port: 8888,
+ });
+ assert.deepEqual(await waitForManagedServerPort(() => null, () => false, wait), {
+ status: "aborted",
+ });
+ assert.equal(waited, false);
+});
+
+test("a notify with nobody waiting is a no-op and does not cut short a later wait", async () => {
+ const signal = createPortWakeSignal();
+ signal.notify();
+ const started = performance.now();
+ await signal.wait(40);
+ assert.ok(performance.now() - started >= 30);
+});
+
+test("the hook wakes the wait from its server-port listener", () => {
+ const listener = USE_TAURI_BACKEND.slice(
+ USE_TAURI_BACKEND.indexOf('register<number>("server-port"'),
+ );
+ const body = listener.slice(0, listener.indexOf("});"));
+ assert.match(body, /setApiBase\(e\.payload\);\s*portWakeRef\.current\.notify\(\);/);
+ assert.match(
+ USE_TAURI_BACKEND,
+ /await waitForManagedServerPort\(\s*\(\) => portRef\.current,\s*\(\) => startingRef\.current,\s*portWakeRef\.current\.wait,\s*\);/,
+ );
+});