# 异步超时错误：WebSocket接收超时

- **ID:** `python/starlette-websocket-timeout`
- **领域:** python
- **类别:** network_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

WebSocket接收操作超过了配置的超时时间。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.x | active | — | — |

## 解决方案

1. **Use asyncio.wait_for with a reasonable timeout** (90% 成功率)
   ```
   try:
    data = await asyncio.wait_for(websocket.receive_text(), timeout=10)
except asyncio.TimeoutError:
    await websocket.close(code=1000)
   ```
2. **Set timeout in the WebSocket endpoint configuration** (85% 成功率)
   ```
   from starlette.websockets import WebSocketEndpoint
class MyEndpoint(WebSocketEndpoint):
    receive_timeout = 30
   ```

## 无效尝试

- **Increasing timeout to an extremely high value** — May cause resource exhaustion; not a real fix. (60% 失败率)
- **Ignoring the timeout and retrying indefinitely** — Leads to infinite loop and resource leak. (85% 失败率)
