{"id":"1d8989925776fec0","repo":"aio-libs/aiohttp","slug":"connection-timeout-to-host-url","errorCode":null,"errorMessage":"Connection timeout to host {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/c0ef574e29109210e96e652771ae4e7b88615fa4/aiohttp/client.py#L222-L258","documentation":"Raised in _connect_and_send_request (client.py:240) when TCPConnector.connect() does not complete within the configured sock_connect timeout. The asyncio.TimeoutError is wrapped and re-raised as ConnectionTimeoutError with the target URL. It is a subclass of ServerTimeoutError/ClientError.","triggerScenarios":"session.request(...) / session.ws_connect(...) where the TCP (or TLS) handshake to the target host exceeds ClientTimeout.sock_connect (default 5 minutes if unset, but commonly overridden).","commonSituations":"Unreachable/slow host, firewall dropping SYN packets, DNS resolves to a dead IP, proxy connect delay, overloaded server backlog, or an aggressively small sock_connect value.","solutions":["Raise sock_connect via ClientTimeout(sock_connect=N) to match real network latency.","Verify reachability: ping/telnet/curl to the host and port; check DNS with nslookup.","Retry with bounded backoff for transient congestion, and check proxy configuration if applicable."],"exampleFix":"# before\nasync with aiohttp.ClientSession() as s:\n    await s.get('https://slow.example')  # default/short sock_connect\n\n# after\ntimeout = aiohttp.ClientTimeout(sock_connect=30)\nasync with aiohttp.ClientSession(timeout=timeout) as s:\n    await s.get('https://slow.example')","handlingStrategy":"retry","validationCode":"timeout = aiohttp.ClientTimeout(sock_connect=30)\nasync with aiohttp.ClientSession(timeout=timeout) as s:\n    await s.get(url)","typeGuard":null,"tryCatchPattern":"try:\n    async with session.get(url) as r:\n        ...\nexcept aiohttp.ConnectionTimeoutError as exc:\n    # retry with backoff or report unreachable host\n    ...","preventionTips":["Set sock_connect to match real network latency","Verify DNS/reachability before retrying","Retry with bounded backoff for transient congestion"],"tags":["client","timeout","network","connection","configuration"],"analyzedSha":"c0ef574e29109210e96e652771ae4e7b88615fa4","analyzedAt":"2026-08-04T19:51:05.467Z","schemaVersion":2}