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: exit the inference worker instead of waiting to be killed (#16338)
Yakshith53624604ef83
diff --git a/studio/backend/core/inference/worker.py b/studio/backend/core/inference/worker.pyindex 91b2e4bf7e4..a5cd65fb2b1 100644--- a/studio/backend/core/inference/worker.py+++ b/studio/backend/core/inference/worker.py@@ -255,6 +255,26 @@ def _send_response(resp_queue: Any, response: dict) -> None:logger.error("Failed to send response: %s", exc)+def _exit_after_shutdown(resp_queue: Any) -> None:+ """Models are unloaded and acked; interpreter shutdown can stall 10s+ on Windows CUDA (#16330)."""+ try:+ resp_queue.close()+ resp_queue.join_thread()+ except Exception:+ pass+ for stream in (sys.stdout, sys.stderr):+ try:+ stream.flush()+ except Exception:+ pass+ try:+ from utils.worker_stderr import stop_worker_stderr_mirror+ stop_worker_stderr_mirror()+ except Exception:+ pass+ os._exit(0)++def _encode_share_object(obj: Any) -> bytes:data = json.dumps(obj, separators = (",", ":"), ensure_ascii = False).encode("utf-8")if len(data) > _SHARE_OBJECT_MAX_BYTES:@@ -2665,7 +2685,7 @@ def run_inference_process("type": "shutdown_ack",},)- return+ _exit_after_shutdown(resp_queue)else:logger.warning("Unknown command type: %s", cmd_type)diff --git a/studio/backend/tests/test_inference_worker_shutdown_exit.py b/studio/backend/tests/test_inference_worker_shutdown_exit.pynew file mode 100644index 00000000000..a84f53f4d10--- /dev/null+++ b/studio/backend/tests/test_inference_worker_shutdown_exit.py@@ -0,0 +1,86 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++from __future__ import annotations++import subprocess+import sys+from pathlib import Path++_BACKEND_ROOT = Path(__file__).resolve().parents[1]+if str(_BACKEND_ROOT) not in sys.path:+ sys.path.insert(0, str(_BACKEND_ROOT))++from studio.backend.core.inference import worker # noqa: E402+from utils.worker_stderr import WorkerStderrCapture # noqa: E402+++class _Queue:+ def __init__(self, raises: bool = False):+ self.raises = raises+ self.closed = False+ self.joined = False++ def close(self):+ self.closed = True+ if self.raises:+ raise OSError("already closed")++ def join_thread(self):+ self.joined = True+++def _capture_exit(monkeypatch):+ codes: list[int] = []+ monkeypatch.setattr(worker.os, "_exit", lambda code: codes.append(code))+ return codes+++def test_shutdown_exits_the_process_after_flushing(monkeypatch):+ codes = _capture_exit(monkeypatch)+ queue = _Queue()++ worker._exit_after_shutdown(queue)++ assert codes == [0]+ assert queue.closed and queue.joined+++def test_shutdown_still_exits_when_the_queue_is_already_closed(monkeypatch):+ codes = _capture_exit(monkeypatch)++ worker._exit_after_shutdown(_Queue(raises = True))++ assert codes == [0]+++_MIRRORED_CHILD = r"""+import sys+sys.path.insert(0, %(backend)r)+from core.inference import worker+from utils.worker_stderr import install_worker_stderr_mirror++class Q:+ def close(self): pass+ def join_thread(self): pass++assert install_worker_stderr_mirror(%(path)r) is True+for i in range(200):+ sys.stderr.write(f"bulk {i}\n")+sys.stderr.write("the last line before shutdown\n")+worker._exit_after_shutdown(Q())+"""+++def test_shutdown_exit_still_relays_the_mirrored_stderr_tail(tmp_path):+ # os._exit skips the atexit drain, which dropped every line the mirror pump had not relayed yet.+ capture = WorkerStderrCapture(directory = str(tmp_path), prefix = "unsloth-test-")+ script = _MIRRORED_CHILD % {"backend": str(_BACKEND_ROOT), "path": capture.path}+ result = subprocess.run(+ [sys.executable, "-c", script], stderr = subprocess.PIPE, text = True, timeout = 120+ )+ capture.close()++ assert result.returncode == 0, result.stderr+ assert "the last line before shutdown" in result.stderr+ assert result.stderr.count("bulk ") == 200diff --git a/studio/backend/utils/worker_stderr.py b/studio/backend/utils/worker_stderr.pyindex f35878a17c5..999c601707e 100644--- a/studio/backend/utils/worker_stderr.py+++ b/studio/backend/utils/worker_stderr.py@@ -7,6 +7,7 @@ from __future__ import annotationsimport atexitimport collections+import functoolsimport osimport reimport sys@@ -25,6 +26,7 @@ __all__ = ["install_worker_stderr_mirror","mark_log_record_continuations","stderr_tail_from_bytes",+ "stop_worker_stderr_mirror","unexpected_exit_message",]@@ -583,6 +585,9 @@ def _stop_mirror(pass+_MIRROR_STOPPER = None++def install_worker_stderr_mirror(path: "str | None", cap_bytes: int = MIRROR_FILE_CAP_BYTES) -> bool:@@ -636,10 +641,21 @@ def install_worker_stderr_mirror(daemon = True,)pump.start()- atexit.register(_stop_mirror, inherited, pump, stop, relay, rebound)+ global _MIRROR_STOPPER+ _MIRROR_STOPPER = functools.partial(_stop_mirror, inherited, pump, stop, relay, rebound)+ atexit.register(_MIRROR_STOPPER)return True+def stop_worker_stderr_mirror() -> None:+ """The atexit drain, for a worker leaving through os._exit."""+ global _MIRROR_STOPPER+ stopper, _MIRROR_STOPPER = _MIRROR_STOPPER, None+ if stopper is not None:+ atexit.unregister(stopper)+ stopper()++_PREFIX_FORMATTER_CLASS = None# Marked on the formatter, not via a class: a first-call race builds two classes and double-wraps._MARKS_CONTINUATIONS = "_unsloth_marks_continuations"