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 video stream encode: a failed final flush falls back instead of hanging the render (#14413)

Daniel Han731cb16c2d5a
diff --git a/studio/backend/core/inference/video_stream_encode.py b/studio/backend/core/inference/video_stream_encode.py
index 4644d702d5e..4ef98a5b4b8 100644
--- a/studio/backend/core/inference/video_stream_encode.py
+++ b/studio/backend/core/inference/video_stream_encode.py
@@ -196,11 +196,13 @@ class StreamEncode:
def _encode(self) -> None:
container = None
+ stopped = False
try:
import av
while True:
item = self._queue.get()
if item is _STOP:
+ stopped = True
break
if self._failed or not self.valid:
continue
@@ -223,7 +225,8 @@ class StreamEncode:
container.mux(packet)
except Exception: # noqa: BLE001 - drain so the render thread never blocks on a full queue
self._failed = True
- while self._queue.get() is not _STOP:
+ # A failure in the final flush comes after _STOP was taken; waiting for another would hang _join.
+ while not stopped and self._queue.get() is not _STOP:
pass
finally:
if container is not None:
diff --git a/studio/backend/tests/test_video_stream_encode.py b/studio/backend/tests/test_video_stream_encode.py
index 555d9afb03a..982eb196410 100644
--- a/studio/backend/tests/test_video_stream_encode.py
+++ b/studio/backend/tests/test_video_stream_encode.py
@@ -193,3 +193,43 @@ def test_encode_mp4_claims_an_offered_stream_once():
assert vse._mp4_frame_count(data, frames.shape[1:3]) == len(frames)
assert vse._mp4_frame_count(again, frames.shape[1:3]) == len(frames)
assert vse.STATS["streamed"] == before + 1
+
+
+def test_a_failed_final_flush_falls_back_without_hanging(monkeypatch):
+ """The flush runs after the encoder thread took the stop marker; a failure there must not wait for another."""
+ import threading
+
+ class _Stream:
+ width = height = 0
+ pix_fmt = None
+ codec_context = type("Ctx", (), {"thread_type": None})()
+
+ def encode(self, frame = None):
+ if frame is None:
+ raise OSError(28, "No space left on device")
+ return []
+
+ class _Container:
+ def add_stream(self, *args, **kwargs):
+ return _Stream()
+
+ def mux(self, packet):
+ pass
+
+ def close(self):
+ pass
+
+ monkeypatch.setattr(av, "open", lambda *args, **kwargs: _Container())
+ pipe = _Pipe(_vae())
+ session = vse.StreamEncode(pipe.vae, pipe.video_processor, 8)
+ frames = _render(pipe, session, torch.randn(1, 4, 2, 8, 8))
+ result = {}
+ worker = threading.Thread(
+ target = lambda: result.setdefault("data", session.take(frames, 8)), daemon = True
+ )
+ worker.start()
+ worker.join(timeout = 60)
+ assert not worker.is_alive(), "take() hung after the encoder's final flush failed"
+ assert result["data"] is None
+ session.close()
+ assert not os.path.exists(session.path)