Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions aiohttp/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -1005,10 +1005,10 @@ async def close(self, *, abort_ssl: bool = False) -> None:
- If ssl_shutdown_timeout=0: connections are aborted
- If ssl_shutdown_timeout>0: graceful shutdown is performed
"""
if self._resolver_owner:
await self._resolver.close()
# Use abort_ssl param if explicitly set, otherwise use ssl_shutdown_timeout default
await super().close(abort_ssl=abort_ssl or self._ssl_shutdown_timeout == 0)
if self._resolver_owner:
await self._resolver.close()

def _close_immediately(self, *, abort_ssl: bool = False) -> list[Awaitable[object]]:
for fut in chain.from_iterable(self._throttle_dns_futures.values()):
Expand Down
26 changes: 26 additions & 0 deletions tests/test_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -1652,6 +1652,32 @@ async def test_tcp_connector_close_resolver() -> None:
m_resolver.close.assert_awaited_once()


async def test_tcp_connector_close_with_in_flight_dns_resolution() -> None:
# Regression test for #12497: TCPConnector.close() race with in-flight DNS
async def on_dns_start(session, ctx, params):
await asyncio.sleep(0)

trace = aiohttp.TraceConfig()
trace.on_dns_resolvehost_start.append(on_dns_start)

connector = aiohttp.TCPConnector(use_dns_cache=False)
session = aiohttp.ClientSession(
trace_configs=[trace],
connector=connector,
)

# Create a task that will suspend in the trace callback
task = asyncio.create_task(session.ws_connect("wss://example.com"))
await asyncio.sleep(0)

# Close the session while the task is suspended
await session.close()

# The task should fail with ClientConnectionError, not AttributeError
with pytest.raises((aiohttp.ClientConnectionError, asyncio.CancelledError)):
await task


async def test_dns_error(make_client_request: _RequestMaker) -> None:
connector = aiohttp.TCPConnector()
with mock.patch.object(
Expand Down
Loading