{"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/d9aaf697c2cd4783ca5749a971965c689f3ec24f/aiohttp/client.py#L222-L258","documentation":"Raised in _connect_and_send_request (client.py:239-240) when the connector's connection attempt times out. connector.connect() raises asyncio.TimeoutError once the configured connect timeout elapses, and aiohttp wraps it as ConnectionTimeoutError('Connection timeout to host {url}'), a subclass of ServerTimeoutError/ClientError.","triggerScenarios":"The TCP (and TLS) handshake to the target host does not complete within ClientTimeout.connect (default 5 minutes, often overridden lower). The await connector.connect(...) inside _connect_and_send_request crosses the deadline.","commonSituations":"An unreachable/slow host, a firewall dropping SYN packets, a saturated connection pool, a misconfigured proxy, or connect timeout set too low for a high-latency link.","solutions":["Increase the connect timeout: ClientTimeout(connect=30) or pass timeout= to the request.","Verify reachability of the host/port (DNS resolves, firewall allows, service is up).","Retry the request with backoff for transient network issues.","Check proxy configuration (HTTPS_PROXY/HTTP_PROXY) and the connection pool limit."],"exampleFix":"// before\nasync with aiohttp.ClientSession() as s:\n    await s.get('https://slow.example.com')  # default connect timeout\n// after (explicit connect timeout + retry)\nimport asyncio\nfrom aiohttp import ClientTimeout\nasync with aiohttp.ClientSession(timeout=ClientTimeout(connect=30)) as s:\n    for attempt in range(3):\n        try:\n            return await s.get('https://slow.example.com')\n        except asyncio.TimeoutError:\n            await asyncio.sleep(2 ** attempt)","handlingStrategy":"retry","validationCode":"import socket\n# pre-flight reachability check (optional)\ntry:\n    socket.create_connection((host, port), timeout=5).close()\nexcept OSError:\n    skip_request()","typeGuard":null,"tryCatchPattern":"import asyncio\nfrom aiohttp import ClientTimeout, ConnectionTimeoutError\nfor attempt in range(3):\n    try:\n        return await session.get(url, timeout=ClientTimeout(connect=30))\n    except (ConnectionTimeoutError, asyncio.TimeoutError):\n        if attempt == 2:\n            raise\n        await asyncio.sleep(2 ** attempt)","preventionTips":["Set an explicit connect timeout via ClientTimeout(connect=...).","Verify DNS/firewall/service availability before relying on a host.","Retry with exponential backoff for transient slowness.","Check proxy env vars and pool limits."],"tags":["network","timeout","connection","client","retry"],"analyzedSha":"d9aaf697c2cd4783ca5749a971965c689f3ec24f","analyzedAt":"2026-08-06T21:30:48.638Z","schemaVersion":2},"datasetVersion":"2026-08-07T02:17:10.218Z"}