python network_error ai_generated true

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

aiohttp.client_exceptions.ServerDisconnectedError: Server disconnected

ID: python/aiohttp-server-disconnected

其他格式: JSON · Markdown 中文 · English
80%修复率
87%置信度
0证据数
2024-03-22首次发现

版本兼容性

版本状态引入弃用备注
3.8 active
3.9 active
3.10 active
3.11 active

根因分析

远程服务器在发送完整响应之前关闭了 TCP 连接,通常是由于 keep-alive 超时、服务器端崩溃或代理干扰。

English

The remote server closed the TCP connection before sending a complete response, often due to keep-alive timeouts, server-side crashes, or proxy interference.

generic

解决方案

  1. 85% 成功率
    Enable retries with exponential backoff using aiohttp's built-in retry or a custom decorator:
    
    from aiohttp_retry import RetryClient, ExponentialRetry
    retry_options = ExponentialRetry(attempts=3)
    async with RetryClient(retry_options=retry_options) as client:
        await client.get(url)
  2. 70% 成功率
    Set a Connection: close header to avoid keep-alive reuse:
    
    headers = {'Connection': 'close'}
    async with session.get(url, headers=headers) as resp:
        ...

无效尝试

常见但无效的做法:

  1. 60% 失败

    The disconnect is not caused by slowness; a longer timeout just delays the failure.

  2. 80% 失败

    The error is at the TCP layer, not TLS; disabling SSL does not prevent the server from closing the connection.