# starlette.requests.ClientDisconnect：客户端断开连接

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

## 根因

客户端在服务器读取完整请求体之前关闭了连接。

## 版本兼容性

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

## 解决方案

1. **** (90% 成功率)
   ```
   try:
    await request.json()
except ClientDisconnect:
    return JSONResponse(status_code=499, content={'detail': 'Client disconnected'})
   ```
2. **** (85% 成功率)
   ```
   async for chunk in request.stream():
    if await request.is_disconnected():
        break
   ```

## 无效尝试

- **** — Client already disconnected; timeout doesn't help. (80% 失败率)
- **** — May cause duplicate operations if not idempotent. (60% 失败率)
