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
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
"""Ends stopWhenUnattended runs once no follower for _GRACE_S; attendance, not a close request, counts"""
from __future__ import annotations
import asyncio
import math
import os
import threading
import time
from dataclasses import dataclass
from typing import Awaitable, Callable
from loggers import get_logger
from state import run_subscribers
logger = get_logger(__name__)
# Outlasts a reload and a follower's reconnect backoff, yet short enough not to pin the model.
_GRACE_DEFAULT_S = 30.0
_CHECK_INTERVAL_S = 5.0
def _grace_from_env() -> float:
"""Read the grace once, at import. A value nobody can parse falls back rather than raising on the chat path."""
raw = os.environ.get("UNSLOTH_STUDIO_UNATTENDED_RUN_GRACE_S")
if raw is None or not raw.strip():
return _GRACE_DEFAULT_S
try:
value = float(raw)
except ValueError:
return _GRACE_DEFAULT_S
if not math.isfinite(value) or value < 0:
return _GRACE_DEFAULT_S
return value
_GRACE_S = _grace_from_env()
@dataclass
class _Watch:
attendance_id: str
account_id: str
# Blocking (it reads the run's database), so it is called off the loop.
settled: Callable[[], bool]
# Runs on the serving loop: a supervisor's cancel can touch its asyncio tasks.
cancel: Callable[[], Awaitable[None]]
unattended_since: float
_LOCK = threading.Lock()
# (account_id, attendance_id) -> watch
_WATCHES: dict[tuple[str, str], _Watch] = {}
_task: asyncio.Task | None = None
def _key(account_id: str, attendance_id: str) -> tuple[str, str]:
return (account_id or "", attendance_id)
def watch(
attendance_id: str,
account_id: str = "",
*,
settled: Callable[[], bool],
cancel: Callable[[], Awaitable[None]],
replace: bool = False,
) -> None:
"""Stops attendance_id once it has gone _GRACE_S with no follower; re-arming keeps an existing
grace; replace gives a new attempt its own grace; thread-safe"""
if not attendance_id:
return
key = _key(account_id, attendance_id)
with _LOCK:
if key in _WATCHES and not replace:
return
_WATCHES[key] = _Watch(
attendance_id = attendance_id,
account_id = account_id or "",
settled = settled,
cancel = cancel,
unattended_since = time.monotonic(),
)
def unwatch(attendance_id: str, account_id: str = "") -> None:
"""Let ``attendance_id`` run on unattended, e.g. a run id rebound to an attempt that keeps running."""
with _LOCK:
_WATCHES.pop(_key(account_id, attendance_id), None)
def _drop(key: tuple[str, str], entry: _Watch) -> None:
with _LOCK:
# Only this entry: a run re-armed after it settled holds a newer one under the same key.
if _WATCHES.get(key) is entry:
del _WATCHES[key]
async def check_once(now: float | None = None) -> list[str]:
"""One pass over the watched runs. Returns the attendance ids it stopped."""
now = time.monotonic() if now is None else now
with _LOCK:
entries = list(_WATCHES.items())
stopped: list[str] = []
for key, entry in entries:
try:
if await asyncio.to_thread(entry.settled):
_drop(key, entry)
continue
except Exception as exc:
# A locked database or a deleted account must not end the watch for good.
logger.warning("Unattended run check failed for %s: %s", entry.attendance_id, exc)
continue
if run_subscribers.is_attended(entry.attendance_id, entry.account_id):
entry.unattended_since = now
continue
if now - entry.unattended_since < _GRACE_S:
continue
try:
await entry.cancel()
except Exception as exc:
# Kept, so the next pass tries again: a lost writer-lock race must not let the run go on for good.
logger.warning("Stopping unattended run %s failed: %s", entry.attendance_id, exc)
continue
_drop(key, entry)
logger.info(
"Stopped run %s: no tab has watched it for %.0fs", entry.attendance_id, _GRACE_S
)
stopped.append(entry.attendance_id)
return stopped
async def _run() -> None:
while True:
await asyncio.sleep(_CHECK_INTERVAL_S)
try:
await check_once()
except asyncio.CancelledError:
raise
except Exception as exc:
logger.warning("Unattended run watchdog pass failed: %s", exc)
def start() -> None:
"""Start the watchdog on the running loop. Idempotent within one lifespan."""
global _task
if _task is not None and not _task.done():
return
_task = asyncio.create_task(_run(), name = "unattended-run-watchdog")
async def stop() -> None:
global _task
task, _task = _task, None
if task is None or task.done():
return
task.cancel()
try:
await task
except asyncio.CancelledError:
pass
def watched_for_tests(attendance_id: str, account_id: str = "") -> bool:
with _LOCK:
return _key(account_id, attendance_id) in _WATCHES
def reset_for_tests() -> None:
with _LOCK:
_WATCHES.clear()