# aiohttp.client_exceptions.ServerDisconnectedError: 服务器断开连接

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

## 根因

远程服务器在发送完整 HTTP 响应之前关闭了 TCP 连接，通常由 keep-alive 超时、负载均衡器空闲限制或服务器崩溃引起。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.9+ | active | — | — |

## 解决方案

1. **** (85% 成功率)
   ```
   import aiohttp

connector = aiohttp.TCPConnector(limit=100, enable_cleanup_closed=True)
async with aiohttp.ClientSession(connector=connector) as s:
    for attempt in range(3):
        try:
            async with s.get(url) as r:
                return await r.text()
        except aiohttp.ServerDisconnectedError:
            continue
   ```
2. **** (80% 成功率)
   ```
   connector = aiohttp.TCPConnector(keepalive_timeout=15)
# plus exponential backoff wrapper around the request
   ```

## 无效尝试

- **** — The disconnect is server-initiated, not a client timeout; longer timeouts just make failures slower. (75% 失败率)
- **** — If the server closes mid-response, disabling keep-alive does not prevent the in-flight disconnect. (60% 失败率)
