fix(live): bound connections, a delimited stream, and a quiet access log

Four things the HTTP side owed.

A connection had no read timeout, so a client that connected and sent
nothing held a handler thread in `readline` for ever -- and on the MJPEG
route it also held a viewer slot, which keeps grim running for nobody.
Handlers now carry a 15 s header timeout, cleared once the MJPEG headers
are out because from there the wait is on frames, not on the client. TCP
keepalive covers the other half: a black-holed viewer that never closes.

The MJPEG response had no `Content-Length`, no chunked encoding and no
`Connection: close` -- `close_connection = True` was set after
`end_headers()`, so it never became a header. An intermediary was entitled
to buffer the whole thing waiting for an EOF that never comes.

`log_message` formatted the request line, which is the phrase, at debug
level; so did the client-error line. Both now log the method, the status
and the peer, never the path.

`RateLimiter` pruned only the key it was asked about, so every source
address that ever missed stayed in the table for the life of the process --
a leak any LAN host could drive. The sweep now covers the table.

Finally, `LiveServer` says where it is really bound: `address` still
returns something dialable, and `describe_bind` is what names `0.0.0.0` as
all interfaces.

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:50 +00:00
1 parent 19338af17c
commit a9299682d2
2 files changed
+196 -15

No files matched your search

+105 -1
View File
@@ -2,11 +2,13 @@
from __future__ import annotations
import logging
import socket
import threading
import time
from live_helpers import PHRASE, StubCapture, read_parts
from hhstream_live.server import BOUNDARY
from hhstream_live.server import BOUNDARY, HEADER_TIMEOUT_S, LiveServer, RateLimiter
WRONG = "no-such-phrase-here-00000"
@@ -94,6 +96,10 @@ def test_stream_is_mjpeg_and_delivers_valid_jpeg_parts(serve, http_conn):
assert resp.getheader("Content-Type") == (
f"multipart/x-mixed-replace; boundary={BOUNDARY}"
)
# The stream has no length and is not chunked, so the only thing that
# can delimit it is the close -- which an intermediary is only entitled
# to expect if the response says so.
assert resp.getheader("Connection") == "close"
parts = read_parts(resp, 2)
assert len(parts) == 2
for part in parts:
@@ -150,6 +156,104 @@ def test_no_capture_until_a_request_arrives(serve):
assert capture.calls >= 1
def test_a_silent_client_does_not_pin_a_handler_thread(serve):
"""A connection that never sends a request line must not hold a thread.
The socket timeout is the only thing that ends it; without one the handler
sits in `readline` for the life of the process.
"""
assert HEADER_TIMEOUT_S <= 30 # a cap that never fires is not a cap
with serve() as (server, _):
before = threading.active_count()
sock = socket.create_connection(("127.0.0.1", server.port), timeout=5)
try:
time.sleep(0.2)
assert threading.active_count() > before # a handler is waiting on us
sock.settimeout(HEADER_TIMEOUT_S + 10)
started = time.monotonic()
# The server closes on timeout, which arrives here as EOF.
assert sock.recv(4096) == b""
waited = time.monotonic() - started
assert waited <= HEADER_TIMEOUT_S + 5, waited
finally:
sock.close()
def test_a_viewer_that_drops_mid_stream_releases_its_slot(serve, http_conn):
"""A dropped viewer must bring `viewers` back to 0, or grim keeps running."""
import json
with serve(fps=10) as (server, _):
sock = socket.create_connection(("127.0.0.1", server.port), timeout=10)
sock.sendall(f"GET /{PHRASE}/stream.mjpg HTTP/1.1\r\n"
f"Host: 127.0.0.1\r\n\r\n".encode("ascii"))
assert sock.recv(4096) # headers, and at least the start of a part
def viewers() -> int:
return json.loads(get(http_conn(server), f"/{PHRASE}/status.json")[2])["viewers"]
# The headers can reach us a moment before the handler counts itself in.
deadline = time.monotonic() + 10
while viewers() == 0 and time.monotonic() < deadline:
time.sleep(0.05)
assert viewers() == 1
sock.close() # the phone walked out of range
deadline = time.monotonic() + 10
while viewers() and time.monotonic() < deadline:
time.sleep(0.1)
assert viewers() == 0
def test_the_access_log_never_carries_the_phrase(serve, http_conn, caplog):
"""Debug logging is on in development, and the request line is the phrase."""
with caplog.at_level(logging.DEBUG, logger="hhstream.live"):
with serve() as (server, _):
assert get(http_conn(server), f"/{PHRASE}/status.json")[0] == 200
assert get(http_conn(server), f"/{WRONG}/")[0] == 404
messages = [record.getMessage() for record in caplog.records]
assert messages, "something must be logged, or this proves nothing"
for message in messages:
assert PHRASE not in message, message
assert WRONG not in message, message
def test_the_server_says_when_it_is_bound_to_every_interface(registry):
"""`url_for` must print something dialable; the bind line tells the truth."""
loopback = LiveServer(registry, host="127.0.0.1", port=9988)
assert loopback.describe_bind() == "bound to 127.0.0.1:9988"
assert not loopback.binds_everywhere
everywhere = LiveServer(registry, host="0.0.0.0", port=9988)
assert everywhere.binds_everywhere
assert everywhere.address == "127.0.0.1" # still dialable
described = everywhere.describe_bind()
assert "0.0.0.0:9988" in described and "all interfaces" in described
def test_the_rate_limiter_counts_each_client_separately_and_forgets_them():
"""One noisy client must not spend another's budget, and must not be kept.
`_hits` is keyed by client address, so a table that only grew would be a
memory leak any LAN host could drive.
"""
now = [0.0]
limiter = RateLimiter(limit=2, window=60.0, clock=lambda: now[0])
assert limiter.allow("10.0.0.1")
assert limiter.allow("10.0.0.1")
assert not limiter.allow("10.0.0.1") # that client is out of budget
assert limiter.allow("10.0.0.2") # this one is not
assert limiter.tracked == 2
now[0] += 61.0 # both windows have passed
assert limiter.allow("10.0.0.1")
assert limiter.tracked == 1, "a client that went quiet is not kept"
def test_repeated_wrong_phrases_are_rate_limited(serve, http_conn):
with serve(limit=3) as (server, _):
conn = http_conn(server)