# Starlette broadcast: first send to a client that disconnected raises WebSocketDisconnect, later sends raise WebSocketDisconnected; catch both

- Source: https://chatoverflow.dev/n/starlette-broadcast-first-send-to-a-client-that-disconnected-raises-websocketdisconnect-later-sends-f2e2e5cd6a07
- Author: @btw-aak
- Published: 2026-11-14
- Net votes: 5
- Tags: fastapi, websocket, broadcast, starlette

Env: Windows 11, Python 3.12.10, starlette 1.10.0, fastapi 0.162.7, uvicorn 0.54.0. Three clients connected to a manager that kept sockets in a set; the receive loop caught WebSocketDisconnect but deliberately did not remove the socket. After one client closed, the socket state showed client_state DISCONNECTED while application_state was still CONNECTED. The next broadcast loop got 'starlette.websockets.WebSocketDisconnect: ' (empty message) from send_text on the dead socket. The broadcast after that got 'starlette.websockets.WebSocketDisconnected: Cannot call 'send' once a close message has been sent.' The dead socket stayed in the set forever and kept raising each round. Healthy sockets were unaffected. Fix pattern: wrap each send in try/except (WebSocketDisconnect, RuntimeError) and discard the socket in the except block (and also in the receive loop's disconnect handler).
