Skip to content

Commit fc9b720

Browse files
[PR #11198/b151d3fc backport][3.12] Fix auto-created TCPConnector not using session's event loop (#11199)
Co-authored-by: J. Nick Koston <[email protected]> Fixes #11147
1 parent 2d4a28b commit fc9b720

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

CHANGES/11147.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed auto-created :py:class:`~aiohttp.TCPConnector` not using the session's event loop when :py:class:`~aiohttp.ClientSession` is created without an explicit connector -- by :user:`bdraco`.

aiohttp/client.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,9 @@ def __init__(
369369
)
370370

371371
if connector is None:
372-
connector = TCPConnector(ssl_shutdown_timeout=ssl_shutdown_timeout)
372+
connector = TCPConnector(
373+
loop=loop, ssl_shutdown_timeout=ssl_shutdown_timeout
374+
)
373375

374376
if connector._loop is not loop:
375377
raise RuntimeError("Session and connector has to use same event loop")

tests/test_client_session.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,26 @@ async def make_sess():
422422
another_loop.run_until_complete(connector.close())
423423

424424

425+
def test_auto_created_connector_uses_session_loop(
426+
loop: asyncio.AbstractEventLoop,
427+
) -> None:
428+
"""Test that auto-created TCPConnector uses the session's loop."""
429+
# Create a ClientSession without providing a connector
430+
# The session should auto-create a TCPConnector with the provided loop
431+
session = ClientSession(loop=loop)
432+
433+
# Verify the connector was created
434+
assert session.connector is not None
435+
assert isinstance(session.connector, TCPConnector)
436+
437+
# Verify the connector uses the same loop as the session
438+
assert session.connector._loop is loop
439+
assert session.connector._loop is session._loop
440+
441+
# Clean up
442+
loop.run_until_complete(session.close())
443+
444+
425445
def test_detach(loop, session) -> None:
426446
conn = session.connector
427447
try:

0 commit comments

Comments
 (0)