Files
HermenixandClaude Opus 5 52e3786bfb 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
2026-10-06 16:19:32 +00:00

185 lines
6.4 KiB
Python

"""The publisher: no capture while unwatched, one slot, and giving up when dead."""
from __future__ import annotations
import logging
import time
import pytest
from live_helpers import PHRASE, StubCapture
from hhstream_live.publisher import (FPS_CAP, FAILURE_LIMIT, Stream, StreamRegistry,
StreamSpec)
def make_stream(capture, *, fps=10, phrase=PHRASE):
return Stream(StreamSpec(phrase=phrase, output="hh-demo", fps=fps), capture)
@pytest.mark.parametrize("bad", [0, -1, True, 1.5, FPS_CAP + 1])
def test_fps_must_be_a_positive_int_within_the_cap(bad):
with pytest.raises(ValueError):
make_stream(StubCapture(), fps=bad)
def test_no_capture_while_nobody_is_watching():
"""An enabled stream costs nothing until a request arrives."""
capture = StubCapture()
stream = make_stream(capture)
stream.start()
try:
time.sleep(0.4)
assert capture.calls == 0
finally:
stream.stop()
def test_capture_starts_when_a_viewer_arrives_and_stops_when_it_leaves():
capture = StubCapture()
stream = make_stream(capture)
stream.start()
try:
stream.add_viewer()
frame, seq = stream.wait_frame(0, timeout=5)
assert frame and seq >= 1
stream.remove_viewer()
time.sleep(0.3)
settled = capture.calls
time.sleep(0.4)
assert capture.calls == settled # idle again
finally:
stream.stop()
def test_snapshot_is_none_before_anyone_watches_and_bytes_after():
stream = make_stream(StubCapture())
stream.start()
try:
assert stream.snapshot() is None
stream.add_viewer()
frame, _ = stream.wait_frame(0, timeout=5)
stream.remove_viewer()
assert frame is not None
assert stream.snapshot() == frame
finally:
stream.stop()
def test_a_slow_viewer_does_not_queue_frames():
"""One slot: a viewer that sleeps sees the latest frame, never a backlog."""
stream = make_stream(StubCapture(delay=0.01), fps=10)
stream.start()
try:
stream.add_viewer()
first, seq = stream.wait_frame(0, timeout=5)
time.sleep(0.3) # let many frames go by unsent
later, seq2 = stream.wait_frame(seq, timeout=5)
assert later is not None
# seq jumped by more than one: the frames in between were dropped, not
# buffered for us.
assert seq2 > seq + 1
finally:
stream.stop()
def test_a_dead_capture_gives_up_and_closes_viewers():
capture = StubCapture(fail_first=10_000) # never succeeds
stream = make_stream(capture, fps=10)
stream.start()
try:
stream.add_viewer()
frame, _ = stream.wait_frame(0, timeout=10)
assert frame is None
assert stream.stopped
assert capture.calls >= FAILURE_LIMIT
finally:
stream.stop()
def test_a_capture_that_dies_after_a_good_frame_also_gives_up():
"""The cap counts failures *in a row*, not failures before the first frame.
An output removed while someone is watching fails every grab from then on.
A give-up rule that only applied to a cold start would leave this stream
retrying twice a second for as long as the viewer held the page open --
exactly the busy loop on a dead session the cap exists to stop.
"""
capture = StubCapture(fail_after=1) # one good frame, then the output is gone
stream = make_stream(capture, fps=10)
stream.start()
try:
stream.add_viewer() # and the viewer stays attached for the whole test
frame, seq = stream.wait_frame(0, timeout=5)
assert frame is not None and seq >= 1
# Poll through the viewer's own wait, so a `wait_frame` that hangs is a
# failure here rather than a test that quietly waits for the deadline.
deadline = time.monotonic() + 15
while not stream.stopped and time.monotonic() < deadline:
assert stream.wait_frame(seq, timeout=1.0) == (None, seq)
assert stream.stopped, "a dead output must stop the stream, frame or no frame"
# And the viewer in the loop is released at once rather than on timeout.
started = time.monotonic()
assert stream.wait_frame(seq, timeout=10.0) == (None, seq)
assert time.monotonic() - started < 1.0
assert capture.calls >= FAILURE_LIMIT
finally:
stream.stop()
def test_giving_up_never_writes_the_phrase_to_the_log(caplog):
"""A log line is the easiest place for the stream's one credential to leak.
faulthandler and `threading.excepthook` print thread names, so the capture
thread is checked here too.
"""
capture = StubCapture(fail_first=10_000)
stream = make_stream(capture, fps=10)
with caplog.at_level(logging.DEBUG, logger="hhstream.live"):
stream.start()
thread_name = stream._thread.name
try:
stream.add_viewer()
assert stream.wait_frame(0, timeout=15) == (None, 0)
assert stream.stopped
finally:
stream.stop()
messages = [record.getMessage() for record in caplog.records]
assert messages, "the give-up must be logged at all"
assert any("hh-demo" in message for message in messages)
for message in messages:
assert PHRASE not in message, message
assert PHRASE not in thread_name and "hh-demo" in thread_name
# ---- registry --------------------------------------------------------------
def test_registry_adds_and_finds_a_phrase_case_insensitively():
registry = StreamRegistry()
try:
registry.add(StreamSpec(phrase=PHRASE, output="hh-demo"), StubCapture())
assert registry.get("Amber-Otter-Lantern-Harbor-7K2QP") is not None
assert registry.get("nope-not-here-at-all") is None
finally:
registry.stop_all()
def test_registry_refuses_a_duplicate_phrase():
registry = StreamRegistry()
try:
registry.add(StreamSpec(phrase=PHRASE, output="hh-demo"), StubCapture())
with pytest.raises(ValueError):
registry.add(StreamSpec(phrase=PHRASE, output="hh-other"), StubCapture())
finally:
registry.stop_all()
def test_registry_remove_stops_the_stream():
registry = StreamRegistry()
stream = registry.add(StreamSpec(phrase=PHRASE, output="hh-demo"), StubCapture())
assert registry.remove(PHRASE) is stream
assert registry.get(PHRASE) is None
assert stream.stopped
assert registry.remove(PHRASE) is None