Configurable heartbeat, test that a dead peer doesn't stop pings

This commit is contained in:
2026-11-16 14:47:47 +01:00
parent eae125a469
commit 40e0c7c479
3 changed files with 34 additions and 6 deletions
+1 -1
View File
@@ -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]
+2
View File
@@ -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
+30 -4
View File
@@ -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))
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())