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.

Worker stderr mirror: read once more after an empty read that raced the stop (#16525)

Daniel Han12c4ba529f06
diff --git a/studio/backend/tests/test_inference_worker_shutdown_exit.py b/studio/backend/tests/test_inference_worker_shutdown_exit.py
index a84f53f4d10..4770216529b 100644
--- a/studio/backend/tests/test_inference_worker_shutdown_exit.py
+++ b/studio/backend/tests/test_inference_worker_shutdown_exit.py
@@ -6,6 +6,7 @@ from __future__ import annotations
import subprocess
import sys
from pathlib import Path
+from types import SimpleNamespace
_BACKEND_ROOT = Path(__file__).resolve().parents[1]
if str(_BACKEND_ROOT) not in sys.path:
@@ -84,3 +85,39 @@ def test_shutdown_exit_still_relays_the_mirrored_stderr_tail(tmp_path):
assert result.returncode == 0, result.stderr
assert "the last line before shutdown" in result.stderr
assert result.stderr.count("bulk ") == 200
+
+
+class _ReaderThatMissesTheLastWrites:
+ """The pump's first read returns empty; before the pump looks at stop, the worker writes its
+ last lines and stops the mirror, as when the pump thread is descheduled between the two."""
+
+ def __init__(self, stop, data: bytes):
+ self.stop = stop
+ self.pending = data
+ self.reads = 0
+
+ def read(self, size):
+ self.reads += 1
+ if self.reads == 1:
+ self.stop.set()
+ return b""
+ data, self.pending = self.pending[:size], self.pending[size:]
+ return data
+
+ def close(self):
+ pass
+
+
+def test_the_pump_reads_once_more_after_an_empty_read_that_raced_the_stop():
+ import threading
+
+ from utils import worker_stderr
+
+ stop = threading.Event()
+ reader = _ReaderThatMissesTheLastWrites(stop, b"the last line before shutdown\n")
+ relayed: list[bytes] = []
+ relay = SimpleNamespace(emit = relayed.append)
+
+ worker_stderr._tail_sink_to_stderr(reader, relay, SimpleNamespace(close = lambda: None), 0, stop)
+
+ assert b"".join(relayed) == b"the last line before shutdown\n"
diff --git a/studio/backend/utils/worker_stderr.py b/studio/backend/utils/worker_stderr.py
index 999c601707e..dd9793c54cf 100644
--- a/studio/backend/utils/worker_stderr.py
+++ b/studio/backend/utils/worker_stderr.py
@@ -495,12 +495,14 @@ def _tail_sink_to_stderr(
) -> None:
"""fd 2 must be the SINK, not a pipe: a fatal signal kills anything buffered in a pipe with its writer."""
while True:
+ # Read stop BEFORE reading: an empty read that began before stop.set() can predate the last writes.
+ stopping = stop.is_set()
try:
chunk = reader.read(65536)
except (OSError, ValueError):
break
if not chunk:
- if stop.is_set():
+ if stopping:
break
stop.wait(_TAIL_POLL_S)
continue