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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: stop the CLI gate undoing what the backend gate decided
danielhanchenb490f99ec035
diff --git a/studio/backend/run.py b/studio/backend/run.pyindex cdf1472b400..cd81a737c15 100644--- a/studio/backend/run.py+++ b/studio/backend/run.py@@ -2217,6 +2217,14 @@ def _terminal_password_gate(if not tunnel_will_start and not bind_is_exposed:return True, False+ # The parent already held this terminal open and got nothing. Repeating the+ # wait would double the fallback (and triple it on the `studio run` path,+ # which re-enters the CLI gate after re-exec), which is exactly the startup+ # stall the deadline was chosen to stay under. Consumed, not just read, so it+ # cannot leak into a later launch from the same environment.+ if os.environ.pop("UNSLOTH_STUDIO_UNATTENDED_PROMPT_DONE", "") and not tunnel_will_start:+ return True, False+# A raw-bind-only launch decides promptability BEFORE opening auth storage.# Before this gate learned about non-tunnel exposure it returned above, so a# headless container never reached ensure_default_admin() from here. Doing sodiff --git a/studio/backend/tests/test_password_prompt_backstop.py b/studio/backend/tests/test_password_prompt_backstop.pyindex 484d839362e..a779a5a727c 100644--- a/studio/backend/tests/test_password_prompt_backstop.py+++ b/studio/backend/tests/test_password_prompt_backstop.py@@ -783,3 +783,44 @@ def test_a_refusal_still_names_the_deadline_when_one_will_arm(monkeypatch):err = stderr.getvalue()assert "shuts down after the bootstrap deadline" in err, errassert "DISABLED" not in err, err+++def test_the_child_does_not_repeat_a_prompt_the_parent_already_gave_up_on(monkeypatch):+ """A 30s fallback must not become 60s across the re-exec.++ The CLI parent holds the terminal for its deadline, gets nothing, warns and+ launches. The child gate then sees the SAME unattended pty. Without a handoff+ it waits the whole deadline again, and on the `studio run` path a third time,+ which is exactly the startup stall the 30s value was chosen to stay under.+ """+ monkeypatch.setenv("UNSLOTH_STUDIO_UNATTENDED_PROMPT_DONE", "1")+ _patch_streams(monkeypatch, tty = True)++ def _boom(*_a, **_k):+ raise AssertionError("the child prompted again after the parent gave up")++ from auth import storage as _storage+ monkeypatch.setattr(_storage, "ensure_default_admin", _boom)++ assert run._terminal_password_gate(+ tunnel_will_start = False, **_RAW_BIND_KWARGS+ ) == (True, False)+ # Consumed, so a later launch from the same environment is not silently+ # stripped of its own prompt.+ import os as _os+ assert _os.environ.get("UNSLOTH_STUDIO_UNATTENDED_PROMPT_DONE") is None+++def test_the_marker_never_silences_a_tunnel_launch(monkeypatch):+ """A public URL fails closed regardless of what the parent did."""+ monkeypatch.setenv("UNSLOTH_STUDIO_UNATTENDED_PROMPT_DONE", "1")+ _patch_streams(monkeypatch, tty = True)+ _patch_seeded_admin(monkeypatch, requires_change = True)++ from auth import terminal_prompt+ monkeypatch.setattr(terminal_prompt, "prompt_for_password_change",+ lambda **_kw: False)++ assert run._terminal_password_gate(+ tunnel_will_start = True, **_GATE_KWARGS+ ) == (False, False)diff --git a/unsloth_cli/commands/studio.py b/unsloth_cli/commands/studio.pyindex 1472d68c6f3..d6118dfb612 100644--- a/unsloth_cli/commands/studio.py+++ b/unsloth_cli/commands/studio.py@@ -1305,6 +1305,31 @@ def _bootstrap_deadline_active() -> bool:return True+# Set by the parent when it has already put the prompt in front of this terminal+# and got nothing, so the re-exec'd child does not repeat the wait.+_UNATTENDED_PROMPT_DONE_ENV = "UNSLOTH_STUDIO_UNATTENDED_PROMPT_DONE"+++def _deadline_sentence() -> str:+ """Say what will actually happen, not what usually happens.++ UNSLOTH_STUDIO_BOOTSTRAP_TIMEOUT=0 disables the shutdown entirely, and this+ is the sentence an operator acts on, so promising a deadline that will never+ arm is worse than saying nothing.+ """+ if _bootstrap_deadline_active():+ return (+ "Unsloth shuts down after the bootstrap deadline "+ "(UNSLOTH_STUDIO_BOOTSTRAP_TIMEOUT, default 1h) unless the password "+ "is changed."+ )+ return (+ "The bootstrap shutdown deadline is DISABLED for this launch "+ "(UNSLOTH_STUDIO_BOOTSTRAP_TIMEOUT=0), so nothing will stop it serving "+ "that credential."+ )++def _generate_reset_password() -> str:"""Readable 4-word passphrase; the user has to type this one back in."""try:@@ -1818,21 +1843,43 @@ def _enforce_password_change_before_exposure(typer.echo("Warning: no response at the terminal, so Unsloth is starting with ""the auto-generated admin password on a bind that is reachable from "- "the network. Unsloth shuts down after the bootstrap deadline "- "(UNSLOTH_STUDIO_BOOTSTRAP_TIMEOUT, default 1h) unless the password "- "is changed. Change it by logging in, or with `unsloth studio "- "reset-password`.",+ f"the network. {_deadline_sentence()} Change it by logging in, or "+ "with `unsloth studio reset-password`.",err = True,)+ # The child gate sees the SAME unattended terminal and would wait its+ # own deadline over again, so a 30s fallback becomes 60s, or 90 on the+ # `studio run` path that re-enters this gate after re-exec. That is+ # long enough to trip a startup watchdog, which is the restart loop+ # the deadline exists to avoid. Tell the child the terminal has+ # already been tried. A direct `python run.py` sets nothing, so it+ # keeps its own backstop.+ os.environ[_UNATTENDED_PROMPT_DONE_ENV] = "1"returnexcept (KeyboardInterrupt, EOFError):+ if tunnel_will_start:+ typer.echo(+ "\nError: password change aborted; refusing to publish Unsloth "+ "on a public URL with the default admin password. Re-run and "+ "set a password, or launch without --secure/--cloudflare.",+ err = True,+ )+ raise typer.Exit(1)+ # A raw bind is not a publication. This launch worked before the+ # prompt existed, so Ctrl+C returns it to what it did then rather+ # than refusing to start; run.py's gate makes the same choice, and+ # this mirror is the one that actually runs for `unsloth studio+ # -H 0.0.0.0`, so disagreeing here would make that unreachable.typer.echo(- "\nError: password change aborted; refusing to expose Unsloth "- "with the default admin password. Re-run and set a password, "- "or launch without --secure/--cloudflare.",+ "\nWarning: password change aborted, so Unsloth is starting with "+ "the auto-generated admin password on a bind that is reachable "+ f"from the network. {_deadline_sentence()} Change it by logging "+ "in, with `unsloth studio reset-password`, or by passing "+ "--password / UNSLOTH_STUDIO_PASSWORD.",err = True,)- raise typer.Exit(1)+ os.environ[_UNATTENDED_PROMPT_DONE_ENV] = "1"+ return_cli_update_password(conn, DEFAULT_ADMIN_USERNAME, new_password)typer.echo(f"Password updated for '{DEFAULT_ADMIN_USERNAME}'.", err = True)finally:diff --git a/unsloth_cli/tests/test_studio_password_prompt.py b/unsloth_cli/tests/test_studio_password_prompt.pyindex 228cafceca9..98f9b93d692 100644--- a/unsloth_cli/tests/test_studio_password_prompt.py+++ b/unsloth_cli/tests/test_studio_password_prompt.py@@ -2053,3 +2053,57 @@ def test_the_exposure_wording_matches_what_will_actually_happen(cloudflare = cloudflare, host = host, secure = secure, api_only = False)assert tunnel is expect_tunnel_wording+++def test_the_cli_deadline_sentence_tracks_the_configured_timeout(monkeypatch):+ """The CLI fallback must not promise a shutdown that will never arm."""+ studio_mod = _studio()+ monkeypatch.delenv("UNSLOTH_STUDIO_BOOTSTRAP_TIMEOUT", raising = False)+ assert "shuts down after the bootstrap deadline" in studio_mod._deadline_sentence()+ for disabled in ("0", "-1"):+ monkeypatch.setenv("UNSLOTH_STUDIO_BOOTSTRAP_TIMEOUT", disabled)+ sentence = studio_mod._deadline_sentence()+ assert "DISABLED for this launch" in sentence, disabled+ assert "shuts down after" not in sentence, disabled+++def test_a_raw_bind_ctrl_c_does_not_abort_the_launch(monkeypatch, tmp_path):+ """Ctrl+C on `unsloth studio -H 0.0.0.0` must leave it starting.++ The CLI mirror is the gate that actually runs for that command, so if it+ exits here the warn-and-proceed in run.py is unreachable and the docker+ run -it case the two of them exist to protect is not protected at all.+ """+ studio_mod = _studio()+ events = _install_prompt_env(monkeypatch, tmp_path, interactive = True)+ _seed_auth(studio_mod)++ def _abort(*_a, **_kw):+ raise KeyboardInterrupt++ monkeypatch.setattr(studio_mod._password_prompt, "prompt_new_password", _abort)++ # Returns rather than raising typer.Exit: the launch continues.+ studio_mod._enforce_password_change_before_exposure(+ cloudflare = None, host = "0.0.0.0", secure = False, api_only = False+ )+ assert _auth_state(studio_mod)["must_change_password"] == 1+ del events+++def test_a_tunnel_ctrl_c_still_aborts(monkeypatch, tmp_path):+ studio_mod = _studio()+ _install_prompt_env(monkeypatch, tmp_path, interactive = True)+ _seed_auth(studio_mod)++ def _abort(*_a, **_kw):+ raise KeyboardInterrupt++ monkeypatch.setattr(studio_mod._password_prompt, "prompt_new_password", _abort)++ import typer++ with pytest.raises(typer.Exit):+ studio_mod._enforce_password_change_before_exposure(+ cloudflare = None, host = "127.0.0.1", secure = True, api_only = False+ )