python network_error ai_generated true

aiohttp.client_exceptions.ServerDisconnectedError: Server disconnected

ID: python/aiohttp-server-disconnected

Also available as: JSON · Markdown · 中文
80%Fix Rate
87%Confidence
0Evidence
2024-03-22First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.8 active
3.9 active
3.10 active
3.11 active

Root Cause

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

中文

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

Workarounds

  1. 85% success
    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% success
    Set a Connection: close header to avoid keep-alive reuse:
    
    headers = {'Connection': 'close'}
    async with session.get(url, headers=headers) as resp:
        ...

Dead Ends

Common approaches that don't work:

  1. 60% fail

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

  2. 80% fail

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