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: share one in-flight /api/health read between the startup and route platform checks (#15794)

Daniel Han05db3950daa4
diff --git a/studio/frontend/src/config/env.ts b/studio/frontend/src/config/env.ts
index 8288e3ad75c..1e07723e3f9 100644
--- a/studio/frontend/src/config/env.ts
+++ b/studio/frontend/src/config/env.ts
@@ -108,24 +108,76 @@ let hardwareWaitSpent = false;
/** A repair or restart replaced the backend: its predecessor's verdict is no longer measured. Platform fields stay. */
export function forgetMeasuredVerdict(): void {
hardwareWaitSpent = false;
+ backendGeneration += 1;
// The old deferred flag would mask staleness, so a failed first read would look settled.
usePlatformStore.setState({ verdictStale: true, detectionDeferred: false });
}
+// Bumped on a backend replacement so a read sent to the old backend is never joined.
+let backendGeneration = 0;
+
+// main.tsx and __root.tsx's beforeLoad both read at startup: an unforced call joins an unforced
+// read in flight for the same token, URL and backend generation. Forced refreshes never share.
+let sharedHealthRead: {
+ token: string | null;
+ url: string;
+ generation: number;
+ promise: Promise<DeviceType>;
+} | null = null;
+
+function readAuthToken(): string | null {
+ // device_type is only reported to authed callers. Read the token directly: importing
+ // features/auth would cycle.
+ return typeof window === "undefined"
+ ? null
+ : localStorage.getItem("unsloth_auth_token");
+}
+
// `force` re-reads /api/health even if cached, to pick up a late-arriving tunnel URL.
export async function fetchDeviceType(options?: {
force?: boolean;
}): Promise<DeviceType> {
const { fetched } = usePlatformStore.getState();
if (fetched && !options?.force) return usePlatformStore.getState().deviceType;
+ if (options?.force) return readHealth(options);
+
+ let token: string | null;
+ try {
+ token = readAuthToken();
+ } catch {
+ // Same fallback as a failed read; readHealth records it.
+ return readHealth(options);
+ }
+ const url = apiUrl("/api/health");
+ const shared = sharedHealthRead;
+ if (
+ shared !== null &&
+ shared.token === token &&
+ shared.url === url &&
+ shared.generation === backendGeneration
+ ) {
+ return shared.promise;
+ }
+ const entry = {
+ token,
+ url,
+ generation: backendGeneration,
+ promise: readHealth(options, token),
+ };
+ sharedHealthRead = entry;
+ const release = () => {
+ if (sharedHealthRead === entry) sharedHealthRead = null;
+ };
+ entry.promise.then(release, release);
+ return entry.promise;
+}
+async function readHealth(
+ options: { force?: boolean } | undefined,
+ knownToken?: string | null,
+): Promise<DeviceType> {
try {
- // device_type is only reported to authed callers. Read the token directly: importing
- // features/auth would cycle.
- const token =
- typeof window === "undefined"
- ? null
- : localStorage.getItem("unsloth_auth_token");
+ const token = knownToken === undefined ? readAuthToken() : knownToken;
// Re-read while the backend is still detecting: chat_only is its pre-detection default and
// __root.tsx's beforeLoad acts on it.
const headers = token ? { Authorization: `Bearer ${token}` } : undefined;
diff --git a/studio/frontend/tests/health-read-coalescing.test.ts b/studio/frontend/tests/health-read-coalescing.test.ts
new file mode 100644
index 00000000000..c81ebb8e8d0
--- /dev/null
+++ b/studio/frontend/tests/health-read-coalescing.test.ts
@@ -0,0 +1,152 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+// main.tsx fires an unforced fetchDeviceType at startup while __root.tsx's beforeLoad awaits its
+// own, so both read /api/health side by side (and both poll a still-detecting backend).
+
+import assert from "node:assert/strict";
+import { register } from "node:module";
+import test from "node:test";
+
+import { installLocalStorageFake, registerBundlerResolver } from "./helpers/kit.ts";
+
+register("./helpers/vite-env-loader.mjs", import.meta.url);
+registerBundlerResolver();
+const { store } = installLocalStorageFake();
+Object.defineProperty(globalThis, "navigator", {
+ configurable: true,
+ value: { platform: "Win32", userAgent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64)" },
+});
+
+const AUTHED = {
+ device_type: "linux",
+ apple_silicon: false,
+ file_manager: "files",
+ chat_only: false,
+ chat_only_reason: null,
+ version: "2026.1.1",
+};
+const UNAUTHED = { chat_only: false };
+const DETECTING = { chat_only: true, hardware_detecting: true, version: "2026.1.1" };
+
+let calls: Array<string | null> = [];
+let replies: Array<Record<string, unknown> | null> = [];
+let fallback: Record<string, unknown> | null = UNAUTHED;
+globalThis.fetch = (async (_url: string, init?: { headers?: Record<string, string> }) => {
+ calls.push(init?.headers?.Authorization ?? null);
+ await new Promise((resolve) => setTimeout(resolve, 5));
+ const body = replies.length ? replies.shift()! : fallback;
+ if (body === null) throw new TypeError("Failed to fetch");
+ const res = { ok: true, json: async () => ({ ...body }), clone: () => res };
+ return res;
+}) as unknown as typeof fetch;
+
+const { fetchDeviceType, forgetMeasuredVerdict, usePlatformStore } = await import(
+ "../src/config/env.ts"
+);
+const { resetApiBase, setApiBase } = await import("../src/lib/api-base.ts");
+
+function reset(token: string | null) {
+ calls = [];
+ replies = [];
+ fallback = UNAUTHED;
+ if (token === null) store.delete("unsloth_auth_token");
+ else store.set("unsloth_auth_token", token);
+ usePlatformStore.setState({ fetched: false, verdictStale: false, detectionDeferred: false });
+}
+
+test("startup's two unforced callers share one /api/health read", async () => {
+ reset(null);
+ const [a, b] = await Promise.all([fetchDeviceType(), fetchDeviceType()]);
+ assert.equal(calls.length, 1, "each concurrent caller fetched /api/health on its own");
+ assert.equal(a, "windows");
+ assert.equal(b, "windows");
+ assert.equal(usePlatformStore.getState().fetched, false);
+ // Settled: an unauthed result stays unfetched, so the next navigation reads again.
+ await fetchDeviceType();
+ assert.equal(calls.length, 2);
+});
+
+test("a detecting backend is polled by one loop, not one per caller", async () => {
+ reset("tok");
+ replies = [DETECTING, DETECTING, AUTHED];
+ fallback = AUTHED;
+ const [a, b] = await Promise.all([fetchDeviceType(), fetchDeviceType()]);
+ assert.equal(calls.length, 3, "both callers polled the detecting backend");
+ assert.equal(a, "linux");
+ assert.equal(b, "linux");
+ const s = usePlatformStore.getState();
+ assert.equal(s.fetched, true);
+ assert.equal(s.capabilitiesUnknown(), false);
+ assert.equal(s.isChatOnly(), false);
+});
+
+test("a forced refresh never joins an unforced read", async () => {
+ reset(null);
+ const p1 = fetchDeviceType();
+ const p2 = fetchDeviceType({ force: true });
+ await Promise.all([p1, p2]);
+ assert.equal(calls.length, 2);
+});
+
+test("a different token does not join the read in flight", async () => {
+ reset(null);
+ fallback = UNAUTHED;
+ const p1 = fetchDeviceType();
+ store.set("unsloth_auth_token", "tok2");
+ replies = [UNAUTHED, AUTHED];
+ const p2 = fetchDeviceType();
+ const [, b] = await Promise.all([p1, p2]);
+ assert.deepEqual(calls, [null, "Bearer tok2"]);
+ assert.equal(b, "linux");
+});
+
+test("an unforced read never joins a forced refresh in flight", async () => {
+ reset(null);
+ const p1 = fetchDeviceType({ force: true });
+ const p2 = fetchDeviceType();
+ await Promise.all([p1, p2]);
+ assert.equal(calls.length, 2);
+});
+
+test("a read sent to another backend URL is not shared (desktop port change)", async () => {
+ reset(null);
+ const urls: string[] = [];
+ const realFetch = globalThis.fetch;
+ globalThis.fetch = ((url: string, init?: unknown) => {
+ urls.push(url);
+ return realFetch(url, init as RequestInit);
+ }) as typeof fetch;
+ try {
+ setApiBase(8901);
+ const p1 = fetchDeviceType();
+ setApiBase(8902);
+ const p2 = fetchDeviceType();
+ await Promise.all([p1, p2]);
+ } finally {
+ globalThis.fetch = realFetch;
+ resetApiBase();
+ }
+ assert.deepEqual(urls, ["http://127.0.0.1:8901/api/health", "http://127.0.0.1:8902/api/health"]);
+});
+
+test("a read started before a backend restart is not shared after it", async () => {
+ reset(null);
+ const p1 = fetchDeviceType();
+ forgetMeasuredVerdict();
+ const p2 = fetchDeviceType();
+ await Promise.all([p1, p2]);
+ assert.equal(calls.length, 2);
+});
+
+test("a failed read gives every joined caller the local fallback", async () => {
+ reset(null);
+ fallback = null;
+ const [a, b] = await Promise.all([fetchDeviceType(), fetchDeviceType()]);
+ assert.equal(a, "windows");
+ assert.equal(b, "windows");
+ assert.equal(usePlatformStore.getState().fetched, false);
+ fallback = UNAUTHED;
+ await fetchDeviceType();
+ assert.equal(calls.length <= 2, true);
+});