{"record":{"id":"fccc8e0edcaf67c7","repo":"aio-libs/aiohttp","slug":"connection-timeout-to-host-req-url","errorCode":null,"errorMessage":"Connection timeout to host {req.url}","messagePattern":"Connection timeout to host (.+?)","errorType":"exception","errorClass":"ConnectionTimeoutError","httpStatus":null,"severity":"error","filePath":"aiohttp/client.py","lineNumber":240,"sourceCode":")\n\n_RetType_co = TypeVar(\n    \"_RetType_co\",\n    bound=\"ClientResponse | ClientWebSocketResponse[bool]\",\n    covariant=True,\n)\n_CharsetResolver = Callable[[ClientResponse, bytes], str]\n\n\n# Module-level (not a closure) so it has a stable identity for the\n# ``_cached_build_client_middlewares`` cache key.\nasync def _connect_and_send_request(req: ClientRequest) -> ClientResponse:\n    connector = req._session._connector\n    assert connector is not None\n    try:\n        conn = await connector.connect(req, traces=req._traces, timeout=req._timeout)\n    except asyncio.TimeoutError as exc:\n        raise ConnectionTimeoutError(f\"Connection timeout to host {req.url}\") from exc\n\n    assert conn.protocol is not None\n    conn.protocol.set_response_params(**req._response_params)\n    try:\n        resp = await req._send(conn)\n        try:\n            await resp.start(conn)\n        except BaseException:\n            resp.close()\n            raise\n    except BaseException:\n        conn.close()\n        raise\n    return resp\n\n\n@final\nclass ClientSession:","sourceCodeStart":222,"sourceCodeEnd":258,"githubUrl":"https://github.com/aio-libs/aiohttp/blob/d041d4d0fd48c3f0832084d33be16cf1c4835f85/aiohttp/client.py#L222-L258","documentation":"Raised as ConnectionTimeoutError('Connection timeout to host {url}') in _connect_and_send_request when the connector's connect() call raises asyncio.TimeoutError. This means the TCP connection (and any TLS handshake governed by the connect phase) did not complete within the configured timeout window. The original asyncio.TimeoutError is chained as the cause.","triggerScenarios":"await connector.connect(req, traces=..., timeout=req._timeout) does not finish before the relevant timeout (total or sock_connect) elapses, raising asyncio.TimeoutError, which _connect_and_send_request converts to ConnectionTimeoutError. Typical with a slow/unreachable host, a blocking DNS lookup, or an over-tight sock_connect value.","commonSituations":"The target host is unreachable or firewalled (SYN with no SYN-ACK); DNS resolution is slow; an over-restrictive ClientTimeout(sock_connect=...) or total=...; a proxy that stalls; connection pool exhaustion making connect wait; transient network blips or cloud cold-start latency.","solutions":["Increase ClientTimeout total and/or sock_connect to match the real network conditions.","Verify the URL/host is reachable and DNS resolves (test with a raw socket or curl).","Retry with exponential backoff for transient failures.","If using a proxy or connector pool, check the proxy health and pool limit/limit_per_host settings.","For slow TLS handshakes, ensure ssl timeouts and SNI are configured correctly."],"exampleFix":"// before\nasync with aiohttp.ClientSession(timeout=ClientTimeout(sock_connect=0.5)) as s:\n    await s.get(url)   // ConnectionTimeoutError\n// after\nasync with aiohttp.ClientSession(timeout=ClientTimeout(total=30, sock_connect=10)) as s:\n    await s.get(url)","handlingStrategy":"retry","validationCode":"// Right-size the timeout for the target's real latency.\ntimeout = aiohttp.ClientTimeout(total=30, sock_connect=10, sock_read=30)\nasync with aiohttp.ClientSession(timeout=timeout) as session:\n    ...\n","typeGuard":"null","tryCatchPattern":"for attempt in range(MAX_RETRIES):\n    try:\n        async with session.get(url, timeout=timeout) as resp:\n            return await resp.read()\n    except aiohttp.ConnectionTimeoutError:\n        backoff = 2 ** attempt\n        await asyncio.sleep(backoff)\nraise\n","preventionTips":["Set sock_connect/total to match worst-case network conditions, not best-case.","Verify reachability and DNS independently before retrying.","Retry transient timeouts with exponential backoff.","Check proxy health and connector pool limits (limit, limit_per_host)."],"tags":["network","timeout","client","connection","configuration"],"backgroundTag":null,"analyzedSha":"d041d4d0fd48c3f0832084d33be16cf1c4835f85","analyzedAt":"2026-08-11T20:44:15.550Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}