# websockets.exceptions.ConnectionClosedOK: received 1000 (OK); then sent 1000 (OK)

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

## Root Cause

The WebSocket connection was closed by the client or server normally.

## Version Compatibility

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

## Workarounds

1. **Handle the close event gracefully and clean up resources.** (90% success)
   ```
   async def websocket_endpoint(websocket):
    await websocket.accept()
    try:
        while True:
            data = await websocket.receive_text()
            await websocket.send_text(f"Echo: {data}")
    except websockets.exceptions.ConnectionClosed:
        print('Connection closed')
   ```
2. **Implement reconnection logic with exponential backoff.** (85% success)
   ```
   import asyncio
async def connect():
    while True:
        try:
            async with websockets.connect('ws://...') as ws:
                await ws.send('hello')
        except websockets.exceptions.ConnectionClosed:
            await asyncio.sleep(1)
   ```

## Dead Ends

- **Assuming it's an error and trying to reconnect indefinitely.** — Normal closure is not an error; reconnecting without reason is wasteful. (60% fail)
- **Ignoring the close event and continuing to send messages.** — The connection is closed; sending messages will raise another error. (90% fail)
