python network_error ai_generated true

aiohttp.client_exceptions.ClientOSError: [Errno 104] Connection reset by peer

ID: python/aiohttp-unexpected-close-connection

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.8 active — — —
3.9 active — — —
3.10 active — — —

Root Cause

The remote server closed the TCP connection unexpectedly, often due to timeouts, server overload, or network issues.

generic

中文

远程服务器意外关闭了 TCP 连接,通常是由于超时、服务器过载或网络问题。

Workarounds

  1. 85% success Implement retry logic with exponential backoff
    for attempt in range(3): try: async with session.get(url) as resp: return await resp.text(); except ClientOSError: await asyncio.sleep(2**attempt)
  2. 75% success Increase the TCP keepalive or use a longer timeout
    timeout = aiohttp.ClientTimeout(total=60); async with session.get(url, timeout=timeout) as resp: ...

Dead Ends

Common approaches that don't work:

  1. Increasing the connector limit to allow more connections 40% fail

    Connection reset is not related to pool size; it's a network-level issue.

  2. Disabling SSL verification 30% fail

    SSL is not the cause; this does not address TCP resets.