{"record":{"id":"95d2e283c018ceee","repo":"aio-libs/aiohttp","slug":"connector-is-closed-95d2e2","errorCode":null,"errorMessage":"Connector is closed","messagePattern":"Connector is closed","errorType":"exception","errorClass":"ClientConnectionError","httpStatus":null,"severity":"error","filePath":"aiohttp/connector.py","lineNumber":1149,"sourceCode":"                raise InvalidUrlClientError(host, \"is not a canonical IPv4 address\")\n            return [\n                {\n                    \"hostname\": host,\n                    \"host\": host,\n                    \"port\": port,\n                    \"family\": self._family,\n                    \"proto\": 0,\n                    \"flags\": 0,\n                }\n            ]\n\n        if not self._use_dns_cache:\n            if traces:\n                for trace in traces:\n                    await trace.send_dns_resolvehost_start(host)\n\n            if self._closed:\n                raise ClientConnectionError(\"Connector is closed\")\n\n            res = await self._resolver.resolve(host, port, family=self._family)\n\n            if traces:\n                for trace in traces:\n                    await trace.send_dns_resolvehost_end(host)\n\n            return res\n\n        key = (host, port)\n        if key in self._cached_hosts and not self._cached_hosts.expired(key):\n            # get result early, before any await (#4014)\n            result = self._cached_hosts.next_addrs(key)\n\n            if traces:\n                for trace in traces:\n                    await trace.send_dns_cache_hit(host)\n            return result","sourceCodeStart":1131,"sourceCodeEnd":1167,"githubUrl":"https://github.com/aio-libs/aiohttp/blob/d041d4d0fd48c3f0832084d33be16cf1c4835f85/aiohttp/connector.py#L1131-L1167","documentation":"Raised in _resolve_host when use_dns_cache is False and self._closed is True at the moment a DNS resolve is attempted. It is the DNS-path counterpart of error 88: the connector was closed (or is closing) while a request still needs it, so rather than dispatch a resolve against a dead connector aiohttp raises ClientConnectionError immediately.","triggerScenarios":"Disabling DNS caching and then issuing a request after connector.close(); a session closed by an `async with` exit that still has a pending request; reconnect logic reusing a closed connector.","commonSituations":"use_dns_cache=False for low-latency clients combined with aggressive connector recycling; test teardown closing the connector before slow requests finish; microservices that pool connectors but close them on config reload.","solutions":["Check `if connector.closed` before each request and rebuild when closed.","Keep one long-lived connector/session instead of opening and closing per cycle.","Await all in-flight requests before closing the connector.","When use_dns_cache=False, prefer creating a fresh connector per logical session."],"exampleFix":"# before\nconnector = aiohttp.TCPConnector(use_dns_cache=False)\n# ... later, after connector.close()\nawait session.get(url)\n# after\nif connector.closed:\n    connector = aiohttp.TCPConnector(use_dns_cache=False)\n    session = aiohttp.ClientSession(connector=connector)\nawait session.get(url)","handlingStrategy":"try-catch","validationCode":"async def resolve_or_rebuild(connector, session, host, port):\n    if connector.closed:\n        raise aiohttp.ClientConnectionError('Connector is closed; rebuild before retry')\n    return await connector._resolve_host(host, port)  # or issue the request instead","typeGuard":"null","tryCatchPattern":"try:\n    resp = await session.get(url)\nexcept aiohttp.ClientConnectionError as exc:\n    if 'Connector is closed' in str(exc):\n        connector = aiohttp.TCPConnector(use_dns_cache=False)\n        session = aiohttp.ClientSession(connector=connector)\n        resp = await session.get(url)\n    else:\n        raise","preventionTips":["Check `connector.closed` before each request when reuse patterns are unpredictable.","Prefer one long-lived connector rather than recycling with use_dns_cache=False.","Await all in-flight requests before closing the connector or session."],"tags":["connector","lifecycle","dns","client-connection-error"],"backgroundTag":null,"analyzedSha":"d041d4d0fd48c3f0832084d33be16cf1c4835f85","analyzedAt":"2026-08-11T20:44:15.550Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}