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
fix(studio): stop a managed runtime when the client drops its stream (#12266)
Aleksei Boklag95529f15652f
diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.pyindex 583b940bf3a..e95e6cb8c88 100644--- a/studio/backend/routes/inference.py+++ b/studio/backend/routes/inference.py@@ -27716,6 +27716,12 @@ async def _stop_on_cancel(agen, cancel_event: threading.Event):leaves a cancelled generation holding the model through a swap's teardown."""+ def _retrieve_exception(task: asyncio.Task) -> None:+ try:+ task.exception()+ except asyncio.CancelledError:+ pass+async def _cancelled() -> None:while not cancel_event.is_set():await asyncio.sleep(0.1)@@ -27734,11 +27740,13 @@ async def _stop_on_cancel(agen, cancel_event: threading.Event):returnyield linefinally:- for task in (step, waiter):- if task is not None and not task.done():- # cancelling the pending read closes the upstream response inside ``agen``.- task.cancel()- await asyncio.gather(task, return_exceptions = True)+ pending = [task for task in (step, waiter) if task is not None and not task.done()]+ for task in pending:+ task.add_done_callback(_retrieve_exception)+ task.cancel()+ if pending:+ # asyncio.wait leaves cancellation cleanup running when the relay is cancelled.+ await asyncio.wait(pending)try:await agen.aclose()except RuntimeError:diff --git a/studio/backend/tests/test_stop_on_cancel.py b/studio/backend/tests/test_stop_on_cancel.pynew file mode 100644index 00000000000..42014678f27--- /dev/null+++ b/studio/backend/tests/test_stop_on_cancel.py@@ -0,0 +1,61 @@+# 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 == []