# starlette.websockets.WebSocketDisconnect: 1000

- **ID:** `python/starlette-websocket-disconnect-not-handled`
- **Domain:** python
- **Category:** network_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

Client disconnects from a WebSocket connection, and the server code doesn't catch the `WebSocketDisconnect` exception, causing an unhandled error.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 3.x | active | — | — |

## Workarounds

1. **** (95% success)
   ```
   Wrap the WebSocket communication in a try-except block: `try: await websocket.receive_text() except WebSocketDisconnect: await websocket.close()`
   ```
2. **** (90% success)
   ```
   Use a context manager like `async with websocket` which automatically handles disconnects.
   ```

## Dead Ends

- **** — Ignoring the exception and continuing to send messages after disconnect leads to `RuntimeError` or broken pipe errors. (90% fail)
- **** — Reconnecting the client doesn't help; the server must handle the disconnect gracefully. (80% fail)
