aio-libs / aiohttp

Asynchronous HTTP client/server framework for asyncio and Python
https://docs.aiohttp.org
Other
14.95k stars 1.99k forks source link

Using eager task factory with aiohttp 3.10.0 and cpython 3.12+ raise an AssertionError in stdlib `asyncio.staggered` #8599

Open 2-5 opened 1 month ago

2-5 commented 1 month ago

Describe the bug

discord.py 2.4.0 throws exception after upgrading aiohttp to 3.10.0:

To Reproduce

  1. use discord.py 2.4.0

Expected behavior

no exception

Logs/tracebacks

Traceback (most recent call last):
  File ".venv/lib/python3.12/site-packages/discord/client.py", line 786, in start
    await self.login(token)
  File ".venv/lib/python3.12/site-packages/discord/client.py", line 620, in login
    data = await self.http.static_login(token)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.12/site-packages/discord/http.py", line 816, in static_login
    data = await self.request(Route('GET', '/users/@me'))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.12/site-packages/discord/http.py", line 638, in request
    async with self.__session.request(method, url, **kwargs) as response:
  File ".venv/lib/python3.12/site-packages/aiohttp/client.py", line 1344, in __aenter__
    self._resp = await self._coro
                 ^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.12/site-packages/aiohttp/client.py", line 648, in _request
    conn = await self._connector.connect(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.12/site-packages/aiohttp/connector.py", line 546, in connect
    proto = await self._create_connection(req, traces, timeout)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.12/site-packages/aiohttp/connector.py", line 954, in _create_connection
    _, proto = await self._create_direct_connection(req, traces, timeout)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.12/site-packages/aiohttp/connector.py", line 1282, in _create_direct_connection
    transp, proto = await self._wrap_create_connection(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.12/site-packages/aiohttp/connector.py", line 1036, in _wrap_create_connection
    sock = await aiohappyeyeballs.start_connection(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.12/site-packages/aiohappyeyeballs/impl.py", line 89, in start_connection
    sock, _, _ = await staggered.staggered_race(
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/asyncio/staggered.py", line 144, in staggered_race
    raise d.exception()
  File "/usr/lib/python3.12/asyncio/staggered.py", line 101, in run_one_coro
    assert len(running_tasks) == this_index + 2
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError

Python Version

$ python --version
Python 3.12.4

aiohttp Version

$ python -m pip show aiohttp
Name:                  aiohttp                                                                            
Latest version:        3.10.0                                                                             
Latest stable version: 3.10.0                                                                             
Installed version:     3.10.0                                                                             
Summary:               Async http client/server framework (asyncio)                                       
Requires Python:       >=3.8                                                                              
Author:                                                                                                   
Author email:                                                                                             
License:               Apache 2                                                                           
Homepage:              https://github.com/aio-libs/aiohttp                                                
Project URLs:          Chat: Matrix: https://matrix.to/#/#aio-libs:matrix.org                             
                       Chat: Matrix Space: https://matrix.to/#/#aio-libs-space:matrix.org                 
                       CI: GitHub Actions: https://github.com/aio-libs/aiohttp/actions?query=workflow%3ACI
                       Coverage: codecov: https://codecov.io/github/aio-libs/aiohttp                      
                       Docs: Changelog: https://docs.aiohttp.org/en/stable/changes.html                   
                       Docs: RTD: https://docs.aiohttp.org                                                
                       GitHub: issues: https://github.com/aio-libs/aiohttp/issues                         
                       GitHub: repo: https://github.com/aio-libs/aiohttp                                  
Platform:                                                                                                 
Keywords:

multidict Version

$ python -m pip show multidict
Name:                  multidict                                                      
Latest version:        6.0.5                                                          
Latest stable version: 6.0.5                                                          
Installed version:     6.0.5                                                          
Summary:               multidict implementation                                       
Requires Python:       >=3.7                                                          
Author:                Andrew Svetlov                                                 
Author email:          andrew.svetlov@gmail.com                                       
License:               Apache 2                                                       
Homepage:              https://github.com/aio-libs/multidict                          
Project URLs:          Chat: Gitter: https://gitter.im/aio-libs/Lobby                 
                       CI: GitHub: https://github.com/aio-libs/multidict/actions      
                       Coverage: codecov: https://codecov.io/github/aio-libs/multidict
                       Docs: RTD: https://multidict.aio-libs.org                      
                       GitHub: issues: https://github.com/aio-libs/multidict/issues   
                       GitHub: repo: https://github.com/aio-libs/multidict            
Platform:                                                                             
Keywords:

yarl Version

$ python -m pip show yarl
Name:                  yarl                                                                               
Latest version:        1.9.4                                                                              
Latest stable version: 1.9.4                                                                              
Installed version:     1.9.4                                                                              
Summary:               Yet another URL library                                                            
Requires Python:       >=3.7                                                                              
Author:                Andrew Svetlov                                                                     
Author email:          andrew.svetlov@gmail.com                                                           
License:               Apache-2.0                                                                         
Homepage:              https://github.com/aio-libs/yarl                                                   
Project URLs:          Chat: Matrix: https://matrix.to/#/#aio-libs:matrix.org                             
                       Chat: Matrix Space: https://matrix.to/#/#aio-libs-space:matrix.org                 
                       CI: GitHub Workflows: https://github.com/aio-libs/yarl/actions?query=branch:master 
                       Code of Conduct: https://github.com/aio-libs/.github/blob/master/CODE_OF_CONDUCT.md
                       Coverage: codecov: https://codecov.io/github/aio-libs/yarl                         
                       Docs: Changelog: https://yarl.aio-libs.org/en/latest/changes/                      
                       Docs: RTD: https://yarl.aio-libs.org                                               
                       GitHub: issues: https://github.com/aio-libs/yarl/issues                            
                       GitHub: repo: https://github.com/aio-libs/yarl                                     
Platform:                                                                                                 
Keywords:              cython, cext, yarl

OS

Ubuntu 22.04

Related component

Client

Additional context

No response

Code of Conduct

bdraco commented 1 month ago

This looks like a bug in stdlib asyncio.staggered

Have you had a chance to look through python’s issue tracker?

bdraco commented 1 month ago

https://github.com/python/cpython/blob/1573d90ce17f27fd30a251de897a35bf598d2655/Lib/asyncio/staggered.py#L90

bdraco commented 1 month ago

Are you by chance using an eager task factory?

2-5 commented 1 month ago

Yes, I'm using eager task factory:

loop = asyncio.get_running_loop()
loop.set_task_factory(asyncio.eager_task_factory)

aiohttp 3.9.5 worked fine with the same Python 3.12 and the eager task factory, 3.10 is triggering the bug.

bdraco commented 1 month ago

It looks like asyncio.staggered is broken with eager task factory because it assumes the task will not run immediately. Would you please open a bug report for cpython?

bdraco commented 1 month ago

A workaround would be to turn off happyeyeballs in the connector until cpython is fixed when using eager task factory.

happy_eyeballs_delay can be set to None on the connector to disable happyeyeballs which will workaround the bug in cpython asyncio.staggered when using it with eager task factory in the mean time.

bdraco commented 1 month ago

@2-5 I'd like to get this fixed in upstream cpython. Would you please open an issue there?

yuvalshi0 commented 1 month ago

We seem to have a somewhat related issue, only on python 3.11.9 (aiohttp 3.10.1)

Traceback (most recent call last):
File "/usr/local/lib/python3.11/site-packages/aiobotocore/httpsession.py", line 208, in send
response = await self._session.request(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/aiohttp/client.py", line 648, in _request
conn = await self._connector.connect(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/aiohttp/connector.py", line 546, in connect
proto = await self._create_connection(req, traces, timeout)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/aiohttp/connector.py", line 954, in _create_connection
_, proto = await self._create_direct_connection(req, traces, timeout)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/aiohttp/connector.py", line 1282, in _create_direct_connection
transp, proto = await self._wrap_create_connection(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/aiohttp/connector.py", line 1036, in _wrap_create_connection
sock = await aiohappyeyeballs.start_connection(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/aiohappyeyeballs/impl.py", line 89, in start_connection
sock, _, _ = await staggered.staggered_race(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/asyncio/staggered.py", line 144, in staggered_race
raise d.exception()
File "/usr/local/lib/python3.11/asyncio/staggered.py", line 116, in run_one_coro
assert winner_index is None
^^^^^^^^^^^^^^^^^^^^
AssertionError
2-5 commented 1 month ago

@bdraco Sorry, I wouldn't know how to report this bug upstream. I don't know what a staggered_race is, why what we see here is a Python bug and not an aiohttp one, nor what is the expected behavior.