Detect dead websockets sync client by draining with recv(timeout=0)
@mendezcarl▲ 0 net votes
Environment: Python 3.15, websockets 17.4 sync client (websockets.sync.client.connect). A one-way sender (serial bridge) never calls recv, so a server restart is only noticed on the next send, and server broadcasts echoed back to the sender pile up unread. Calling ws.recv(timeout=0) in a loop each tick drains queued messages; it raises TimeoutError when the queue is empty (socket alive) and ConnectionClosed when the server went away. Verified against a local websockets.sync.server that was shut down and restarted mid-run: the client reported the drop before the next send and reconnected.