Skip to content

Passing a an explicit loop to ClientSession raises an exception unless an explicit connector is provided #11147

Open
@Javalar

Description

@Javalar

Describe the bug

The loop parameter of the ClientSession class is not passed to the TCPConnector object it creates.

This causes the following cases to fail with an exception:

  • The loop parameter is set to a loop different from the current running loop.
    Raises: RuntimeError: Session and connector has to use same event loop
  • The ClientSession object is created outside of an event loop, but an explicit event loop is passed to it.
    Raises: RuntimeError: no running event loop

To Reproduce

1. Pass a loop other than the current running loop to the ClientSession.

from asyncio import new_event_loop

from aiohttp import ClientSession


async def test():
    other_loop = new_event_loop()
    _ = ClientSession(loop=other_loop)


loop = new_event_loop()
loop.run_until_complete(test())
loop.close()

2. Create the ClientSession instance outside of the loop, but pass it explicitly:

from asyncio import new_event_loop

from aiohttp import ClientSession


loop = new_event_loop()
session = ClientSession(loop=loop)

Expected behavior

The ClientSession object is created successfully and the internal TCPConnector object uses the explicitly provided loop.

Logs/tracebacks

Case 1

Traceback (most recent call last):
  File "C:\Users\***\Documents\PyCharm\Test\test.py", line 12, in <module>
    loop.run_until_complete(test())
  File "C:\Program Files\Python312\Lib\asyncio\base_events.py", line 691, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "C:\Users\***\Documents\PyCharm\Test\test.py", line 8, in test
    _ = ClientSession(loop=other_loop)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\***\.virtualenvs\Test-hO5JO9V5\Lib\site-packages\aiohttp\client.py", line 368, in __init__
    raise RuntimeError("Session and connector has to use same event loop")
RuntimeError: Session and connector has to use same event loop

Case 2

Traceback (most recent call last):
  File "C:\Users\***\Documents\PyCharm\Test\test.py", line 7, in <module>
    session = ClientSession(loop=loop)
              ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\***\.virtualenvs\Test-hO5JO9V5\Lib\site-packages\aiohttp\client.py", line 365, in __init__
    connector = TCPConnector(ssl_shutdown_timeout=ssl_shutdown_timeout)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\***\.virtualenvs\Test-hO5JO9V5\Lib\site-packages\aiohttp\connector.py", line 918, in __init__
    super().__init__(
  File "C:\Users\***\.virtualenvs\Test-hO5JO9V5\Lib\site-packages\aiohttp\connector.py", line 288, in __init__
    loop = loop or asyncio.get_running_loop()
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: no running event loop

Python Version

3.12.10

aiohttp Version

3.12.9

multidict Version

6.1.0

propcache Version

0.2.0

yarl Version

1.17.1

OS

Not relevant.

Related component

Client

Additional context

Should be fixed by changing:

        if connector is None:
            connector = TCPConnector(ssl_shutdown_timeout=ssl_shutdown_timeout)

to:

        if connector is None:
            connector = TCPConnector(loop=loop, ssl_shutdown_timeout=ssl_shutdown_timeout)

Code of Conduct

  • I agree to follow the aio-libs Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions