fix(live): a dead output stops the stream even after a good frame

The failure cap read `consecutive_failures >= FAILURE_LIMIT and captured ==
0`, so it could only ever fire on a cold start. An output removed while
someone was watching -- grim answering "unknown output" to every grab from
then on -- kept the loop retrying twice a second for as long as the viewer
held the page open. That is the busy loop the cap exists to stop, so the
count is now of failures in a row and nothing else. Giving up stays sticky:
a removed output does not come back, and re-enabling the session is how it
returns.

While here, the capture thread is named for its output and the give-up is
logged by output. The phrase is the stream's only credential, and
faulthandler, `threading.excepthook` and any sampling profiler all print
thread names.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01URhyHGQnmKH9zBXxoznfQY
This commit is contained in:
HermenixandClaude Opus 5 committed 2026-10-06 16:19:32 +00:00
1 parent 9744d35945
commit 52e3786bfb
3 files changed
+92 -10

No files matched your search

+21 -8
View File
@@ -31,9 +31,12 @@ log = logging.getLogger("hhstream.live")
#: the box is also running the compositor and the main publisher.
DEFAULT_FPS = 5
FPS_CAP = 10
#: After this many capture failures with no successful frame, a stream gives up
#: and closes its viewers. `grim` against a removed output ("unknown output")
#: fails every time, and retrying forever would be a busy loop on a dead session.
#: After this many *consecutive* capture failures a stream gives up and closes
#: its viewers. `grim` against a removed output ("unknown output") fails every
#: time, and retrying forever would be a busy loop on a dead session -- which is
#: as true after a thousand good frames as it is from a cold start, so the count
#: is of failures in a row, not of failures-before-the-first-frame. A successful
#: grab resets it; giving up does not reset, because a dead output stays dead.
FAILURE_LIMIT = 10
@@ -95,8 +98,11 @@ class Stream:
# ---- lifecycle ---------------------------------------------------
def start(self) -> None:
# Named for the output, never the phrase: faulthandler, the default
# `threading.excepthook` and any profiler that samples the process all
# print thread names, and the phrase is this stream's only credential.
self._thread = threading.Thread(
target=self._run, name=f"hhstream-live-{self.spec.phrase}", daemon=True
target=self._run, name=f"hhstream-live-{self.spec.output}", daemon=True
)
self._thread.start()
@@ -129,15 +135,22 @@ class Stream:
with self._cond:
self._failures += 1
self._consecutive_failures += 1
give_up = self._consecutive_failures >= FAILURE_LIMIT and self._captured == 0
give_up = self._consecutive_failures >= FAILURE_LIMIT
if give_up:
# Sticky on purpose: a stream that reached the cap is
# not re-armed by `add_viewer`, because the case this
# guards -- an output that was removed -- does not come
# back. Re-enabling the session is how it comes back.
self._stopped = True
self._cond.notify_all()
if give_up:
log.warning("stream %s: giving up after %d capture failures: %s",
self.spec.phrase, FAILURE_LIMIT, exc)
# By output, not by phrase: a log line is the easiest place
# for a credential to escape to.
log.warning("stream on %s: giving up after %d consecutive "
"capture failures: %s",
self.spec.output, FAILURE_LIMIT, exc)
return
log.debug("stream %s: capture failed: %s", self.spec.phrase, exc)
log.debug("stream on %s: capture failed: %s", self.spec.output, exc)
if self._sleep(0.5):
return
continue