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.

Unsloth Studio: keep tabs signed in on plain-HTTP origins when two refresh at once (#16755)

Leo Borcherdingf643501244af
diff --git a/studio/frontend/src/features/auth/api.ts b/studio/frontend/src/features/auth/api.ts
index 94895fe7ddb..4c9ee26b9ac 100644
--- a/studio/frontend/src/features/auth/api.ts
+++ b/studio/frontend/src/features/auth/api.ts
@@ -306,6 +306,8 @@ const REFRESH_LOCK_WAIT_MS = 8000;
const REFRESH_RETRY_DEFAULT_MS = 1000;
const REFRESH_RETRY_MIN_MS = 250;
const REFRESH_RETRY_MAX_MS = 5000;
+const REFRESH_PEER_WAIT_MS = 3000;
+const REFRESH_PEER_POLL_MS = 100;
function refreshRetryDelay(response: Response): number {
const header = response.headers.get("Retry-After")?.trim();
@@ -319,7 +321,33 @@ function refreshRetryDelay(response: Response): number {
);
}
-async function runRefresh(refreshToken: string, preserveSessionMark = false): Promise<RefreshOutcome> {
+/** The winner's 200 committed before this 401 was sent, so its pair lands here within moments. */
+async function peerRotated(
+ refreshToken: string,
+ startGeneration: number,
+ sessionMark: string,
+): Promise<boolean> {
+ const deadline = Date.now() + REFRESH_PEER_WAIT_MS;
+ for (;;) {
+ if (
+ startGeneration !== logoutGeneration ||
+ accountTransitionPending() ||
+ getAuthSessionMark() !== sessionMark
+ )
+ return false;
+ const current = getRefreshToken();
+ if (current !== refreshToken) return current !== null;
+ if (Date.now() >= deadline) return false;
+ await wait(REFRESH_PEER_POLL_MS);
+ }
+}
+
+async function runRefresh(
+ refreshToken: string,
+ preserveSessionMark = false,
+ // No Web Locks: a peer tab may spend this token first. Only a rotation under this mark is theirs.
+ peerSessionMark?: string,
+): Promise<RefreshOutcome> {
const startGeneration = logoutGeneration;
const startedAt = Date.now();
const superseded = (): RefreshOutcome | null => {
@@ -340,6 +368,11 @@ async function runRefresh(refreshToken: string, preserveSessionMark = false): Pr
}
// Only a 401 rejects the token; a 5xx, 429 or proxy error says nothing about the session.
if (response.status === 401) {
+ if (
+ peerSessionMark !== undefined &&
+ (await peerRotated(refreshToken, startGeneration, peerSessionMark))
+ )
+ return "ok";
clearAuthTokensIfCurrent(refreshToken);
return "rejected";
}
@@ -381,7 +414,8 @@ export async function refreshSessionOutcome(): Promise<RefreshOutcome> {
const generation = logoutGeneration;
const locks = typeof window !== "undefined" ? window.navigator?.locks : undefined;
- const sessionMark = locks ? ensureAuthSessionMark() : null;
+ // Without a mark at refresh start, a peer's backfill cannot be told from a logout and sign-in.
+ const sessionMark = locks ? ensureAuthSessionMark() : getAuthSessionMark();
if (getRefreshToken() !== refreshToken) return "unavailable";
const promise = locks
? (async (): Promise<RefreshOutcome> => {
@@ -409,7 +443,7 @@ export async function refreshSessionOutcome(): Promise<RefreshOutcome> {
clearTimeout(timer);
}
})()
- : runRefresh(refreshToken);
+ : runRefresh(refreshToken, false, sessionMark ?? undefined);
refreshInflight = promise;
refreshInflightToken = refreshToken;
try {
diff --git a/studio/frontend/tests/auth-refresh-transient.test.ts b/studio/frontend/tests/auth-refresh-transient.test.ts
index fabc612b5bc..5fa8ebbca65 100644
--- a/studio/frontend/tests/auth-refresh-transient.test.ts
+++ b/studio/frontend/tests/auth-refresh-transient.test.ts
@@ -405,3 +405,62 @@ test("the route guard still sends a rejected refresh to /login", async (t) => {
await assert.rejects(loadGuards(f).requireAuth(), /redirect \/login/);
assert.equal(f.tokens.refresh, null);
});
+
+// Web Locks need a secure context, so plain-HTTP tabs can race one single-use refresh token.
+test("without Web Locks a peer tab's rotation does not sign every tab out", async (t) => {
+ const f = fixture(t, [
+ () => new Promise<Response>((resolve) =>
+ setImmediate(() => setImmediate(() => resolve(rotated())))),
+ () => new Response(null, { status: 401 }),
+ ]);
+ const otherWindow = f.makeApi();
+ const results = await Promise.all([
+ f.api.refreshSessionOutcome(),
+ otherWindow.refreshSessionOutcome(),
+ ]);
+ assert.deepEqual(results, ["ok", "ok"]);
+ assert.equal(f.refreshBodies.length, 2);
+ assert.deepEqual(f.tokens, { access: "new-access", refresh: "new-refresh" });
+ assert.equal(f.clears(), 0);
+});
+
+test("without Web Locks a 401 after a peer stored the rotation replays instead of redirecting", async (t) => {
+ const f = fixture(t, [() => {
+ f.tokens.access = "new-access";
+ f.tokens.refresh = "new-refresh";
+ return new Response(null, { status: 401 });
+ }]);
+ const response = await f.api.authFetch("/api/models");
+ assert.equal(response.status, 200);
+ assert.deepEqual(f.apiTokens, ["Bearer old-access", "Bearer new-access"]);
+ assert.equal(f.clears(), 0);
+ await f.settle();
+ assert.equal(f.location.href, "");
+});
+
+test("without Web Locks a sign-in that replaced the session is not taken for a peer's rotation", async (t) => {
+ const f = fixture(t, [() => {
+ f.state.mark = "another-session";
+ f.tokens.access = "other-access";
+ f.tokens.refresh = "other-refresh";
+ return new Response(null, { status: 401 });
+ }]);
+ assert.equal(await f.api.refreshSessionOutcome(), "rejected");
+ assert.deepEqual(f.tokens, { access: "other-access", refresh: "other-refresh" });
+ assert.equal(f.clears(), 0);
+});
+
+test("without Web Locks a session with no mark does not take a later sign-in for a peer's rotation", async (t) => {
+ const f = fixture(t, [() => {
+ f.state.mark = "new-sign-in";
+ f.tokens.access = "other-access";
+ f.tokens.refresh = "other-refresh";
+ return new Response(null, { status: 401 });
+ }]);
+ f.state.mark = null;
+ const response = await f.api.authFetch("/api/models");
+ assert.equal(response.status, 401);
+ assert.deepEqual(f.apiTokens, ["Bearer old-access"]);
+ assert.deepEqual(f.tokens, { access: "other-access", refresh: "other-refresh" });
+ assert.equal(f.clears(), 0);
+});
diff --git a/studio/frontend/tests/tts-custom-connections-disabled.test.ts b/studio/frontend/tests/tts-custom-connections-disabled.test.ts
index b1f109177d7..31237dcf005 100644
--- a/studio/frontend/tests/tts-custom-connections-disabled.test.ts
+++ b/studio/frontend/tests/tts-custom-connections-disabled.test.ts
@@ -301,6 +301,7 @@ test("authFetch invokes its policy guard after refresh and before retry", async
accessToken = null;
refreshToken = null;
},
+ getAuthSessionMark: () => "session",
getAuthToken: () => accessToken,
getRefreshToken: () => refreshToken,
mustChangePassword: () => false,