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.

32.9 KB
Raw Blame
"""Cancel tracker must register before StreamingResponse returns, or a Stop leaves a zombie decode."""

from __future__ import annotations

import ast
import asyncio
import importlib.util
import sys
import json
import threading
import time
from pathlib import Path


SOURCE_PATH = Path(__file__).resolve().parents[2] / "studio" / "backend" / "routes" / "inference.py"
SRC = SOURCE_PATH.read_text(encoding = "utf-8")
_TREE = ast.parse(SRC)


def _collect_async_functions(tree: ast.AST):
    return [n for n in ast.walk(tree) if isinstance(n, ast.AsyncFunctionDef)]


def _has_tracker_enter_call(node: ast.AST) -> bool:
    for sub in ast.walk(node):
        if not isinstance(sub, ast.Call):
            continue
        fn = sub.func
        if (
            isinstance(fn, ast.Attribute)
            and fn.attr == "__enter__"
            and isinstance(fn.value, ast.Name)
            and fn.value.id.startswith("_tracker")
        ):
            return True
    return False


def _finalbody_has_tracker_exit(finalbody) -> bool:
    for stmt in finalbody:
        if not isinstance(stmt, ast.Expr):
            continue
        call = stmt.value
        if not (isinstance(call, ast.Call) and isinstance(call.func, ast.Attribute)):
            continue
        fn = call.func
        if (
            fn.attr == "__exit__"
            and isinstance(fn.value, ast.Name)
            and fn.value.id.startswith("_tracker")
        ):
            return True
    return False


def _async_function(name: str) -> ast.AsyncFunctionDef:
    for node in ast.walk(_TREE):
        if isinstance(node, ast.AsyncFunctionDef) and node.name == name:
            return node
    raise AssertionError(f"{name} handler missing")


def _calls_name(node: ast.AST, name: str) -> bool:
    for sub in ast.walk(node):
        if isinstance(sub, ast.Call) and isinstance(sub.func, ast.Name):
            if sub.func.id == name:
                return True
    return False


def test_no_tracker_enter_inside_async_generators():
    offenders = []
    for fn in _collect_async_functions(_TREE):
        if fn.name in {
            "gguf_tool_stream",
            "gguf_stream_chunks",
            "stream_chunks",
            "audio_input_stream",
        }:
            if _has_tracker_enter_call(fn):
                offenders.append(fn.name)
    assert not offenders, (
        f"Cancel tracker registration must live OUTSIDE the async generator "
        f"body so a stop POST can find the registry entry before the first "
        f"SSE chunk. Offending generators: {offenders}"
    )


def test_tracker_enter_exists_in_sync_body_of_chat_completions():
    top = None
    for n in ast.walk(_TREE):
        if isinstance(n, ast.AsyncFunctionDef) and n.name == "produce_openai_chat_completions":
            top = n
            break
    assert top is not None, "produce_openai_chat_completions handler missing"
    count = 0
    for sub in ast.walk(top):
        if not isinstance(sub, ast.Call):
            continue
        fn = sub.func
        if (
            isinstance(fn, ast.Attribute)
            and fn.attr == "__enter__"
            and isinstance(fn.value, ast.Name)
            and fn.value.id.startswith("_tracker")
        ):
            count += 1
    assert count >= 3, (
        f"expected >=3 _tracker.__enter__() calls in produce_openai_chat_completions "
        f"(one per streaming path), got {count}"
    )


def test_async_generators_cleanup_tracker_in_finally():
    required = {
        "gguf_tool_stream",
        "gguf_stream_chunks",
        "stream_chunks",
        "audio_input_stream",
    }
    found: set[str] = set()
    for fn in [n for n in ast.walk(_TREE) if isinstance(n, ast.AsyncFunctionDef)]:
        if fn.name not in required:
            continue
        for sub in ast.walk(fn):
            if isinstance(sub, ast.Try) and sub.finalbody:
                if _finalbody_has_tracker_exit(sub.finalbody):
                    found.add(fn.name)
                    break
    missing = required - found
    assert not missing, (
        f"Cleanup must run via `finally: _tracker.__exit__(None, None, None)` "
        f"inside each streaming generator so ClientDisconnect / OSError paths "
        f"also release registry entries (Starlette skips `background` callbacks "
        f"when stream_response raises). Missing in: {sorted(missing)}"
    )


_SAME_TASK_BUILDERS = ("_SameTaskStreamingResponse", "_sse_streaming_response")


def test_sse_streaming_response_builds_a_same_task_response():
    fn = next(
        n
        for n in ast.walk(_TREE)
        if isinstance(n, ast.FunctionDef) and n.name == "_sse_streaming_response"
    )
    names = {
        sub.func.id
        for sub in ast.walk(fn)
        if isinstance(sub, ast.Call) and isinstance(sub.func, ast.Name)
    }
    assert "_SameTaskStreamingResponse" in names
    assert "StreamingResponse" not in names


def test_chat_completions_streams_avoid_starlette_task_group():
    top = _async_function("produce_openai_chat_completions")
    legacy_calls = []
    same_task_calls = 0
    for sub in ast.walk(top):
        if not (isinstance(sub, ast.Call) and isinstance(sub.func, ast.Name)):
            continue
        if sub.func.id == "StreamingResponse":
            legacy_calls.append(sub.lineno)
        if sub.func.id in _SAME_TASK_BUILDERS:
            same_task_calls += 1
    assert not legacy_calls, (
        "Streaming /v1/chat/completions must use _SameTaskStreamingResponse, "
        "not Starlette's legacy task-group StreamingResponse. Lines: "
        f"{legacy_calls}"
    )
    assert same_task_calls >= 5


def test_openai_passthrough_stream_avoids_starlette_task_group():
    functions = [
        _async_function("_openai_passthrough_stream"),
        _async_function("_openai_passthrough_stream_admitted"),
    ]
    legacy_calls = []
    same_task_calls = 0
    for fn in functions:
        for sub in ast.walk(fn):
            if not (isinstance(sub, ast.Call) and isinstance(sub.func, ast.Name)):
                continue
            if sub.func.id == "StreamingResponse":
                legacy_calls.append(sub.lineno)
            if sub.func.id in _SAME_TASK_BUILDERS:
                same_task_calls += 1
    assert not legacy_calls, (
        "OpenAI passthrough streams must use _SameTaskStreamingResponse, "
        "not Starlette's legacy task-group StreamingResponse. Lines: "
        f"{legacy_calls}"
    )
    assert same_task_calls >= 2


def test_local_chat_streams_install_same_task_disconnect_watcher():
    top = _async_function("produce_openai_chat_completions")
    assert _calls_name(top, "_await_disconnect_then_cancel"), (
        "Local same-task streams must watch request disconnects themselves; "
        "do not restore Starlette's task-group StreamingResponse for this."
    )


def test_direct_llama_server_streams_install_disconnect_watcher():
    required = {
        "openai_completions",
        "_responses_stream",
        "_anthropic_passthrough_stream",
        "_openai_passthrough_stream_admitted",
    }
    missing = [
        name
        for name in sorted(required)
        if not _calls_name(_async_function(name), "_await_disconnect_then_close")
    ]
    assert not missing, (
        "Direct httpx streams to llama-server must close the upstream response "
        "when the downstream client disconnects during prefill. Missing in: "
        f"{missing}"
    )


def test_audio_input_stream_installs_disconnect_watcher():
    audio = _async_function("audio_input_stream")
    has_watcher = False
    has_cleanup = False
    for sub in ast.walk(audio):
        if isinstance(sub, ast.Call):
            fn = sub.func
            if (
                isinstance(fn, ast.Attribute)
                and fn.attr == "create_task"
                and isinstance(fn.value, ast.Name)
                and fn.value.id == "asyncio"
                and sub.args
                and isinstance(sub.args[0], ast.Call)
                and isinstance(sub.args[0].func, ast.Name)
                and sub.args[0].func.id == "_await_disconnect_then_cancel"
            ):
                has_watcher = True
            if isinstance(fn, ast.Name) and fn.id == "_stop_local_disconnect_cancel_watcher":
                has_cleanup = True
    assert has_watcher, (
        "audio_input_stream must install a disconnect watcher so client "
        "disconnects set cancel_event while asyncio.to_thread(next, ...) is blocked"
    )
    assert has_cleanup, "audio_input_stream must stop its disconnect watcher in finally"


_WANTED = {
    "_CANCEL_REGISTRY",
    "_CANCEL_LOCK",
    "_PENDING_CANCELS",
    "_PENDING_CANCEL_TTL_S",
    "_prune_pending",
    "_TrackedCancel",
    "_slot_lock",
    "_cancel_by_keys",
    "_cancel_by_cancel_id_or_stash",
}


def _load_active_generations():
    """Loads the real registry from disk, not via import, to avoid pulling in the route package."""
    path = SOURCE_PATH.parents[1] / "state" / "active_generations.py"
    spec = importlib.util.spec_from_file_location("studio_active_generations", path)
    module = importlib.util.module_from_spec(spec)
    backend = str(SOURCE_PATH.parents[1])
    sys.path.insert(0, backend)
    try:
        spec.loader.exec_module(module)
    finally:
        sys.path.remove(backend)
    return module


_REGISTRY_SOURCE = None


def _registry_source():
    """Built once per process: only the kept top-level nodes are cut, since get_source_segment is slow."""
    global _REGISTRY_SOURCE
    if _REGISTRY_SOURCE is not None:
        return _REGISTRY_SOURCE
    chunks = []
    for n in _TREE.body:
        if isinstance(n, (ast.FunctionDef, ast.ClassDef)):
            wanted = n.name in _WANTED
        elif isinstance(n, ast.Assign):
            wanted = any(t.id in _WANTED for t in n.targets if isinstance(t, ast.Name))
        elif isinstance(n, ast.AnnAssign):
            wanted = isinstance(n.target, ast.Name) and n.target.id in _WANTED
        else:
            wanted = False
        if not wanted:
            continue
        seg = ast.get_source_segment(SRC, n)
        if seg is None:
            continue
        chunks.append(seg)
    _REGISTRY_SOURCE = "\n\n".join(chunks)
    return _REGISTRY_SOURCE


def _load_registry_module():
    mod = {"active_generations": _load_active_generations()}
    exec(
        'import contextvars\nrouted_slot = contextvars.ContextVar("routed_slot", default = None)\n'
        "import threading, time\n_account_cancel_key = lambda key: key\n" + _registry_source(),
        mod,
    )
    return mod


def _load_same_task_response_module():
    for n in _TREE.body:
        if isinstance(n, ast.ClassDef) and n.name == "_SameTaskStreamingResponse":
            source = ast.get_source_segment(SRC, n)
            break
    else:
        raise AssertionError("_SameTaskStreamingResponse missing")
    mod = {}
    exec(
        "class StreamingResponse: pass\nclass ClientDisconnect(Exception): pass\n" + source,
        mod,
    )
    return mod


def _make_stream(tracker, raise_exc):
    async def gen():
        try:
            try:
                yield "data: first\n\n"
                if raise_exc is not None:
                    raise raise_exc
                yield "data: [DONE]\n\n"
            except asyncio.CancelledError:
                raise
            except Exception:
                yield "data: error\n\n"
            finally:
                tracker.__exit__(None, None, None)
        except BaseException:
            raise

    return gen()


async def _consume(agen):
    out = []
    try:
        async for ch in agen:
            out.append(ch)
    except BaseException as e:
        out.append(type(e).__name__)
    return out


def _llama_stub_raises_on_preset_cancel(cancel_event):
    if cancel_event.is_set():
        raise GeneratorExit
    yield "cumulative-1"
    yield "cumulative-2"


async def _post_fix_gguf_loop(cancel_event):
    yield "first_chunk"
    gen = _llama_stub_raises_on_preset_cancel(cancel_event)
    sentinel = object()
    while True:
        if cancel_event.is_set():
            break
        cumulative = await asyncio.to_thread(next, gen, sentinel)
        if cumulative is sentinel:
            break
        yield cumulative
    yield "final_chunk"
    yield "[DONE]"


def test_finally_cleanup_on_normal_completion():
    m = _load_registry_module()
    m["_CANCEL_REGISTRY"].clear()
    ev = threading.Event()
    tr = m["_TrackedCancel"](ev, "cid-ok", "sid-ok")
    tr.__enter__()
    assert "cid-ok" in m["_CANCEL_REGISTRY"]
    chunks = asyncio.run(_consume(_make_stream(tr, None)))
    assert chunks == ["data: first\n\n", "data: [DONE]\n\n"]
    assert "cid-ok" not in m["_CANCEL_REGISTRY"]
    assert "sid-ok" not in m["_CANCEL_REGISTRY"]


def test_finally_cleanup_on_mid_stream_exception():
    m = _load_registry_module()
    m["_CANCEL_REGISTRY"].clear()
    ev = threading.Event()
    tr = m["_TrackedCancel"](ev, "cid-err", "sid-err")
    tr.__enter__()
    assert "cid-err" in m["_CANCEL_REGISTRY"]
    asyncio.run(_consume(_make_stream(tr, OSError("disconnect"))))
    assert "cid-err" not in m["_CANCEL_REGISTRY"]
    assert "sid-err" not in m["_CANCEL_REGISTRY"]


def test_finally_cleanup_on_aclose():
    m = _load_registry_module()
    m["_CANCEL_REGISTRY"].clear()
    ev = threading.Event()
    tr = m["_TrackedCancel"](ev, "cid-abort", "sid-abort")
    tr.__enter__()
    assert "cid-abort" in m["_CANCEL_REGISTRY"]

    async def run():
        gen = _make_stream(tr, None)
        it = gen.__aiter__()
        await it.__anext__()
        await gen.aclose()

    asyncio.run(run())
    assert "cid-abort" not in m["_CANCEL_REGISTRY"]
    assert "sid-abort" not in m["_CANCEL_REGISTRY"]


def test_same_task_response_closes_body_iterator_on_send_disconnect():
    m = _load_same_task_response_module()
    closed = False

    async def body():
        nonlocal closed
        try:
            yield "data: first\n\n"
        finally:
            closed = True

    async def run():
        agen = body()
        await agen.__anext__()
        response = m["_SameTaskStreamingResponse"].__new__(m["_SameTaskStreamingResponse"])
        response.body_iterator = agen
        response.background = None
        # __new__ bypasses __init__, but the disconnect branch reads _unstarted_cleanup.
        response._unstarted_cleanup = None

        async def stream_response(_send):
            raise OSError("client disconnected")

        response.stream_response = stream_response
        try:
            await response({}, None, lambda _message: None)
        except m["ClientDisconnect"]:
            pass
        else:
            raise AssertionError("expected ClientDisconnect")

    asyncio.run(run())
    assert closed


def test_preset_cancel_event_exits_cleanly_with_done():
    ev = threading.Event()
    ev.set()
    chunks = asyncio.run(_consume(_post_fix_gguf_loop(ev)))
    assert "first_chunk" in chunks
    assert "final_chunk" in chunks
    assert "[DONE]" in chunks
    assert "GeneratorExit" not in chunks
    assert "cumulative-1" not in chunks
    assert "cumulative-2" not in chunks


def test_normal_path_streams_all_tokens():
    ev = threading.Event()
    chunks = asyncio.run(_consume(_post_fix_gguf_loop(ev)))
    assert chunks == ["first_chunk", "cumulative-1", "cumulative-2", "final_chunk", "[DONE]"]


def test_cancel_during_streaming_stops_iteration_promptly():
    ev = threading.Event()

    async def _run():
        gen = _post_fix_gguf_loop(ev)
        seen = []
        async for ch in gen:
            seen.append(ch)
            if ch == "cumulative-1":
                ev.set()
        return seen

    seen = asyncio.run(_run())
    assert "first_chunk" in seen
    assert "cumulative-1" in seen
    assert "cumulative-2" not in seen
    assert "final_chunk" in seen
    assert "[DONE]" in seen


def _loop_has_cancel_event_check(fn) -> bool:
    # Colab-style proxies drop request.is_disconnected(), so only cancel_event can interrupt.
    for sub in ast.walk(fn):
        if not isinstance(sub, (ast.While, ast.For, ast.AsyncFor)):
            continue
        for stmt in ast.walk(sub):
            if not isinstance(stmt, ast.If):
                continue
            t = stmt.test
            if (
                isinstance(t, ast.Call)
                and isinstance(t.func, ast.Attribute)
                and t.func.attr == "is_set"
                and isinstance(t.func.value, ast.Name)
                and t.func.value.id == "cancel_event"
            ):
                return True
    return False


def test_streaming_generators_check_cancel_event_in_loop():
    required = {
        "gguf_tool_stream",
        "gguf_stream_chunks",
        "stream_chunks",
        "audio_input_stream",
    }
    missing = []
    for fn in [n for n in ast.walk(_TREE) if isinstance(n, ast.AsyncFunctionDef)]:
        if fn.name not in required:
            continue
        if not _loop_has_cancel_event_check(fn):
            missing.append(fn.name)
    assert not missing, (
        f"Each streaming generator must check `cancel_event.is_set()` inside "
        f"its main loop so `POST /api/inference/cancel` can interrupt the "
        f"stream through proxies that do not forward fetch aborts. "
        f"Missing in: {sorted(missing)}"
    )


def _steps_generator_off_loop(fn) -> bool:
    for sub in ast.walk(fn):
        if not isinstance(sub, ast.Call):
            continue
        f = sub.func
        if isinstance(f, ast.Name) and f.id == "ThreadStepper":
            return True
        if (
            isinstance(f, ast.Attribute)
            and f.attr == "to_thread"
            and isinstance(f.value, ast.Name)
            and f.value.id == "asyncio"
            and sub.args
            and isinstance(sub.args[0], ast.Name)
            and sub.args[0].id == "next"
        ):
            return True
    return False


def test_audio_input_stream_offloads_blocking_next_to_thread():
    audio = None
    for fn in ast.walk(_TREE):
        if isinstance(fn, ast.AsyncFunctionDef) and fn.name == "audio_input_stream":
            audio = fn
            break
    assert audio is not None, "audio_input_stream generator missing"

    for sub in ast.walk(audio):
        if isinstance(sub, (ast.For, ast.AsyncFor)):
            it_src = ast.unparse(sub.iter)
            assert "audio_input_generate" not in it_src, (
                "audio_input_stream must not iterate audio_input_generate() "
                "directly -- that blocks the event loop. Step it with "
                "`ThreadStepper(gen).next` inside a `while True` loop instead"
            )

    assert _steps_generator_off_loop(audio), (
        "audio_input_stream must step its generator with `ThreadStepper(gen).next` "
        "or `asyncio.to_thread(next, gen, ...)` to keep the event loop free while "
        "whisper yields the next chunk"
    )


def test_generate_stream_offloads_blocking_next_to_thread():
    outer = None
    for fn in ast.walk(_TREE):
        if isinstance(fn, ast.AsyncFunctionDef) and fn.name == "generate_stream":
            outer = fn
            break
    assert outer is not None, "generate_stream handler missing"

    inner = None
    for sub in ast.walk(outer):
        if isinstance(sub, ast.AsyncFunctionDef) and sub.name == "stream":
            inner = sub
            break
    assert inner is not None, "generate_stream inner stream() generator missing"

    for sub in ast.walk(inner):
        if isinstance(sub, (ast.For, ast.AsyncFor)):
            it_src = ast.unparse(sub.iter)
            assert "generate_chat_response" not in it_src, (
                "generate_stream's inner stream() must not iterate "
                "backend.generate_chat_response() directly -- that blocks the event "
                "loop on every blocking subprocess read between tokens. Step it with "
                "`ThreadStepper(gen).next` inside a `while True` loop instead"
            )

    assert _steps_generator_off_loop(inner), (
        "generate_stream's inner stream() must step its generator with "
        "`ThreadStepper(gen).next` or `asyncio.to_thread(next, gen, _DONE)` to keep "
        "the event loop free while the worker subprocess produces the next token"
    )


def test_generate_stream_cancels_backend_on_stream_cancelled_error():
    outer = None
    for fn in ast.walk(_TREE):
        if isinstance(fn, ast.AsyncFunctionDef) and fn.name == "generate_stream":
            outer = fn
            break
    assert outer is not None, "generate_stream handler missing"

    outer_src = ast.unparse(outer)
    assert "cancel_event = threading.Event()" in outer_src

    inner = None
    for sub in ast.walk(outer):
        if isinstance(sub, ast.AsyncFunctionDef) and sub.name == "stream":
            inner = sub
            break
    assert inner is not None, "generate_stream inner stream() generator missing"

    def _awaits_to_thread_gen_close(node: ast.AST) -> bool:
        for sub in ast.walk(node):
            if not isinstance(sub, ast.Await):
                continue
            call = sub.value
            if not isinstance(call, ast.Call):
                continue
            fn_expr = call.func
            if not (
                isinstance(fn_expr, ast.Attribute)
                and fn_expr.attr == "to_thread"
                and isinstance(fn_expr.value, ast.Name)
                and fn_expr.value.id == "asyncio"
            ):
                continue
            if not call.args:
                continue
            close_expr = call.args[0]
            if (
                isinstance(close_expr, ast.Attribute)
                and close_expr.attr == "close"
                and isinstance(close_expr.value, ast.Name)
                and close_expr.value.id == "gen"
            ):
                return True
        return False

    found_cancel_kwarg = False
    found_cancel_handler = False
    found_finally_cleanup = False
    for sub in ast.walk(inner):
        if isinstance(sub, ast.Call):
            call_src = ast.unparse(sub.func)
            if call_src.endswith("generate_chat_response"):
                found_cancel_kwarg = any(
                    kw.arg == "cancel_event"
                    and isinstance(kw.value, ast.Name)
                    and kw.value.id == "cancel_event"
                    for kw in sub.keywords
                )
        if isinstance(sub, ast.ExceptHandler):
            exc_src = ast.unparse(sub.type) if sub.type is not None else ""
            if exc_src != "asyncio.CancelledError":
                continue
            body_src = "\n".join(ast.unparse(stmt) for stmt in sub.body)
            found_cancel_handler = (
                "cancel_event.set()" in body_src
                and "backend.reset_generation_state(cancel_event)" in body_src
                and any(isinstance(stmt, ast.Raise) and stmt.exc is None for stmt in sub.body)
            )
        if isinstance(sub, ast.Try) and sub.finalbody:
            final_src = "\n".join(ast.unparse(stmt) for stmt in sub.finalbody)
            found_finally_cleanup = found_finally_cleanup or (
                "not completed" in final_src
                and "not cancel_event.is_set()" in final_src
                and "cancel_event.set()" in final_src
                and "backend.reset_generation_state(cancel_event)" in final_src
                and _awaits_to_thread_gen_close(sub)
            )

    assert found_cancel_kwarg, (
        "generate_stream must pass cancel_event into backend.generate_chat_response "
        "so cancelled streams can stop backend generation"
    )
    assert found_cancel_handler, (
        "generate_stream must catch asyncio.CancelledError, set cancel_event, "
        "reset backend state, and re-raise"
    )
    assert found_finally_cleanup, (
        "generate_stream cleanup must cancel/reset incomplete streams and "
        "offload gen.close() with asyncio.to_thread so backend joins cannot "
        "block the event loop"
    )


def test_stream_chunks_cancel_branch_resets_backend_state():
    fn = None
    top = None
    for n in ast.walk(_TREE):
        if isinstance(n, ast.AsyncFunctionDef) and n.name == "produce_openai_chat_completions":
            top = n
            break
    assert top is not None
    for n in ast.walk(top):
        if isinstance(n, ast.AsyncFunctionDef) and n.name == "stream_chunks":
            fn = n
            break
    assert fn is not None, "stream_chunks generator missing"

    for sub in ast.walk(fn):
        if not isinstance(sub, ast.If):
            continue
        t = sub.test
        if not (
            isinstance(t, ast.Call)
            and isinstance(t.func, ast.Attribute)
            and t.func.attr == "is_set"
            and isinstance(t.func.value, ast.Name)
            and t.func.value.id == "cancel_event"
        ):
            continue
        body_src = "\n".join(ast.unparse(s) for s in sub.body)
        if "backend.reset_generation_state(cancel_event)" in body_src:
            return
    raise AssertionError(
        "stream_chunks `if cancel_event.is_set():` branch must call "
        "backend.reset_generation_state(cancel_event) -- matches the existing "
        "request.is_disconnected() / CancelledError cleanup paths and "
        "prevents KV-cache drift after cancel-via-POST"
    )


def test_unsloth_stream_loop_breaks_on_external_cancel_event():
    cancel_event = threading.Event()
    reset_calls = [0]

    class _Backend:
        def reset_generation_state(self):
            reset_calls[0] += 1

    backend = _Backend()

    def _generate():
        for i in range(200):
            time.sleep(0.005)
            yield f"cum-{i}"

    async def _loop():
        _DONE = object()
        loop = asyncio.get_event_loop()
        gen = _generate()
        seen = []
        while True:
            if cancel_event.is_set():
                backend.reset_generation_state()
                break
            cumulative = await loop.run_in_executor(None, next, gen, _DONE)
            if cumulative is _DONE:
                break
            seen.append(cumulative)
        return seen

    async def _fire():
        await asyncio.sleep(0.05)
        cancel_event.set()

    async def _main():
        return await asyncio.gather(_loop(), _fire())

    seen, _ = asyncio.run(_main())
    assert (
        len(seen) < 200
    ), f"loop must not drain the generator after cancel; got {len(seen)} tokens"
    assert reset_calls[0] == 1, (
        f"backend.reset_generation_state() must be called exactly once on "
        f"cancel-via-POST, got {reset_calls[0]}"
    )


def test_generate_stream_stays_responsive_under_blocking_next():
    chunks = ["alpha", "beta", "gamma", "delta"]

    def _generate_chat_response():
        for chunk in chunks:
            time.sleep(0.08)
            yield chunk

    def _sse(chunk):
        return f"data: {json.dumps({'content': chunk})}\n\n"

    async def _direct_loop():
        out = []
        for chunk in _generate_chat_response():
            out.append(_sse(chunk))
        out.append("data: [DONE]\n\n")
        return out

    async def _to_thread_loop():
        _DONE = object()
        gen = _generate_chat_response()
        out = []
        try:
            while True:
                chunk = await asyncio.to_thread(next, gen, _DONE)
                if chunk is _DONE:
                    break
                out.append(_sse(chunk))
            out.append("data: [DONE]\n\n")
            return out
        finally:
            try:
                gen.close()
            except (RuntimeError, ValueError):
                pass

    async def _run_with_heartbeat(loop_coro):
        ticks = 0
        max_gap = 0.0

        async def _heartbeat():
            nonlocal ticks, max_gap
            last = time.monotonic()
            while True:
                await asyncio.sleep(0.01)
                now = time.monotonic()
                max_gap = max(max_gap, now - last)
                last = now
                ticks += 1

        heartbeat = asyncio.create_task(_heartbeat())
        await asyncio.sleep(0)
        try:
            out = await loop_coro()
        finally:
            heartbeat.cancel()
            try:
                await heartbeat
            except asyncio.CancelledError:
                pass
        return out, ticks, max_gap

    async def _main():
        direct_out, direct_ticks, direct_max_gap = await _run_with_heartbeat(_direct_loop)
        threaded_out, threaded_ticks, threaded_max_gap = await _run_with_heartbeat(_to_thread_loop)
        return (
            direct_out,
            direct_ticks,
            direct_max_gap,
            threaded_out,
            threaded_ticks,
            threaded_max_gap,
        )

    (
        direct_out,
        direct_ticks,
        direct_max_gap,
        threaded_out,
        threaded_ticks,
        threaded_max_gap,
    ) = asyncio.run(_main())

    assert threaded_out == direct_out == [_sse(chunk) for chunk in chunks] + ["data: [DONE]\n\n"]
    assert direct_ticks == 0, (
        f"direct generate_stream loop should block the event loop; "
        f"got {direct_ticks} heartbeat ticks and max gap {direct_max_gap:.3f}s"
    )
    assert threaded_ticks >= 8, (
        f"to_thread generate_stream loop should let the event loop run; "
        f"got {threaded_ticks} heartbeat ticks"
    )
    assert threaded_max_gap < 0.06, (
        f"to_thread generate_stream loop should avoid long heartbeat gaps; "
        f"got {threaded_max_gap:.3f}s"
    )


def test_audio_stream_stays_responsive_under_blocking_next():
    cancel_event = threading.Event()

    def _audio_gen():
        for i in range(8):
            time.sleep(0.15)
            yield f"chunk-{i}"

    async def _prefix_loop():
        seen = []
        for chunk_text in _audio_gen():
            if cancel_event.is_set():
                break
            seen.append(chunk_text)
        return seen

    async def _postfix_loop():
        _DONE = object()
        gen = _audio_gen()
        seen = []
        while True:
            if cancel_event.is_set():
                break
            chunk_text = await asyncio.to_thread(next, gen, _DONE)
            if chunk_text is _DONE:
                break
            seen.append(chunk_text)
        return seen

    async def _fire_early():
        await asyncio.sleep(0.05)
        cancel_event.set()

    async def _run(loop_coro):
        return await asyncio.gather(loop_coro, _fire_early())

    # The blocking loop never lets _fire_early run, so it drains all chunks before the cancel.
    cancel_event.clear()
    t0 = time.monotonic()
    prefix_seen, _ = asyncio.run(_run(_prefix_loop()))
    prefix_elapsed = time.monotonic() - t0
    assert len(prefix_seen) == 8, (
        "pre-fix pattern let the cancel through, so it is no longer modelling a blocked "
        f"event loop; got {len(prefix_seen)} chunks"
    )

    cancel_event.clear()
    postfix_seen, _ = asyncio.run(_run(_postfix_loop()))
    assert (
        len(postfix_seen) < 8
    ), f"post-fix loop must not drain all chunks; got {len(postfix_seen)}"
    assert len(postfix_seen) < len(prefix_seen), (
        f"post-fix pattern saw as much as the blocking one: post={len(postfix_seen)} "
        f"vs pre={len(prefix_seen)}"
    )
    assert prefix_elapsed < 30.0, f"the blocking loop never returned: {prefix_elapsed:.3f}s"


def test_unsloth_stream_loop_emits_zero_tokens_on_preset_cancel():
    cancel_event = threading.Event()
    cancel_event.set()
    reset_calls = [0]

    class _Backend:
        def reset_generation_state(self):
            reset_calls[0] += 1

    backend = _Backend()

    next_calls = [0]

    def _generate():
        while True:
            next_calls[0] += 1
            yield f"cum-{next_calls[0]}"

    async def _loop():
        _DONE = object()
        loop = asyncio.get_event_loop()
        gen = _generate()
        seen = []
        while True:
            if cancel_event.is_set():
                backend.reset_generation_state()
                break
            cumulative = await loop.run_in_executor(None, next, gen, _DONE)
            if cumulative is _DONE:
                break
            seen.append(cumulative)
        return seen

    seen = asyncio.run(_loop())
    assert seen == [], (
        f"loop must emit zero tokens when cancel_event is pre-set "
        f"(pending-replay path); got {seen}"
    )
    assert next_calls[0] == 0, (
        f"loop must not call next() at all on pre-set cancel; got " f"{next_calls[0]} calls"
    )
    assert reset_calls[0] == 1, (
        f"backend.reset_generation_state() must still fire exactly once "
        f"on pre-set cancel; got {reset_calls[0]}"
    )


def test_audio_stream_emits_zero_chunks_on_preset_cancel():
    cancel_event = threading.Event()
    cancel_event.set()

    next_calls = [0]

    def _audio_gen():
        while True:
            next_calls[0] += 1
            yield f"chunk-{next_calls[0]}"

    async def _loop():
        _DONE = object()
        gen = _audio_gen()
        seen = []
        while True:
            if cancel_event.is_set():
                break
            chunk_text = await asyncio.to_thread(next, gen, _DONE)
            if chunk_text is _DONE:
                break
            seen.append(chunk_text)
        return seen

    seen = asyncio.run(_loop())
    assert seen == [], f"audio loop must emit zero chunks on pre-set cancel; got {seen}"
    assert next_calls[0] == 0, (
        f"audio loop must not call next() on pre-set cancel; got " f"{next_calls[0]} calls"
    )