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.

Studio: read only the run state for chunk-only chat run event batches (#15807)

Daniel Hana4aee21490c2
diff --git a/studio/backend/routes/chat_generation_runs.py b/studio/backend/routes/chat_generation_runs.py
index a8e496f3080..d6b0e24b901 100644
--- a/studio/backend/routes/chat_generation_runs.py
+++ b/studio/backend/routes/chat_generation_runs.py
@@ -448,7 +448,13 @@ async def chat_generation_events(
cursor,
15,
)
- snapshot = await asyncio.to_thread(db.get_run, run_id)
+ # Only non-chunk events carry the run, so a chunk-only batch skips parsing request_json.
+ snapshot = await asyncio.to_thread(
+ db.get_run
+ if any(event["type"] != "chunk" for event in events)
+ else db.get_run_state,
+ run_id,
+ )
if snapshot is None:
return
for event in events:
diff --git a/studio/backend/storage/chat_generation_runs_db.py b/studio/backend/storage/chat_generation_runs_db.py
index 39099d7a58b..77abc5db119 100644
--- a/studio/backend/storage/chat_generation_runs_db.py
+++ b/studio/backend/storage/chat_generation_runs_db.py
@@ -24,6 +24,7 @@ ACTIVE_STATUSES = frozenset({"queued", "running", "cancelling"})
TERMINAL_STATUSES = frozenset({"cancelled", "completed", "failed"})
ALL_STATUSES = ACTIVE_STATUSES | TERMINAL_STATUSES
_EVENTS_CHANGED = threading.Condition()
+_EVENTS_COMMITS = 0
_RUN_TOMBSTONE_PREFIX = "chat-generation-run-tombstone:"
ChatGenerationEventInput = Union[tuple[str, dict[str, Any]], tuple[str, dict[str, Any], int]]
@@ -405,9 +406,11 @@ def _touch_progress_locked(conn: sqlite3.Connection, run_id: str, tokens: int) -
def _commit(conn: sqlite3.Connection, *, notify: bool = False) -> None:
+ global _EVENTS_COMMITS
conn.commit()
if notify:
with _EVENTS_CHANGED:
+ _EVENTS_COMMITS += 1
_EVENTS_CHANGED.notify_all()
@@ -642,6 +645,25 @@ def get_run(run_id: str, owner_subject: str | None = None) -> dict[str, Any] | N
conn.close()
+def get_run_state(run_id: str) -> dict[str, Any] | None:
+ """Status, lastEventSeq and updatedAt of get_run's row, without reading or parsing request_json."""
+ conn = _connect()
+ try:
+ row = conn.execute(
+ "SELECT status, last_event_seq, updated_at FROM chat_generation_runs WHERE id=?",
+ (run_id,),
+ ).fetchone()
+ if row is None:
+ return None
+ return {
+ "status": row["status"],
+ "lastEventSeq": int(row["last_event_seq"]),
+ "updatedAt": int(row["updated_at"]),
+ }
+ finally:
+ conn.close()
+
+
def get_worker_token(run_id: str) -> str | None:
conn = _connect()
try:
@@ -937,13 +959,16 @@ def wait_for_events(
after: int = 0,
timeout: float = 15,
) -> list[dict[str, Any]]:
+ # Bumped under the lock after each notifying commit: unchanged means the unlocked read saw them all.
+ seen = _EVENTS_COMMITS
events = list_events(run_id, after)
if events:
return events
with _EVENTS_CHANGED:
- events = list_events(run_id, after)
- if events:
- return events
+ if _EVENTS_COMMITS != seen:
+ events = list_events(run_id, after)
+ if events:
+ return events
_EVENTS_CHANGED.wait(timeout)
return list_events(run_id, after)
diff --git a/studio/backend/tests/test_chat_generation_events_reads.py b/studio/backend/tests/test_chat_generation_events_reads.py
new file mode 100644
index 00000000000..d1bbcd7a8f3
--- /dev/null
+++ b/studio/backend/tests/test_chat_generation_events_reads.py
@@ -0,0 +1,172 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+import asyncio
+import json
+import random
+import threading
+import time
+from types import SimpleNamespace
+from unittest.mock import AsyncMock
+
+import pytest
+
+from routes import chat_generation_runs as run_routes
+from storage import chat_generation_runs_db as runs_db
+from storage import studio_db
+
+
+def running_run():
+ studio_db.upsert_chat_thread(
+ {"id": "thread-1", "title": "Chat", "modelType": "base", "modelId": "local", "createdAt": 1}
+ )
+ studio_db.upsert_chat_message(
+ {
+ "id": "user-1",
+ "threadId": "thread-1",
+ "role": "user",
+ "content": [{"type": "text", "text": "Hello"}],
+ "createdAt": 2,
+ }
+ )
+ history = [{"role": "user", "content": "x" * 2000} for _ in range(20)]
+ runs_db.create_run(
+ run_id = "run-1",
+ owner_subject = "alice",
+ thread_id = "thread-1",
+ user_message_id = "user-1",
+ assistant_message_id = "assistant-1",
+ request_payload = {"model": "local", "messages": history, "stream": True},
+ )
+ token = runs_db.get_worker_token("run-1")
+ assert runs_db.mark_running("run-1", token) is True
+ return token
+
+
+async def _stream(after = 0):
+ response = await run_routes.chat_generation_events(
+ "run-1",
+ SimpleNamespace(is_disconnected = AsyncMock(return_value = False)),
+ after = after,
+ last_event_id = None,
+ current_subject = "alice",
+ )
+ raw = ""
+ async for part in response.body_iterator:
+ raw += part.decode() if isinstance(part, bytes) else part
+ return raw
+
+
+def test_chunk_only_batches_do_not_parse_the_request(running_run, monkeypatch):
+ for i in range(5):
+ runs_db.append_events("run-1", running_run, [("chunk", {"delta": f"t{i}"})])
+ runs_db.finish_run("run-1", worker_token = running_run, status = "completed", finish_reason = "stop")
+ original_list = runs_db.list_events
+ monkeypatch.setattr(
+ runs_db, "list_events", lambda run_id, after = 0, limit = 1000: original_list(run_id, after, 1)
+ )
+ full_reads = []
+ original_get = runs_db.get_run
+ monkeypatch.setattr(
+ runs_db, "get_run", lambda *a, **k: full_reads.append(a) or original_get(*a, **k)
+ )
+
+ raw = asyncio.run(_stream())
+
+ frames = [json.loads(line[6:]) for line in raw.splitlines() if line.startswith("data: ")]
+ assert [frame["type"] for frame in frames] == ["run.created", "run.started"] + ["chunk"] * 5 + [
+ "run.completed"
+ ]
+ for frame in frames:
+ if frame["type"] == "chunk":
+ assert "run" not in frame
+ else:
+ assert len(frame["run"]["requestPayload"]["messages"]) == 20
+ # Ownership + opening checks, plus one per run-event batch (3).
+ assert len(full_reads) == 5
+
+
+def test_caught_up_wait_reads_events_twice(running_run, monkeypatch):
+ calls = []
+ original_list = runs_db.list_events
+ monkeypatch.setattr(
+ runs_db, "list_events", lambda *a, **k: calls.append(a) or original_list(*a, **k)
+ )
+ cursor = int(runs_db.get_run("run-1")["lastEventSeq"])
+ timer = threading.Timer(
+ 0.3, lambda: runs_db.append_events("run-1", running_run, [("chunk", {"delta": "a"})])
+ )
+ timer.start()
+ try:
+ events = runs_db.wait_for_events("run-1", cursor, 10)
+ finally:
+ timer.cancel()
+ assert [event["payload"] for event in events] == [{"delta": "a"}]
+ assert len(calls) == 2
+
+
+def test_commit_between_read_and_wait_is_not_missed(running_run, monkeypatch):
+ cursor = int(runs_db.get_run("run-1")["lastEventSeq"])
+ original_list = runs_db.list_events
+ state = {"first": True}
+
+ def racing_list(*args, **kwargs):
+ events = original_list(*args, **kwargs)
+ if state["first"]:
+ state["first"] = False
+ # Lands after the unlocked read saw nothing and before the waiter takes the lock.
+ runs_db.append_events("run-1", running_run, [("chunk", {"delta": "late"})])
+ return events
+
+ monkeypatch.setattr(runs_db, "list_events", racing_list)
+ started = time.monotonic()
+ events = runs_db.wait_for_events("run-1", cursor, 10)
+ assert [event["payload"] for event in events] == [{"delta": "late"}]
+ assert time.monotonic() - started < 5
+
+
+def test_concurrent_followers_never_miss_a_commit(running_run, monkeypatch):
+ # Widens the window between each unlocked read and the lock, so commits keep landing inside it.
+ original_list = runs_db.list_events
+ rng_lock = threading.Lock()
+ rng = random.Random(0)
+
+ def slow_list(*args, **kwargs):
+ events = original_list(*args, **kwargs)
+ with rng_lock:
+ delay = rng.random() * 0.002
+ time.sleep(delay)
+ return events
+
+ monkeypatch.setattr(runs_db, "list_events", slow_list)
+ total = 150
+ start = int(runs_db.get_run("run-1")["lastEventSeq"])
+ stalls = []
+ seen = {}
+
+ def follower(name):
+ cursor = start
+ received = []
+ while len(received) < total:
+ began = time.monotonic()
+ events = runs_db.wait_for_events("run-1", cursor, 30)
+ if time.monotonic() - began > 5:
+ stalls.append(name)
+ return
+ for event in events:
+ cursor = int(event["seq"])
+ received.append(event["payload"]["delta"])
+ seen[name] = received
+
+ threads = [threading.Thread(target = follower, args = (i,)) for i in range(6)]
+ for thread in threads:
+ thread.start()
+ for i in range(total):
+ runs_db.append_events("run-1", running_run, [("chunk", {"delta": str(i)})])
+ time.sleep(rng.random() * 0.003)
+ for thread in threads:
+ thread.join(60)
+ assert not stalls
+ assert all(not thread.is_alive() for thread in threads)
+ assert seen == {i: [str(n) for n in range(total)] for i in range(6)}