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.

17.6 KB
Raw Blame
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.

import asyncio
import json
import math
import os
import sys
import time
import threading
from types import SimpleNamespace

import pytest

_backend = os.path.join(os.path.dirname(__file__), "..")
sys.path.insert(0, _backend)

import routes.inference as inf_mod  # noqa: E402


class _Request:
    async def is_disconnected(self):
        return False


def test_non_streaming_generation_timeout_has_read_deadline():
    timeout = inf_mod._llama_non_streaming_generation_timeout()
    assert timeout.read == inf_mod._DEFAULT_FIRST_TOKEN_TIMEOUT_S


def test_an_infinite_env_value_does_not_remove_the_deadline(monkeypatch):
    """Non-finite env values (inf, 1e309) fall back to default, so the deadline still applies."""
    for raw in ("inf", "Infinity", "1e309", "-inf"):
        monkeypatch.setenv(inf_mod._OPENAI_COMPAT_FIRST_TOKEN_TIMEOUT_ENV, raw)
        monkeypatch.setenv(inf_mod._OPENAI_COMPAT_STREAM_KEEPALIVE_ENV, raw)

        first = inf_mod._first_token_timeout_s()
        assert math.isfinite(first), (raw, first)
        assert first == inf_mod._DEFAULT_FIRST_TOKEN_TIMEOUT_S, (raw, first)

        keepalive = inf_mod._openai_passthrough_stream_keepalive_interval()
        assert keepalive is None or math.isfinite(keepalive), (raw, keepalive)

        timeout = inf_mod._llama_non_streaming_generation_timeout()
        for field in ("connect", "read", "write", "pool"):
            value = getattr(timeout, field)
            assert value is None or math.isfinite(value), (raw, field, value)

    monkeypatch.setenv(inf_mod._OPENAI_COMPAT_FIRST_TOKEN_TIMEOUT_ENV, "37.5")
    assert inf_mod._first_token_timeout_s() == 37.5


def test_stream_first_item_deadline_after_headers():
    async def _run():
        class _Never:
            async def __anext__(self):
                await asyncio.Future()

        started = time.monotonic()
        try:
            async for _ in inf_mod._aiter_llama_stream_items(
                _Never(),
                first_token_deadline = started + 0.02,
            ):
                pass
        except inf_mod.httpx.ReadTimeout:
            pass
        else:
            raise AssertionError("first item deadline did not fire")
        assert time.monotonic() - started < 0.5

    asyncio.run(_run())


def test_stream_read_is_never_cancelled_to_implement_a_deadline():
    """A stream read must never be cancelled to enforce a deadline, since cancelling truncates the body."""

    async def _run():
        cancels = []
        reads = []

        class _Items:
            def __init__(self):
                self.count = 0

            async def __anext__(self):
                reads.append(1)
                try:
                    await asyncio.sleep(0.05)
                except asyncio.CancelledError:
                    cancels.append(1)
                    raise
                self.count += 1
                if self.count > 2:
                    raise StopAsyncIteration
                return "data: {}"

        out = []
        async for item in inf_mod._aiter_llama_stream_items(
            _Items(),
            first_token_deadline = time.monotonic() + 5,
            keepalive_interval_s = 0.01,
        ):
            if item is inf_mod._LLAMA_STREAM_KEEPALIVE:
                continue
            out.append(item)

        assert out == ["data: {}", "data: {}"]
        assert cancels == [], "a keepalive tick must never cancel the in-flight read"
        assert len(reads) == 3, reads

    asyncio.run(_run())


def test_stream_pump_does_not_require_asyncio_timeout(monkeypatch):
    """The repo floor is 3.9, which has no `asyncio.timeout`. The rewrite needs
    only `ensure_future` + `wait`, so removing it must change nothing."""
    monkeypatch.setattr(inf_mod.asyncio, "timeout", None, raising = False)

    async def _run():
        class _One:
            def __init__(self):
                self.done = False

            async def __anext__(self):
                if self.done:
                    raise StopAsyncIteration
                self.done = True
                return "data: {}"

        out = []
        async for item in inf_mod._aiter_llama_stream_items(
            _One(),
            first_token_deadline = time.monotonic() + 1,
        ):
            out.append(item)

        assert out == ["data: {}"]

    asyncio.run(_run())

    asyncio.run(_run())


def test_stream_wait_stops_on_known_disconnect_before_read():
    async def _run():
        state = SimpleNamespace(disconnect_checks = 0)
        cancel_event = threading.Event()

        class _Request:
            async def is_disconnected(self):
                state.disconnect_checks += 1
                return True

        class _Unread:
            async def __anext__(self):
                raise AssertionError("stream should stop before reading upstream")

        async for _ in inf_mod._aiter_llama_stream_items(
            _Unread(),
            cancel_event = cancel_event,
            request = _Request(),
            first_token_deadline = time.monotonic() + 1,
        ):
            raise AssertionError("stream should stop after disconnect")

        assert cancel_event.is_set()
        assert state.disconnect_checks == 1

    asyncio.run(_run())


def test_stream_wait_does_not_shorten_upstream_read_for_disconnect_poll():
    async def _run():
        response = SimpleNamespace(request = SimpleNamespace(extensions = {"timeout": {}}))
        seen_read_timeouts = []

        class _NoItem:
            async def __anext__(self):
                seen_read_timeouts.append(response.request.extensions["timeout"]["read"])
                raise StopAsyncIteration

        async for _ in inf_mod._aiter_llama_stream_items(
            _NoItem(),
            cancel_event = threading.Event(),
            request = _Request(),
            response = response,
            first_token_deadline = time.monotonic() + 1,
        ):
            raise AssertionError("stream should end")

        assert seen_read_timeouts
        assert seen_read_timeouts[0] > inf_mod._STREAM_DISCONNECT_POLL_TIMEOUT_S

    asyncio.run(_run())


def test_latched_read_ceiling_covers_the_stall_guard():
    """httpcore latches the read timeout before the body loop, so a low first-token value caps all reads."""

    async def _run():
        response = SimpleNamespace(request = SimpleNamespace(extensions = {"timeout": {}}))
        armed = []

        class _Items:
            def __init__(self):
                self.count = 0

            async def __anext__(self):
                armed.append(response.request.extensions["timeout"].get("read"))
                self.count += 1
                if self.count > 1:
                    raise StopAsyncIteration
                return "data: {}"

        async for _ in inf_mod._aiter_llama_stream_items(
            _Items(),
            response = response,
            first_token_deadline = time.monotonic() + 30.0,
            post_first_item_read_timeout_s = 120.0,
        ):
            pass

        assert armed[0] == 120.0, armed

    asyncio.run(_run())


def test_closing_the_pump_under_cancellation_re_raises_it(monkeypatch):
    """A cancel during pump teardown must be re-raised, or the caller runs its completion path anyway."""

    async def _run():
        class _Blocks:
            async def __anext__(self):
                await asyncio.Future()

        agen = inf_mod._aiter_llama_stream_items(
            _Blocks(),
            first_token_deadline = time.monotonic() + 30,
            keepalive_interval_s = 0.01,
        )
        assert await agen.__anext__() is inf_mod._LLAMA_STREAM_KEEPALIVE

        # monkeypatch, not assignment: inf_mod.asyncio is the stdlib module.
        async def _cancelled_wait(*args, **kwargs):
            raise asyncio.CancelledError()

        monkeypatch.setattr(inf_mod.asyncio, "wait", _cancelled_wait)
        try:
            await agen.aclose()
            outcome = "swallowed"
        except asyncio.CancelledError:
            outcome = "re-raised"
        finally:
            monkeypatch.undo()

        assert outcome == "re-raised", (
            "the pump absorbed a cancellation that _aclose_stream_resources "
            "needs to see, so a cancelled stream would report a clean close"
        )

    asyncio.run(_run())


def test_a_callable_bound_latches_no_socket_ceiling():
    """A callable bound can rise later, so no socket ceiling latches; the deadline enforces."""

    async def _run():
        response = SimpleNamespace(request = SimpleNamespace(extensions = {"timeout": {}}))
        armed = []
        values = iter([0.5, inf_mod._OPENAI_PASSTHROUGH_TERMINAL_GRACE_S])

        class _Items:
            def __init__(self):
                self.count = 0

            async def __anext__(self):
                armed.append(response.request.extensions["timeout"].get("read"))
                self.count += 1
                if self.count > 2:
                    raise StopAsyncIteration
                return "data: {}"

        async for _ in inf_mod._aiter_llama_stream_items(
            _Items(),
            response = response,
            first_token_deadline = time.monotonic() + 0.5,
            post_first_item_read_timeout_s = lambda: next(values, 0.5),
        ):
            pass

        assert armed[0] is None, armed

    asyncio.run(_run())


def test_deadline_does_not_discard_a_read_that_already_landed():
    """The deadline may fire only on an empty asyncio.wait, so a landed read is not discarded."""

    async def _run():
        gate = asyncio.Event()

        class _Items:
            def __init__(self):
                self.count = 0

            async def __anext__(self):
                self.count += 1
                if self.count > 1:
                    raise StopAsyncIteration
                await gate.wait()
                return "data: {}"

        out = []
        agen = inf_mod._aiter_llama_stream_items(
            _Items(),
            first_token_deadline = time.monotonic() + 0.2,
            keepalive_interval_s = 0.05,
        )
        async for item in agen:
            if item is inf_mod._LLAMA_STREAM_KEEPALIVE:
                gate.set()
                await asyncio.sleep(0.4)
                continue
            out.append(item)

        assert out == ["data: {}"], out

    asyncio.run(_run())


def test_preheader_send_cleanup_on_disconnect_and_cancel():
    async def _run(cancel_parent):
        state = SimpleNamespace(disconnected = False, closed = False, cancelled = False)
        started = asyncio.Event()

        class _Client:
            async def send(
                self,
                req,
                stream = False,
            ):
                started.set()
                try:
                    await asyncio.Future()
                except asyncio.CancelledError:
                    state.cancelled = True
                    raise

            async def aclose(self):
                state.closed = True

        class _Request:
            async def is_disconnected(self):
                return state.disconnected

        task = asyncio.create_task(
            inf_mod._send_stream_with_preheader_cancel(_Client(), object(), request = _Request())
        )
        await started.wait()
        if cancel_parent:
            task.cancel()
            try:
                await task
            except asyncio.CancelledError:
                pass
            else:
                raise AssertionError("helper cancellation did not propagate")
        else:
            state.disconnected = True
            assert await task is None
        assert state.closed
        assert state.cancelled

    asyncio.run(_run(False))
    asyncio.run(_run(True))


def test_stream_stall_timeout_callable_re_resolved_each_read():
    # Callable must be re-resolved per read, not captured at generator start.
    async def _run():
        response = SimpleNamespace(request = SimpleNamespace(extensions = {"timeout": {}}))
        values = iter([100.0, 2.0])
        seen = []

        class _Items:
            def __init__(self):
                self.count = 0

            async def __anext__(self):
                self.count += 1
                if self.count > 3:
                    raise StopAsyncIteration
                return "data: {}"

        async for _ in inf_mod._aiter_llama_stream_items(
            _Items(),
            cancel_event = threading.Event(),
            request = _Request(),
            response = response,
            first_token_deadline = time.monotonic() + 1,
            post_first_item_read_timeout_s = lambda: next(values, 5.0),
        ):
            seen.append(response.request.extensions["timeout"].get("read"))

        assert len(seen) == 3
        assert seen[0] == 100.0
        assert 1.0 <= seen[1] <= 2.0
        assert 4.0 <= seen[2] <= 5.0

    asyncio.run(_run())


def test_stream_stall_timeout_disabled_clears_read_timeout():
    # Stall guard disabled: the leftover first-token timeout must be cleared.
    async def _run():
        response = SimpleNamespace(request = SimpleNamespace(extensions = {"timeout": {}}))
        seen = []

        class _Items:
            def __init__(self):
                self.count = 0

            async def __anext__(self):
                self.count += 1
                if self.count > 2:
                    raise StopAsyncIteration
                return "data: {}"

        async for _ in inf_mod._aiter_llama_stream_items(
            _Items(),
            cancel_event = threading.Event(),
            request = _Request(),
            response = response,
            first_token_deadline = time.monotonic() + 5,
            post_first_item_read_timeout_s = lambda: None,
        ):
            seen.append(response.request.extensions["timeout"].get("read"))

        assert seen == [None, None], seen

    asyncio.run(_run())


@pytest.mark.parametrize("wire_format", ["lines", "bytes"])
def test_prompt_progress_does_not_start_the_decode_timeout(wire_format):
    async def run():
        progress = "data: " + json.dumps(
            {
                "choices": [{"index": 0, "delta": {"role": "assistant", "content": None}}],
                "prompt_progress": {"processed": 512, "total": 2048},
            }
        )
        output = 'data: {"choices":[{"index":0,"delta":{"content":"ok"}}]}'
        response = SimpleNamespace(request = SimpleNamespace(extensions = {"timeout": {}}))

        async def items():
            if wire_format == "bytes":
                data = (progress + "\n\n").encode()
                yield data[:40]
                yield data[40:]
            else:
                yield progress
                yield ""
            await asyncio.sleep(0.08)
            yield (output + "\n\n").encode() if wire_format == "bytes" else output
            await asyncio.Future()

        received = []
        with pytest.raises(inf_mod.httpx.ReadTimeout, match = "mid-response"):
            async for item in inf_mod._aiter_llama_stream_items(
                items(),
                response = response,
                first_token_deadline = time.monotonic() + 1,
                post_first_item_read_timeout_s = 0.02,
                track_prefill_progress = True,
            ):
                received.append(item)
                if "ok" not in str(item):
                    assert response.request.extensions["timeout"]["read"] is None
        assert any("ok" in str(item) for item in received)
        assert response.request.extensions["timeout"]["read"] == 0.02

    asyncio.run(run())


@pytest.mark.parametrize("advancing", [True, False])
def test_only_advancing_prefill_progress_renews_the_deadline(monkeypatch, advancing):
    monkeypatch.setattr(inf_mod, "_first_token_timeout_s", lambda: 0.12)

    async def run():
        async def items():
            for index in range(6):
                yield "data: " + json.dumps(
                    {
                        "choices": [{"delta": {"role": "assistant", "content": None}}],
                        "prompt_progress": {"processed": index if advancing else 0},
                    }
                )
                await asyncio.sleep(0.04)
            yield 'data: {"choices":[{"delta":{"content":"ok"}}]}'

        received = []

        async def drain():
            async for item in inf_mod._aiter_llama_stream_items(
                items(),
                first_token_deadline = time.monotonic() + 0.12,
                post_first_item_read_timeout_s = 0.02,
                track_prefill_progress = True,
            ):
                received.append(item)

        if advancing:
            await drain()
            assert '"ok"' in received[-1]
        else:
            with pytest.raises(inf_mod.httpx.ReadTimeout, match = "first token"):
                await drain()
            assert len(received) < 6

    asyncio.run(run())


def test_prefill_tracking_preserves_terminal_grace_without_output():
    async def run():
        response = SimpleNamespace(request = SimpleNamespace(extensions = {"timeout": {}}))
        armed = []

        async def items():
            armed.append(response.request.extensions["timeout"]["read"])
            yield 'data: {"choices":[{"delta":{},"finish_reason":"stop"}]}'
            await asyncio.Future()

        with pytest.raises(inf_mod.httpx.ReadTimeout, match = "mid-response"):
            async for _ in inf_mod._aiter_llama_stream_items(
                items(),
                response = response,
                first_token_deadline = time.monotonic() + 1,
                post_first_item_read_timeout_s = lambda: 0.02,
                track_prefill_progress = True,
            ):
                pass
        assert armed == [None]

    asyncio.run(run())