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.

Stop the settings number fields stealing focus from each other (#11211)

Daniel Han2f28fa97c8ba
diff --git a/studio/backend/main.py b/studio/backend/main.py
index fcb30ddebca..16f3c10fbfd 100644
--- a/studio/backend/main.py
+++ b/studio/backend/main.py
@@ -663,11 +663,7 @@ def banner_autofill_available(app_state, environ) -> bool:
def bootstrap_banner_lines(
- username: str,
- bootstrap_path,
- password: Optional[str],
- *,
- autofill_available: bool,
+ username: str, bootstrap_path, password: Optional[str], *, autofill_available: bool
) -> "list[str]":
"""The first-boot banner for a freshly created admin account.
@@ -830,9 +826,7 @@ async def lifespan(app: FastAPI):
# to serve with the default credential: never capture the bootstrap password into app.state.
_suppress_bootstrap = getattr(app.state, "suppress_bootstrap_injection", False)
_created = storage.ensure_default_admin()
- app.state.bootstrap_password = (
- None if _suppress_bootstrap else storage.get_bootstrap_password()
- )
+ app.state.bootstrap_password = None if _suppress_bootstrap else storage.get_bootstrap_password()
# A tunnel launch runs the pre-bind gate first and that gate seeds the account, so
# _created is False there and the whole banner would be skipped on exactly the launch
# that needs it. requires_password_change: the gate may also have taken a new password
diff --git a/studio/backend/tests/test_bootstrap_banner_password_visibility.py b/studio/backend/tests/test_bootstrap_banner_password_visibility.py
index 2dcdcd7a3d5..e1e30e3407e 100644
--- a/studio/backend/tests/test_bootstrap_banner_password_visibility.py
+++ b/studio/backend/tests/test_bootstrap_banner_password_visibility.py
@@ -9,7 +9,6 @@ PATH = "/opt/unsloth-studio/auth/.bootstrap_password"
def _lines(**kwargs):
from main import bootstrap_banner_lines
-
return bootstrap_banner_lines("unsloth", PATH, PASSWORD, **kwargs)
@@ -31,9 +30,7 @@ def test_a_launch_without_autofill_prints_the_password():
def test_a_missing_password_falls_back_to_the_path():
from main import bootstrap_banner_lines
- body = "\n".join(
- bootstrap_banner_lines("unsloth", PATH, None, autofill_available = False)
- )
+ body = "\n".join(bootstrap_banner_lines("unsloth", PATH, None, autofill_available = False))
assert "password: None" not in body
assert f"password saved to: {PATH}" in body
@@ -69,9 +66,9 @@ def test_the_banner_guard_does_not_key_only_on_this_call_creating_the_admin():
guard = _banner_guard()
assert "admin_created_this_process" in guard
- assert "requires_password_change" in guard, (
- "the gate can take a new password at its prompt, which retires the bootstrap one"
- )
+ assert (
+ "requires_password_change" in guard
+ ), "the gate can take a new password at its prompt, which retires the bootstrap one"
def test_the_creation_flag_survives_a_later_call_that_creates_nothing(monkeypatch):
@@ -156,6 +153,6 @@ def test_run_server_resets_the_per_launch_flags_before_the_gate():
]
assert "app.state.api_only = api_only" in assigns
- assert "app.state.suppress_bootstrap_injection = False" in assigns, (
- "a sticky True from an earlier launch withholds the autofill that is available"
- )
+ assert (
+ "app.state.suppress_bootstrap_injection = False" in assigns
+ ), "a sticky True from an earlier launch withholds the autofill that is available"
diff --git a/studio/frontend/src/features/model-picker/components/numeric-value-input.tsx b/studio/frontend/src/features/model-picker/components/numeric-value-input.tsx
index 4dc22d6ddb9..ad57c7ac749 100644
--- a/studio/frontend/src/features/model-picker/components/numeric-value-input.tsx
+++ b/studio/frontend/src/features/model-picker/components/numeric-value-input.tsx
@@ -171,7 +171,18 @@ export const NumericValueInput = forwardRef<
setDraft(next);
setFocused(true);
const target = e.currentTarget;
- requestAnimationFrame(() => target.select());
+ requestAnimationFrame(() => {
+ // Only while this input still holds focus. select() FOCUSES a blurred input in
+ // Chrome, and it takes focus off another element to do it, so an unconditional
+ // select a frame later steals focus back from wherever the user moved to. Two of
+ // these focused in the same task (tab, or a click straight from one field to the
+ // next) then steal from each other every frame forever: each steal fires focus on
+ // the other input, whose onFocus queues the next frame's steal. Measured on
+ // /images with Steps and Guidance: 7870 of 9081 animation frames in 76s scheduled
+ // from here, and every popover opened while it runs is dismissed immediately
+ // because focus keeps landing outside it.
+ if (document.activeElement === target) target.select();
+ });
}}
onBlur={() => {
if (cancelBlurCommitRef.current) {
diff --git a/studio/frontend/tests/numeric-input-focus-steal.test.ts b/studio/frontend/tests/numeric-input-focus-steal.test.ts
new file mode 100644
index 00000000000..cfd86c8b2ce
--- /dev/null
+++ b/studio/frontend/tests/numeric-input-focus-steal.test.ts
@@ -0,0 +1,48 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+// NumericValueInput selects its contents a frame after it takes focus. In Chrome
+// HTMLInputElement.select() FOCUSES a blurred input, taking focus off whatever holds it, so
+// an unguarded select steals focus back from the field the user moved to. Two of these
+// focused in one task -- tab, or a click straight from one field to the next -- then steal
+// from each other every frame for as long as the page stays open, because each steal fires
+// focus on the other input, whose handler queues the next one.
+//
+// The behaviour is pinned in the browser by tests/studio/playwright_image_download_cancel_retry.py.
+// This is the cheap half: it fails in unit CI, with no browser, if the guard is dropped.
+
+import assert from "node:assert/strict";
+import test from "node:test";
+
+import { readSrc } from "./helpers/kit.ts";
+
+const SOURCE = "features/model-picker/components/numeric-value-input.tsx";
+
+test("the queued select is guarded on the input still having focus", () => {
+ const source = readSrc(SOURCE);
+ const raf = source.indexOf("requestAnimationFrame(");
+ assert.ok(raf > 0, "onFocus should still defer the select by a frame");
+
+ const guard = source.indexOf("document.activeElement === target", raf);
+ const select = source.indexOf("target.select()", raf);
+ assert.ok(
+ guard > 0,
+ "the deferred select must check that the input still holds focus, or it steals it back",
+ );
+ assert.ok(
+ guard < select,
+ "the focus check has to run before select(), not after it",
+ );
+});
+
+test("nothing else selects an input without checking focus first", () => {
+ // DRIFT: a second deferred select added later would reintroduce the loop while the guard
+ // above still reads as present.
+ const source = readSrc(SOURCE);
+ const selects = [...source.matchAll(/\.select\(\)/g)];
+ assert.equal(
+ selects.length,
+ 1,
+ "more than one select() here; each one needs its own focus guard",
+ );
+});
diff --git a/tests/studio/playwright_image_download_cancel_retry.py b/tests/studio/playwright_image_download_cancel_retry.py
index 47cb20b4ea2..2cc6e4e4400 100644
--- a/tests/studio/playwright_image_download_cancel_retry.py
+++ b/tests/studio/playwright_image_download_cancel_retry.py
@@ -88,6 +88,52 @@ def _entry(repo_id: str) -> dict[str, object]:
}
+# The settings fields select their contents a frame after they take focus. select() FOCUSES a
+# blurred input in Chrome, taking focus off whatever holds it, so an unguarded select steals
+# focus back from the field the user moved to -- and since each steal fires focus on the other
+# field, whose handler queues the next steal, two of them lock into a loop that runs for as
+# long as the page is open. Everything downstream of it is unusable: the model picker opened
+# during the loop is dismissed in the frame after it opens, which is how this was found.
+#
+# Deterministic, unlike the loop's arrival in a normal run: both focuses happen in one task, so
+# the first field's queued select is still pending when focus moves on, which is the race a user
+# hits tabbing between the two. Measured against the build before the fix: 60 focus events in
+# the 500ms window, against 0 with it.
+_FOCUS_STEAL_PROBE = """() => new Promise(resolve => {
+ const box = name => document.querySelector(`input[aria-label="${name}"]`);
+ const first = box('Steps'), second = box('Guidance');
+ if (!first || !second) return resolve({error: 'Steps/Guidance inputs are not on the page'});
+ let events = 0;
+ const count = () => { events++; };
+ document.addEventListener('focusin', count, true);
+ first.focus();
+ second.focus();
+ const settled = events;
+ setTimeout(() => {
+ document.removeEventListener('focusin', count, true);
+ resolve({
+ active: document.activeElement?.getAttribute('aria-label') ?? null,
+ churn: events - settled,
+ });
+ }, 500);
+})"""
+
+
+def _assert_a_queued_select_does_not_steal_focus(page) -> None:
+ result = page.evaluate(_FOCUS_STEAL_PROBE)
+ assert not result.get("error"), result["error"]
+ # The end-of-frame answer looks right even while the loop runs, because the second field's
+ # steal is the last one in each frame. The churn is what tells them apart, so assert both.
+ assert (
+ result["active"] == "Guidance"
+ ), f"focus left the field it was moved to: {result['active']}"
+ assert result["churn"] <= 2, (
+ f"the settings fields are stealing focus from each other: {result['churn']} focus "
+ "events in 500ms after focus settled. A queued select() must not re-focus an input "
+ "the user has already left."
+ )
+
+
def _open_quant(page, *, navigate: bool) -> None:
if navigate:
page.goto(f"{BASE_URL}/images", wait_until = "domcontentloaded")
@@ -385,6 +431,7 @@ def main() -> None:
page.get_by_role("option", name = "Download only", exact = True).click()
page.get_by_role("textbox", name = "Steps", exact = True).fill("17")
page.get_by_role("textbox", name = "Guidance", exact = True).fill("2.5")
+ _assert_a_queued_select_does_not_steal_focus(page)
_open_quant(page, navigate = not DOWNLOAD_ONLY)
if DOWNLOAD_ONLY:
with page.expect_request(