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: run the Shutdown cleanup off the event loop (#13534)
Daniel Han3b418047add5
diff --git a/studio/backend/main.py b/studio/backend/main.pyindex da7fb4d6fea..6b4ff6ae6cd 100644--- a/studio/backend/main.py+++ b/studio/backend/main.py@@ -2296,12 +2296,27 @@ async def desktop_shutdown_server(request: Request):return _schedule_shutdown(request)+_shutdown_thread_lock = threading.Lock()++def _schedule_shutdown(request: Request) -> dict:async def _delayed_shutdown():await asyncio.sleep(0.2) # Let the HTTP response return firsttrigger = getattr(request.app.state, "trigger_shutdown", None)if trigger is not None:- trigger()+ # On its own thread, like the signal path: the cleanup waits seconds on subprocesses+ # (llama-server alone up to 5 s SIGTERM + 5 s SIGKILL), and run on this loop it kept+ # uvicorn from closing the port until all of it was done. The main thread still waits+ # for it (_shutdown_event) before the process exits. One cleanup per launch: keyed by+ # the trigger, since an embedded host's next run_server() installs a new one.+ with _shutdown_thread_lock:+ state = request.app.state+ if getattr(state, "_shutdown_trigger", None) is not trigger:+ state._shutdown_trigger = trigger+ state._shutdown_thread = threading.Thread(+ target = trigger, name = "studio-shutdown"+ )+ state._shutdown_thread.start()else:# Fallback when not launched via run_server() (e.g. direct uvicorn)import signaldiff --git a/studio/backend/tests/test_account_desktop_network.py b/studio/backend/tests/test_account_desktop_network.pyindex 2473ba56809..28f8f877e81 100644--- a/studio/backend/tests/test_account_desktop_network.py+++ b/studio/backend/tests/test_account_desktop_network.py@@ -394,6 +394,57 @@ def test_desktop_secret_authenticates_the_shell_health_probe(monkeypatch):assert refused.value.status_code == 401+def test_shutdown_cleanup_runs_off_the_event_loop_and_once(monkeypatch):+ # The cleanup waits seconds on subprocesses (llama-server: 5 s SIGTERM + 5 s SIGKILL). Run on the+ # loop it kept uvicorn from closing the port until it finished, so Shutdown left the port open past+ # a 15 s check on a loaded host. The loop must stay free meanwhile, and a second click is no-op.+ import main++ raw = storage.create_desktop_secret()+ add_managed()+ app = FastAPI()+ started, release, calls = [], __import__("threading").Event(), []++ def slow_cleanup():+ calls.append(time.monotonic())+ started.append(True)+ release.wait(10)++ app.state.trigger_shutdown = slow_cleanup+ app.add_api_route("/api/desktop/shutdown", main.desktop_shutdown_server, methods = ["POST"])+ app.add_api_route("/ping", lambda: {"ok": True}, methods = ["GET"])+ try:+ with TestClient(app) as http:+ headers = {"X-Desktop-Secret": raw}+ assert http.post("/api/desktop/shutdown", headers = headers).status_code == 200+ deadline = time.monotonic() + 5+ while not started and time.monotonic() < deadline:+ time.sleep(0.02)+ assert started+ t0 = time.monotonic()+ assert http.get("/ping").json() == {"ok": True}+ assert time.monotonic() - t0 < 1.0 # served while the cleanup is still running+ assert http.post("/api/desktop/shutdown", headers = headers).status_code == 200+ time.sleep(0.5)+ assert len(calls) == 1 # a second Shutdown joins the running cleanup+ finally:+ release.set()+ app.state._shutdown_thread.join(5)+ assert not app.state._shutdown_thread.is_alive()++ # an embedded host's next launch installs a new trigger: its Shutdown runs it, not the old marker+ second = []+ app.state.trigger_shutdown = lambda: second.append(True)+ with TestClient(app) as http:+ assert (+ http.post("/api/desktop/shutdown", headers = {"X-Desktop-Secret": raw}).status_code == 200+ )+ deadline = time.monotonic() + 5+ while not second and time.monotonic() < deadline:+ time.sleep(0.02)+ assert second and len(calls) == 1++def test_desktop_secret_stops_the_backend_it_owns(monkeypatch):import maindiff --git a/tests/studio/playwright_chat_ui.py b/tests/studio/playwright_chat_ui.pyindex 0debbfe7151..6ec7484f91d 100644--- a/tests/studio/playwright_chat_ui.py+++ b/tests/studio/playwright_chat_ui.py@@ -2812,6 +2812,33 @@ with sync_playwright() as p:except urllib.error.URLError as exc:info(f"OK /api/health unreachable: {exc!r}")+ # The port closes as soon as shutdown starts; the process stays up while it stops its children+ # (llama-server: up to 5 s SIGTERM + 5 s SIGKILL). When CI names the server (STUDIO_PID from+ # boot-studio-api-only.sh), the cleanup must also finish: a closed port alone no longer shows it.+ studio_pid = os.environ.get("STUDIO_PID", "").strip()+ if studio_pid.isdigit() and os.name != "nt": # Windows os.kill(pid, 0) would terminate it+ t0 = time.time()+ while time.time() - t0 < 60:+ try:+ os.kill(int(studio_pid), 0)+ if Path("/proc/self").exists(): # Linux: exited but not yet reaped is done+ try:+ stat = Path(f"/proc/{studio_pid}/stat").read_text()+ except FileNotFoundError: # reaped between the probe and the read+ raise ProcessLookupError from None+ if stat.rsplit(")", 1)[1].split()[0] == "Z":+ raise ProcessLookupError+ except ProcessLookupError:+ info(+ f"OK server process {studio_pid} exited {time.time() - t0:.1f}s after the port closed"+ )+ break+ except PermissionError:+ break # not ours to signal: nothing to check+ time.sleep(0.5)+ else:+ fail(f"server process {studio_pid} still running 60s after Shutdown closed its port")+# Some pageerrors are benign: chat-completions 422s (network-layer bubble-up, not a JS bug; per-turn flow already# validates each turn) and fetch failures after Shutdown (server is dead by design). Full list in# `_playwright_robust.BENIGN_PAGE_ERROR_PATTERNS`.