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.

Drop late inference responses after their request is released (#10388)

Ian1112620d7022
diff --git a/studio/backend/core/inference/orchestrator.py b/studio/backend/core/inference/orchestrator.py
index ae2409d5742..bb7d64c358c 100644
--- a/studio/backend/core/inference/orchestrator.py
+++ b/studio/backend/core/inference/orchestrator.py
@@ -913,7 +913,8 @@ class InferenceOrchestrator:
else:
self._mark_worker_started(owner)
other.put(resp)
- return None
+ # Outside the mailbox check on purpose: a released request's late frames go to nobody.
+ return None
return resp
def drain(timeout: float = 5.0) -> bool:
@@ -2017,9 +2018,7 @@ class InferenceOrchestrator:
if not self._ensure_subprocess_alive():
raise RuntimeError(self._subprocess_crash_message("count"))
continue
- # A reply whose own mailbox is gone -- an earlier count that timed out
- # while the worker still held its command -- is handed back to whoever is
- # reading, and the type alone cannot tell it from this one's.
+ # _direct_reader already drops a reply whose mailbox is gone; this is the backstop.
if (
candidate.get("type") == "count_tokens_response"
and candidate.get("request_id") == request_id
diff --git a/studio/backend/tests/test_inference_dispatcher_resilience.py b/studio/backend/tests/test_inference_dispatcher_resilience.py
index ea903a6ce05..5318580f84f 100644
--- a/studio/backend/tests/test_inference_dispatcher_resilience.py
+++ b/studio/backend/tests/test_inference_dispatcher_resilience.py
@@ -17,6 +17,8 @@ import threading
import time
from pathlib import Path
+import pytest
+
_BACKEND_DIR = str(Path(__file__).resolve().parent.parent)
if _BACKEND_DIR not in sys.path:
sys.path.insert(0, _BACKEND_DIR)
@@ -184,3 +186,70 @@ def _direct_reader_calls(o, request_id):
"""_direct_reader wired to a scripted _read_resp (o._scripted, popped in order)."""
o._read_resp = lambda timeout = 1.0: o._scripted.pop(0) if o._scripted else None
return o._direct_reader(request_id)
+
+
+ "response_type", ["token", "gen_done", "gen_error", "audio_done", "audio_error"]
+)
+def test_direct_reader_discards_responses_from_released_requests(response_type):
+ # Consumers dispatch on type alone: a released request's late frame becomes this one's answer.
+ o = _direct_reader_host()
+ current = {"request_id": "current", "type": "token", "text": "current answer"}
+ o._scripted = [
+ {"request_id": "cancelled", "type": response_type, "text": "old answer"},
+ current,
+ ]
+ read_one, _drain, release = _direct_reader_calls(o, "current")
+ try:
+ assert read_one(timeout = 0.1) is None
+ assert read_one(timeout = 0.1) == current
+ finally:
+ release()
+
+
+def test_direct_reader_discards_only_what_is_addressed_to_someone_else():
+ # Dropping the `rid and` half would swallow the worker's unaddressed crash error and hang the chat.
+ o = _direct_reader_host()
+ worker_error = {"type": "error", "error": "Command 'generate' failed: out of memory"}
+ o._scripted = [worker_error, {"request_id": "", "type": "gen_done"}]
+ read_one, _drain, release = _direct_reader_calls(o, "current")
+ try:
+ assert read_one(timeout = 0.1) == worker_error
+ assert read_one(timeout = 0.1) == {"request_id": "", "type": "gen_done"}
+ finally:
+ release()
+
+
+def test_discarding_a_released_response_leaves_worker_ownership_alone():
+ # Ownership must not move on a discarded frame, or the live chat's Stop hits the wrong generation.
+ o = _direct_reader_host()
+ mine, theirs = threading.Event(), threading.Event()
+ o._request_cancel_events = {"current": mine, "cancelled": theirs}
+ o._claim_worker(mine)
+ o._mark_worker_started(mine)
+ o._scripted = [{"request_id": "cancelled", "type": "token", "text": "late"}]
+
+ read_one, _drain, release = _direct_reader_calls(o, "current")
+ try:
+ assert read_one(timeout = 0.1) is None
+ assert o._owns_worker(mine), "a discarded frame must not move the executor"
+ assert not o._owns_worker(theirs)
+ finally:
+ release()
+
+
+def test_direct_reader_drain_waits_for_its_own_terminal_response():
+ # Ending the drain on an orphan terminal hands the next request a worker that never stopped.
+ o = _direct_reader_host()
+ o._scripted = [
+ {"request_id": "cancelled", "type": "gen_done"},
+ {"request_id": "current", "type": "token", "text": "still running"},
+ {"request_id": "current", "type": "gen_done"},
+ ]
+ o._ensure_subprocess_alive = lambda: True
+ _read_one, drain, release = _direct_reader_calls(o, "current")
+ try:
+ assert drain(timeout = 1.0)
+ assert not o._scripted, "an orphan gen_done must not end the current drain early"
+ finally:
+ release()