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
Graceful shutdown on Windows (signal handlers for Ctrl+C) (#4306)
Roland Tannous0818f786173c
diff --git a/cli/commands/studio.py b/cli/commands/studio.pyindex 60a0a4d0ff1..0f1c021873c 100644--- a/cli/commands/studio.py+++ b/cli/commands/studio.py@@ -106,7 +106,21 @@ def studio_default(args.extend(["--frontend", str(frontend)])if silent:args.append("--silent")- os.execvp(str(studio_python), args)+ # On Windows, os.execvp() spawns a child but the parent lingers,+ # so Ctrl+C only kills the parent leaving the child orphaned.+ # Use subprocess.run() on Windows so the parent waits for the child.+ if sys.platform == "win32":+ import subprocess as _sp++ proc = _sp.Popen(args)+ try:+ rc = proc.wait()+ except KeyboardInterrupt:+ # Child has its own signal handler — let it finish+ rc = proc.wait()+ raise typer.Exit(rc)+ else:+ os.execvp(str(studio_python), args)else:typer.echo("Studio not set up. Run 'unsloth studio setup' first.")raise typer.Exit(1)@@ -126,10 +140,18 @@ def studio_default(silent = silent,)+ from studio.backend.run import _shutdown_event+try:- while True:- time.sleep(1)+ if _shutdown_event is not None:+ _shutdown_event.wait()+ else:+ while True:+ time.sleep(1)except KeyboardInterrupt:+ from studio.backend.run import _graceful_shutdown, _server++ _graceful_shutdown(_server)typer.echo("\nShutting down...")diff --git a/cli/commands/ui.py b/cli/commands/ui.pyindex 10db9e243e1..79a76e629b8 100644--- a/cli/commands/ui.py+++ b/cli/commands/ui.py@@ -49,7 +49,21 @@ def ui(args.extend(["--frontend", str(frontend)])if silent:args.append("--silent")- os.execvp(str(studio_python), args)+ # On Windows, os.execvp() spawns a child but the parent lingers,+ # so Ctrl+C only kills the parent leaving the child orphaned.+ # Use subprocess.run() on Windows so the parent waits for the child.+ if sys.platform == "win32":+ import subprocess as _sp++ proc = _sp.Popen(args)+ try:+ rc = proc.wait()+ except KeyboardInterrupt:+ # Child has its own signal handler — let it finish+ rc = proc.wait()+ raise typer.Exit(rc)+ else:+ os.execvp(str(studio_python), args)else:typer.echo("Studio not set up. Run 'unsloth studio setup' first.")raise typer.Exit(1)@@ -69,8 +83,16 @@ def ui(silent = silent,)+ from studio.backend.run import _shutdown_event+try:- while True:- time.sleep(1)+ if _shutdown_event is not None:+ _shutdown_event.wait()+ else:+ while True:+ time.sleep(1)except KeyboardInterrupt:+ from studio.backend.run import _graceful_shutdown, _server++ _graceful_shutdown(_server)typer.echo("\nShutting down...")diff --git a/studio/backend/run.py b/studio/backend/run.pyindex 4924406d2eb..3e2bdb5a9a0 100644--- a/studio/backend/run.py+++ b/studio/backend/run.py@@ -92,6 +92,66 @@ def _find_free_port(host: str, start: int, max_attempts: int = 20) -> int:)+def _graceful_shutdown(server = None):+ """Explicitly shut down all subprocess backends and the uvicorn server.++ Called from signal handlers to ensure child processes are cleaned up+ before the parent exits. This is critical on Windows where atexit+ handlers are unreliable after Ctrl+C.+ """+ logger.info("Graceful shutdown initiated — cleaning up subprocesses...")++ # 1. Shut down uvicorn server (releases the listening socket)+ if server is not None:+ server.should_exit = True++ # 2. Clean up inference subprocess (if instantiated)+ try:+ from core.inference.orchestrator import _inference_backend++ if _inference_backend is not None:+ _inference_backend._shutdown_subprocess(timeout = 5.0)+ except Exception as e:+ logger.warning("Error shutting down inference subprocess: %s", e)++ # 3. Clean up export subprocess (if instantiated)+ try:+ from core.export.orchestrator import _export_backend++ if _export_backend is not None:+ _export_backend._shutdown_subprocess(timeout = 5.0)+ except Exception as e:+ logger.warning("Error shutting down export subprocess: %s", e)++ # 4. Clean up training subprocess (if active)+ try:+ from core.training.training import _training_backend++ if _training_backend is not None:+ _training_backend.force_terminate()+ except Exception as e:+ logger.warning("Error shutting down training subprocess: %s", e)++ # 5. Kill llama-server subprocess (if loaded)+ try:+ from routes.inference import _llama_cpp_backend++ if _llama_cpp_backend is not None:+ _llama_cpp_backend._kill_process()+ except Exception as e:+ logger.warning("Error shutting down llama-server: %s", e)++ logger.info("All subprocesses cleaned up")+++# The uvicorn server instance — set by run_server(), used by callers+# that need to tell the server to exit (e.g. signal handlers).+_server = None++# Shutdown event — used to wake the main loop on signal+_shutdown_event = None++def run_server(host: str = "0.0.0.0",port: int = 8000,@@ -106,13 +166,20 @@ def run_server(port: Port to bind to (auto-increments if in use)frontend_path: Path to frontend build directory (optional)silent: Suppress startup messages++ Note:+ Signal handlers are NOT registered here so that embedders+ (e.g. Colab notebooks) keep their own interrupt semantics.+ Standalone callers should register handlers after calling this."""+ global _server, _shutdown_event+import nest_asyncionest_asyncio.apply()import asyncio- from threading import Thread+ from threading import Thread, Eventimport timeimport uvicorn@@ -138,13 +205,16 @@ def run_server(if not silent:print(f"⚠️ Frontend not found at {frontend_path}")- # Run server+ # Create the uvicorn server and expose it for signal handlers+ config = uvicorn.Config(+ app, host = host, port = port, log_level = "info", access_log = False+ )+ _server = uvicorn.Server(config)+ _shutdown_event = Event()++ # Run server in a daemon threaddef _run():- config = uvicorn.Config(- app, host = host, port = port, log_level = "info", access_log = False- )- server = uvicorn.Server(config)- asyncio.run(server.serve())+ asyncio.run(_server.serve())thread = Thread(target = _run, daemon = True)thread.start()@@ -165,9 +235,10 @@ def run_server(return app-# For direct execution+# For direct execution (also invoked by CLI via os.execvp / subprocess)if __name__ == "__main__":import argparse+ import signalparser = argparse.ArgumentParser(description = "Run Unsloth UI Backend server")parser.add_argument("--host", default = "0.0.0.0", help = "Host to bind to")@@ -187,8 +258,17 @@ if __name__ == "__main__":kwargs["frontend_path"] = Path(args.frontend)run_server(**kwargs)- # Keep running- import time+ # ── Signal handler — ensures subprocess cleanup on Ctrl+C ────+ def _signal_handler(signum, frame):+ _graceful_shutdown(_server)+ _shutdown_event.set()++ signal.signal(signal.SIGINT, _signal_handler)+ signal.signal(signal.SIGTERM, _signal_handler)++ # On Windows, some terminals send SIGBREAK for Ctrl+C / Ctrl+Break+ if hasattr(signal, "SIGBREAK"):+ signal.signal(signal.SIGBREAK, _signal_handler)- while True:- time.sleep(1)+ # Keep running until shutdown signal+ _shutdown_event.wait()