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.

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

"""_stop_on_cancel must let a cancelled upstream read finish closing its httpcore connection."""

import asyncio
import gc
import threading

import anyio

from routes.inference import _stop_on_cancel


def test_cancelled_read_finishes_closing_the_upstream():
    closed = asyncio.Event()

    async def upstream():
        yield "data: first"
        try:
            # the runtime keeps generating while the read is pending
            await asyncio.Event().wait()
        except asyncio.CancelledError:
            await asyncio.sleep(0.05)  # model connection cleanup that requires another await
            closed.set()
            raise

    async def main():
        cancel_event = threading.Event()
        with anyio.CancelScope() as scope:
            async for _line in _stop_on_cancel(upstream(), cancel_event):
                scope.cancel()  # the client left after the first frame
        await asyncio.wait_for(closed.wait(), timeout = 2)

    asyncio.run(main())


def test_cancelled_read_exception_is_retrieved():
    contexts = []

    async def upstream():
        yield "data: first"
        try:
            await asyncio.Event().wait()
        except asyncio.CancelledError:
            await asyncio.sleep(0.05)
            raise RuntimeError("transport close failed")

    async def main():
        loop = asyncio.get_running_loop()
        loop.set_exception_handler(lambda _loop, context: contexts.append(context))
        existing_tasks = asyncio.all_tasks()
        with anyio.CancelScope() as scope:
            async for _line in _stop_on_cancel(upstream(), threading.Event()):
                scope.cancel()
        gc.collect()
        await asyncio.sleep(0.1)
        assert not (asyncio.all_tasks() - existing_tasks)

    asyncio.run(main())
    assert contexts == []