python network_error ai_generated true

asyncio.exceptions.TimeoutError: Connection timeout to host https://slow.example.com

ID: python/aiohttp-timeout-total

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.8 active — — —
3.9 active — — —
3.10 active — — —
3.11 active — — —
3.12 active — — —

Root Cause

The aiohttp ClientTimeout (total or sock_connect) elapsed before the server responded; default total is 5 minutes but sock_connect is only 30s.

generic

中文

aiohttp ClientTimeout(total 或 sock_connect)在服务器响应之前耗尽;默认 total 为 5 分钟,但 sock_connect 只有 30 秒。

Workarounds

  1. 95% success
    Configure explicit timeouts:
    
    timeout = aiohttp.ClientTimeout(total=30, connect=5, sock_read=10)
    async with aiohttp.ClientSession(timeout=timeout) as s:
        await s.get(url)
  2. 90% success
    Wrap with asyncio.wait_for for a per-call ceiling:
    
    await asyncio.wait_for(session.get(url), timeout=15)
  3. 88% success
    Use aiohttp-retry for exponential backoff on TimeoutError.

Dead Ends

Common approaches that don't work:

  1. 85% fail

    Requests hang forever on dead servers, exhausting the connection pool.

  2. 60% fail

    Masks slow endpoints but ties up sockets and worker slots.

  3. 70% fail

    Hammers an already-slow server and can trigger rate limits.