From 40e0c7c47969aa8c86d517393497c1707f5af6ce Mon Sep 17 00:00:00 2001 From: Showdown76py Date: Mon, 16 Nov 2026 14:47:47 +0100 Subject: [PATCH] Configurable heartbeat, test that a dead peer doesn't stop pings --- rotatyvote/app.py | 2 +- rotatyvote/config.py | 2 ++ tests/test_e2e.py | 36 +++++++++++++++++++++++++++++++----- 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/rotatyvote/app.py b/rotatyvote/app.py index bf5c232..1ce32dd 100644 --- a/rotatyvote/app.py +++ b/rotatyvote/app.py @@ -235,7 +235,7 @@ hub = Hub() async def heartbeat(): # proxies (cloudflare ~100s) drop idle websockets, keep every socket chatty while True: - await asyncio.sleep(25) + await asyncio.sleep(config.HEARTBEAT_S) try: conns = [ws for group in hub.screens.values() for ws in group] conns += [ws for group in hub.admins.values() for ws in group] diff --git a/rotatyvote/config.py b/rotatyvote/config.py index 5c33bd3..3d5aa67 100644 --- a/rotatyvote/config.py +++ b/rotatyvote/config.py @@ -22,6 +22,8 @@ TOKEN_GRACE_MS = int(os.environ.get("TOKEN_GRACE_MS", "2500")) ATTEMPT_SLACK_MS = int(os.environ.get("ATTEMPT_SLACK_MS", "8000")) # once presence is proven, how long the voter has to type an access code CODE_ENTRY_TTL_S = 180 +# keepalive for websockets, proxies drop idle ones (cloudflare ~100s) +HEARTBEAT_S = float(os.environ.get("HEARTBEAT_S", "25")) # codes needed to confirm each vote, 0 turns the check off DEFAULT_VOTE_SCANS = 3 diff --git a/tests/test_e2e.py b/tests/test_e2e.py index 4a3b809..dde314c 100644 --- a/tests/test_e2e.py +++ b/tests/test_e2e.py @@ -35,7 +35,7 @@ def server(tmp_path_factory): port = free_port() tmp = tmp_path_factory.mktemp("db") db = tmp / "test.db" - env = {**os.environ, "PORT": str(port), "HOST": "127.0.0.1", "DB_PATH": str(db), "PUBLIC_URL": ""} + env = {**os.environ, "PORT": str(port), "HOST": "127.0.0.1", "DB_PATH": str(db), "PUBLIC_URL": "", "HEARTBEAT_S": "1"} scheme, wscheme, ws_kw = "http", "ws", {} if TLS: cert, key = tmp / "cert.pem", tmp / "key.pem" @@ -575,11 +575,37 @@ def test_no_presence_qr_when_everyone_has_a_code(server): await ws.send(json.dumps({"key": p["screen_key"]})) st = json.loads(await asyncio.wait_for(ws.recv(), 3)) assert st["type"] == "state" and st["presence"] is False - with pytest.raises(asyncio.TimeoutError): - while True: - m = json.loads(await asyncio.wait_for(ws.recv(), 1.5)) - assert m["type"] != "qr" + t0 = time.time() + while time.time() - t0 < 2: + try: + m = json.loads(await asyncio.wait_for(ws.recv(), 0.5)) + except asyncio.TimeoutError: + continue + assert m["type"] != "qr" await ws.close() await a.aclose() asyncio.run(run()) + + +def test_heartbeat_survives_dead_client(server): + """a peer that vanished without a close frame must not stop pings to the others""" + p = create(server) + + async def run(): + async def connect(): + ws = await websockets.connect(f"{server['ws']}/ws/{p['id']}/admin", ping_interval=None, **server["ws_kw"]) + await ws.send(json.dumps({"key": p["admin_key"]})) + assert json.loads(await asyncio.wait_for(ws.recv(), 3))["type"] == "state" + return ws + dead, alive = await connect(), await connect() + dead.transport.abort() + pings = 0 + t0 = time.time() + while time.time() - t0 < 4: + m = json.loads(await asyncio.wait_for(alive.recv(), 3)) + pings += m["type"] == "ping" + assert pings >= 2 + await alive.close() + + asyncio.run(run())