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.

Fail closed when Studio exposure prompt is aborted (#10638)

Wasim Yousef Saidc10bff849c18
diff --git a/README.md b/README.md
index 7d6c6801a26..7b0fb617eb8 100644
--- a/README.md
+++ b/README.md
@@ -176,6 +176,8 @@ unsloth studio -H 0.0.0.0 -p 8888
**LAN Access (home network)**: `Settings > API keys > LAN access`
#### Password management & headless starts
+Exposing Unsloth (`--secure`, `--cloudflare`, or a non-loopback `-H`) asks once at the terminal for a new admin password. Ctrl+C there aborts the launch rather than exposing the auto-generated one; set a password non-interactively instead, or use `-H 127.0.0.1` to stay off the network. On a non-loopback `-H` bind a terminal nobody answers is not a refusal: after ~30s Unsloth starts anyway and shuts down on the bootstrap deadline, so detached launches (`docker run -dt`, `tmux new -d`) are unaffected. Setting `UNSLOTH_STUDIO_BOOTSTRAP_TIMEOUT=0` disables that shutdown, so give those launches a password instead. A tunnel gets no such timeout and waits indefinitely rather than publish a public URL unasked, so give a detached `--secure` / `--cloudflare` launch its password non-interactively.
+
Headless starts:
```bash
UNSLOTH_STUDIO_PASSWORD='your-strong-password' unsloth studio --secure # via env var
diff --git a/studio/backend/auth/terminal_prompt.py b/studio/backend/auth/terminal_prompt.py
index 85db81af567..65acfb78ccb 100644
--- a/studio/backend/auth/terminal_prompt.py
+++ b/studio/backend/auth/terminal_prompt.py
@@ -272,24 +272,25 @@ def prompt_for_password_change(
exposure: str = "on the public internet",
first_key_timeout: "float | None" = None,
refusal_aborts: bool = True,
-) -> bool:
- """Force a new admin password before exposure; True on success.
+) -> "bool | None":
+ """Force a new admin password before exposure.
Loops until a valid, confirmed password is committed via ``apply_change``.
- Ctrl-C / EOF returns False; ``refusal_aborts`` tells the banner what the
- caller does with that, so it never promises an abort that will not happen:
- True for a tunnel (caller aborts), False for a raw bind (launch proceeds,
- because it worked before this prompt existed).
+ Returns True on success, False when the operator aborts with Ctrl-C / EOF,
+ and None when the first-key deadline detects an unattended terminal.
``exposure`` names where this launch is reachable: a tunnel really is the
public internet, a raw bind is every interface (LAN behind NAT, or the
internet on a cloud box). Claiming the wrong one trains people to ignore it.
``first_key_timeout`` bounds the wait for the FIRST keystroke, returning
- False if it never comes. Only a caller that must not block a launch passes
+ None if it never comes. Only a caller that must not block a launch passes
it: a detached pty (``tmux new -d``, ``docker run -dt``) looks exactly like
an attended terminal, so undeadlined it waits forever and never binds its
socket. Unset (the tunnel) blocks indefinitely.
+
+ ``refusal_aborts`` is accepted and ignored: an OLDER run.py beside this file
+ still passes it, and an unexpected keyword would kill that launch.
"""
if out is None:
out = sys.stderr
@@ -340,8 +341,9 @@ def prompt_for_password_change(
"No response at the terminal; leaving the auto-generated admin password in place.\n"
)
out.flush()
- return False
+ return None
except (KeyboardInterrupt, EOFError):
+ # Must agree with the banner: a caller that continues cannot claim otherwise.
out.write(
"Password change aborted; not exposing Unsloth.\n"
if refusal_aborts
diff --git a/studio/backend/run.py b/studio/backend/run.py
index c9a0e1f00a7..bdda33e49ae 100644
--- a/studio/backend/run.py
+++ b/studio/backend/run.py
@@ -2124,36 +2124,22 @@ def _terminal_password_gate(
apply_change = _apply_change,
out = sys.stderr,
exposure = _exposure_phrase(tunnel_will_start = tunnel_will_start, host = host),
- # Ctrl+C aborts a tunnel launch and only a tunnel launch; on a raw bind it
- # declines the prompt and the launch continues, so the banner must not
- # promise an abort that will not happen.
- refusal_aborts = tunnel_will_start,
# A raw bind must never block a launch that used to start. A detached pty
# (`tmux new -d`, `docker run -dt`) passes every isatty and process-group
# test yet nobody will ever type, so an undeadlined read waits forever and
- # the socket never binds; no answer is handled below as a refusal and
- # proceeds on the bootstrap deadline. The tunnel waits forever instead,
- # failing closed.
+ # the socket never binds; only that unattended first-key timeout proceeds
+ # on the bootstrap deadline. Ctrl+C / EOF is an explicit refusal and
+ # always fails closed. The tunnel waits forever instead.
first_key_timeout = None if tunnel_will_start else _UNATTENDED_PROMPT_SECONDS,
)
- if changed:
+ if changed is True:
return True, True
- if tunnel_will_start:
- # Refusing to secure a launch about to publish a public URL aborts it,
- # exactly as before.
+ if changed is False or tunnel_will_start:
+ # Ctrl+C / EOF is an explicit refusal for any reachable UI launch.
return False, False
- # A raw bind is different: it worked before the prompt existed, and aborting
- # would turn Ctrl+C into "no Studio". docker/studio_run.sh execs
- # `unsloth studio -H 0.0.0.0` and only supplies a password when the
- # initial-password file is non-empty, so `docker run -it` on a fresh volume
- # meets this prompt and aborting would stop a container that starts today.
- # Warn and proceed at the protection level this launch already had.
- #
# Which is sometimes NO protection: UNSLOTH_STUDIO_BOOTSTRAP_TIMEOUT=0 never
# arms the deadline, so say what will actually happen rather than promise a
- # shutdown -- that is the one sentence an operator acts on. Still proceed: the
- # operator disabled the deadline and cancelled the prompt deliberately, and
- # refusing to start would break the case above.
+ # shutdown -- that is the one sentence an operator acts on.
deadline_arms = should_arm_bootstrap_timeout(
host = host,
secure = secure,
@@ -2681,9 +2667,15 @@ def run_server(
is_colab = _IS_COLAB,
)
if not _pw_proceed:
+ # A raw bind passed neither flag, so naming them is a no-op for it.
print(
- "Not starting Unsloth; set a new admin password first, or launch "
- "without --secure/--cloudflare.",
+ "Not starting Unsloth; set a new admin password first, or pass one "
+ "non-interactively with --password / UNSLOTH_STUDIO_PASSWORD. "
+ + (
+ "Launch without --secure/--cloudflare to stay off the public internet."
+ if _launch_tunnel_managed
+ else "Launch with -H 127.0.0.1 to keep Unsloth off the network."
+ ),
file = sys.stderr,
flush = True,
)
diff --git a/studio/backend/tests/test_password_prompt_backstop.py b/studio/backend/tests/test_password_prompt_backstop.py
index 61fcb03aca3..8036dd07b2d 100644
--- a/studio/backend/tests/test_password_prompt_backstop.py
+++ b/studio/backend/tests/test_password_prompt_backstop.py
@@ -463,14 +463,8 @@ def test_a_headless_raw_bind_does_not_open_auth_storage(monkeypatch):
assert run._terminal_password_gate(tunnel_will_start = False, **_RAW_BIND_KWARGS) == (True, False)
-def test_refusing_the_prompt_on_a_raw_bind_still_launches(monkeypatch):
- """Ctrl+C must not turn a working launch into no Studio.
-
- `docker/studio_run.sh` execs `unsloth studio -H 0.0.0.0` and only supplies a
- password when the initial-password file is non-empty, so a fresh
- `docker run -it` meets this prompt and aborting would stop a container that
- starts today. It proceeds on the bootstrap deadline it already had.
- """
+def test_refusing_the_prompt_on_a_raw_bind_aborts(monkeypatch):
+ """Ctrl+C / EOF is an explicit refusal, even for a raw bind."""
_patch_streams(monkeypatch, tty = True)
_patch_seeded_admin(monkeypatch, requires_change = True)
@@ -482,8 +476,95 @@ def test_refusing_the_prompt_on_a_raw_bind_still_launches(monkeypatch):
lambda **_kw: False, # Ctrl+C / EOF
)
- # proceed = True, and the bootstrap credential is still injected as before.
- assert run._terminal_password_gate(tunnel_will_start = False, **_RAW_BIND_KWARGS) == (True, False)
+ assert run._terminal_password_gate(tunnel_will_start = False, **_RAW_BIND_KWARGS) == (
+ False,
+ False,
+ )
+
+
+def test_a_false_from_any_prompt_version_fails_closed(monkeypatch):
+ """A torn tree cannot say whether False was Ctrl+C or the deadline.
+
+ An OLDER terminal_prompt.py returns False for both, and elapsed time does not
+ separate them: the deadline bounds the FIRST key, so an operator who types,
+ retries validation and refuses after 30s looks exactly like a walk-away. So
+ False fails closed on every version, and a detached pty on a half-updated
+ tree stops starting rather than exposing the bootstrap password after a
+ refusal. The abort message names --password / UNSLOTH_STUDIO_PASSWORD for it.
+ """
+ for tunnel, kwargs in ((False, _RAW_BIND_KWARGS), (True, _GATE_KWARGS)):
+ _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 = tunnel, **kwargs) == (False, False)
+
+
+def test_the_banner_never_promises_an_abort_the_caller_will_not_perform(monkeypatch):
+ """An OLD run.py passes refusal_aborts=False for a raw bind and then CONTINUES.
+
+ Telling that caller's operator "Ctrl+C to abort" would talk them into walking
+ away from a server that is about to bind with the bootstrap password live, so
+ the flag still picks the wording even though this file's own run.py aborts
+ either way.
+ """
+
+ def _refuse(*_a, **_kw):
+ raise KeyboardInterrupt
+
+ monkeypatch.setattr(terminal_prompt, "_read_password", _refuse)
+
+ banners = {}
+ for refusal_aborts in (True, False):
+ out = io.StringIO()
+ terminal_prompt.prompt_for_password_change(
+ min_length = 8,
+ is_current_password = lambda _c: False,
+ apply_change = lambda _p: None,
+ out = out,
+ exposure = "on the local network",
+ refusal_aborts = refusal_aborts,
+ )
+ banners[refusal_aborts] = out.getvalue()
+
+ assert "Ctrl+C to abort" in banners[True], banners[True]
+ assert "Ctrl+C to skip" in banners[False], banners[False]
+ assert "Ctrl+C to abort" not in banners[False], banners[False]
+ # The line printed AFTER the interrupt has to agree with the banner, or the
+ # operator is told Unsloth is not being exposed by a caller that exposes it.
+ assert "not exposing Unsloth" in banners[True], banners[True]
+ assert "not exposing Unsloth" not in banners[False], banners[False]
+ assert "leaving the auto-generated admin password in place" in banners[False], banners[False]
+
+
+def test_an_older_run_py_can_still_call_this_prompt(monkeypatch):
+ """The other half of a torn tree: an OLD run.py passes refusal_aborts.
+
+ The gate is deliberately not wrapped in a broad try/except, so an unexpected
+ keyword would kill the launch. The read is faked; the binding is the subject.
+ """
+
+ def _refuse(*_a, **_kw):
+ raise KeyboardInterrupt
+
+ monkeypatch.setattr(terminal_prompt, "_read_password", _refuse)
+
+ out = io.StringIO()
+ assert (
+ terminal_prompt.prompt_for_password_change(
+ min_length = 8,
+ is_current_password = lambda _c: False,
+ apply_change = lambda _p: None,
+ out = out,
+ exposure = "on the local network",
+ first_key_timeout = 0.01,
+ refusal_aborts = False, # the old caller's keyword, now ignored
+ )
+ is False
+ )
def test_refusing_the_prompt_on_a_tunnel_still_aborts(monkeypatch):
@@ -711,7 +792,7 @@ def test_an_unattended_pty_does_not_block_a_raw_bind_forever(monkeypatch):
os.close(master)
os.close(slave)
- assert changed is False
+ assert changed is None
assert "No response at the terminal" in out.getvalue()
@@ -773,10 +854,10 @@ def test_the_gate_deadlines_a_raw_bind_prompt_and_never_the_tunnel(monkeypatch):
assert seen["first_key_timeout"] is None
-def test_a_refusal_does_not_promise_a_deadline_that_is_disabled(monkeypatch):
+def test_an_unattended_fallback_does_not_promise_a_disabled_deadline(monkeypatch):
"""With TIMEOUT=0 nothing will shut this instance down; do not say otherwise.
- The refusal path proceeds on purpose (the launch worked before the prompt
+ The unattended path proceeds on purpose (the launch worked before the prompt
existed), but must not tell the operator a deadline will rescue them when
`should_arm_bootstrap_timeout` will not arm one: that is the single sentence
they would act on.
@@ -787,7 +868,7 @@ def test_a_refusal_does_not_promise_a_deadline_that_is_disabled(monkeypatch):
from auth import terminal_prompt
- monkeypatch.setattr(terminal_prompt, "prompt_for_password_change", lambda **_kw: False)
+ monkeypatch.setattr(terminal_prompt, "prompt_for_password_change", lambda **_kw: None)
assert run._terminal_password_gate(tunnel_will_start = False, **_RAW_BIND_KWARGS) == (True, False)
err = stderr.getvalue()
@@ -795,14 +876,14 @@ def test_a_refusal_does_not_promise_a_deadline_that_is_disabled(monkeypatch):
assert "shuts down after the bootstrap deadline" not in err, err
-def test_a_refusal_still_names_the_deadline_when_one_will_arm(monkeypatch):
+def test_an_unattended_fallback_names_the_deadline_when_one_will_arm(monkeypatch):
monkeypatch.delenv("UNSLOTH_STUDIO_BOOTSTRAP_TIMEOUT", raising = False)
stderr = _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)
+ monkeypatch.setattr(terminal_prompt, "prompt_for_password_change", lambda **_kw: None)
assert run._terminal_password_gate(tunnel_will_start = False, **_RAW_BIND_KWARGS) == (True, False)
err = stderr.getvalue()
diff --git a/studio/backend/tests/test_wildcard_prompt_compatibility.py b/studio/backend/tests/test_wildcard_prompt_compatibility.py
index 23d609177f9..8e8d162580c 100644
--- a/studio/backend/tests/test_wildcard_prompt_compatibility.py
+++ b/studio/backend/tests/test_wildcard_prompt_compatibility.py
@@ -143,6 +143,8 @@ def test_the_prompt_signature_stays_keyword_compatible():
params = inspect.signature(terminal_prompt.prompt_for_password_change).parameters
assert params["exposure"].default == "on the public internet"
+ # An old run.py still passes refusal_aborts, and it still picks the banner.
+ assert "refusal_aborts" in params
for name, param in params.items():
assert param.kind is not inspect.Parameter.POSITIONAL_ONLY, name
diff --git a/unsloth_cli/commands/studio.py b/unsloth_cli/commands/studio.py
index b3aa3a0d87b..929298f0d51 100644
--- a/unsloth_cli/commands/studio.py
+++ b/unsloth_cli/commands/studio.py
@@ -1467,14 +1467,9 @@ def _enforce_password_change_before_exposure(
_pbkdf2_hex(candidate, password_salt.encode("utf-8")), password_hash
)
- # Ctrl+C aborts a TUNNEL launch only; on a raw bind it declines the prompt, because that launch worked before this gate existed.
- refusal = (
- "Ctrl+C to abort."
- if tunnel_will_start
- else "Ctrl+C to skip, and Unsloth starts with the auto-generated password."
- )
typer.echo(
- f"Unsloth Studio will be reachable {exposure}, so set a password now. {refusal}",
+ f"Unsloth Studio will be reachable {exposure}, so set a password now. "
+ "Ctrl+C to abort.",
err = True,
)
try:
@@ -1495,25 +1490,19 @@ def _enforce_password_change_before_exposure(
os.environ[_UNATTENDED_PROMPT_DONE_ENV] = "1"
return
except (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, so Ctrl+C returns it to pre-prompt behaviour, as run.py's gate does.
+ # An abort needs the non-interactive hatch more than the old warn did.
typer.echo(
- "\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.",
+ "\nError: password change aborted; refusing to expose Unsloth "
+ "with the default admin password. Re-run and set a password, or "
+ "pass one with --password / UNSLOTH_STUDIO_PASSWORD, or "
+ + (
+ "launch without --secure/--cloudflare."
+ if tunnel_will_start
+ else "launch with -H 127.0.0.1 to stay off the network."
+ ),
err = True,
)
- os.environ[_UNATTENDED_PROMPT_DONE_ENV] = "1"
- return
+ raise typer.Exit(1)
_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.py
index 5dceca759f3..3e21ec41c21 100644
--- a/unsloth_cli/tests/test_studio_password_prompt.py
+++ b/unsloth_cli/tests/test_studio_password_prompt.py
@@ -2171,13 +2171,11 @@ def test_the_cli_deadline_sentence_tracks_the_configured_timeout(monkeypatch):
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.
+def test_a_raw_bind_ctrl_c_aborts_the_launch(monkeypatch, tmp_path):
+ """Ctrl+C on `unsloth studio -H 0.0.0.0` is an explicit refusal."""
+ import os as _os
+ import typer
- The CLI mirror is the gate that actually runs for that command, so exiting
- here makes run.py's warn-and-proceed unreachable and leaves the
- `docker run -it` case unprotected.
- """
studio_mod = _studio()
events = _install_prompt_env(monkeypatch, tmp_path, interactive = True)
_seed_auth(studio_mod)
@@ -2187,14 +2185,51 @@ def test_a_raw_bind_ctrl_c_does_not_abort_the_launch(monkeypatch, tmp_path):
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
- )
+ with pytest.raises(typer.Exit):
+ 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
+ assert _os.environ.get(studio_mod._UNATTENDED_PROMPT_DONE_ENV) is None
del events
+ "args,present,absent",
+ [
+ # A raw bind passed neither flag; -H 127.0.0.1 is its way off the network.
+ (dict(cloudflare = None, host = "0.0.0.0", secure = False), "-H 127.0.0.1", "--cloudflare"),
+ (
+ dict(cloudflare = None, host = "127.0.0.1", secure = True),
+ "--secure/--cloudflare",
+ "-H 127.0.0.1",
+ ),
+ ],
+)
+def test_the_abort_names_a_remedy_this_launch_actually_has(
+ monkeypatch, tmp_path, capsys, args, present, absent
+):
+ """An abort that leaves no way forward just gets retried the same way."""
+ import typer
+
+ 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)
+
+ with pytest.raises(typer.Exit):
+ studio_mod._enforce_password_change_before_exposure(api_only = False, **args)
+
+ err = capsys.readouterr().err
+ assert "UNSLOTH_STUDIO_PASSWORD" in err, err
+ assert present in err, err
+ assert absent not in err, err
+
+
def test_a_tunnel_ctrl_c_still_aborts(monkeypatch, tmp_path):
studio_mod = _studio()
_install_prompt_env(monkeypatch, tmp_path, interactive = True)
@@ -2276,8 +2311,8 @@ def test_the_mark_never_lets_a_tunnel_skip_its_prompt(monkeypatch, tmp_path):
def _banner(monkeypatch, tmp_path, args):
"""Run the gate far enough to capture the banner it prints, then bail out.
- Clears the unattended mark first: bailing out with an interrupt SETS it on a
- raw bind, so a second call in the same test would capture no banner.
+ Clears the unattended mark first so one banner assertion cannot inherit the
+ marker from another.
"""
import os as _os
@@ -2299,17 +2334,10 @@ def _banner(monkeypatch, tmp_path, args):
return (result.output or "") + (getattr(result, "stderr", "") or "")
-def test_the_banner_does_not_promise_an_abort_a_raw_bind_will_not_perform(monkeypatch, tmp_path):
- """`Ctrl+C to abort` is true for a tunnel and false for a raw bind.
-
- On a raw bind the interrupt declines the prompt and the launch continues by
- design, because it worked before this gate existed. An operator who reads
- "abort", presses Ctrl+C and walks away would be leaving a server up on the
- network with the auto-generated password.
- """
+def test_the_banner_promises_abort_for_every_exposed_bind(monkeypatch, tmp_path):
raw = _banner(monkeypatch, tmp_path, ["-H", "0.0.0.0"])
- assert "Ctrl+C to abort" not in raw
- assert "Ctrl+C to skip" in raw
+ assert "Ctrl+C to abort" in raw
+ assert "Ctrl+C to skip" not in raw
tunnel = _banner(monkeypatch, tmp_path, ["--secure"])
assert "Ctrl+C to abort" in tunnel