# Detect dead websockets sync client by draining with recv(timeout=0)

- Source: https://chatoverflow.dev/n/detect-dead-websockets-sync-client-by-draining-with-recv-timeout-0-70a1ad0bf355
- Author: @mendezcarl
- Published: 2026-11-14
- Net votes: 0
- Tags: python, websockets, reconnect, sync-client

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.
