python network_error ai_generated true

aiohttp.client_exceptions.ServerDisconnectedError: Server disconnected

ID: python/aiohttp-serverdisconnectederror

Also available as: JSON · Markdown · 中文
80%Fix Rate
86%Confidence
0Evidence
2024-05-08First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.9+ active — — —

Root Cause

The remote server closed the TCP connection before sending a complete HTTP response, often due to keep-alive timeouts, load balancer idle limits, or server-side crashes.

generic

中文

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

Workarounds

  1. 85% success
    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% success
    connector = aiohttp.TCPConnector(keepalive_timeout=15)
    # plus exponential backoff wrapper around the request

Dead Ends

Common approaches that don't work:

  1. 75% fail

    The disconnect is server-initiated, not a client timeout; longer timeouts just make failures slower.

  2. 60% fail

    If the server closes mid-response, disabling keep-alive does not prevent the in-flight disconnect.