aio-libs / aiohttp

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

`maximum recursion depth exceeded while calling a Python object` when KeyboardInterrupt #4852

Closed eugene-kulak closed 2 months ago

eugene-kulak commented 4 years ago

🐞 Describe the bug

I'm trying to implement a graceful shutdown of HTTP server, but even smallest example has a problem with recursion.

πŸ’‘ To Reproduce

import asyncio
from aiohttp import web

async def do():
    http_app = web.Application()
    http_runner = web.AppRunner(http_app)
    await http_runner.setup()
    http_server = web.TCPSite(http_runner, host='', port=1234)
    await http_server.start()

    await asyncio.gather(*asyncio.all_tasks())

asyncio.run(do())

πŸ’‘ Expected behavior

Stack trace without second exception recursion depth exceeded

πŸ“‹ Logs/tracebacks

^CTraceback (most recent call last):
  File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/runners.py", line 43, in run
    return loop.run_until_complete(main)
  File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/base_events.py", line 574, in run_until_complete
    self.run_forever()
  File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/base_events.py", line 541, in run_forever
    self._run_once()
  File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/base_events.py", line 1750, in _run_once
    event_list = self._selector.select(timeout)
  File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/selectors.py", line 558, in select
    kev_list = self._selector.control(None, max_ev, timeout)
KeyboardInterrupt

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/keu/Library/Application Support/JetBrains/PyCharm2020.1/scratches/scratch_6.py", line 15, in <module>
    asyncio.run(do())
  File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/runners.py", line 46, in run
    _cancel_all_tasks(loop)
  File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/runners.py", line 59, in _cancel_all_tasks
    task.cancel()
  File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/tasks.py", line 651, in cancel
    if child.cancel():
  File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/tasks.py", line 651, in cancel
    if child.cancel():
  File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/tasks.py", line 651, in cancel
    if child.cancel():
  [Previous line repeated 329 more times]
RecursionError: maximum recursion depth exceeded while calling a Python object

πŸ“‹ Your version of the Python

$ python --version
Python 3.7.7

πŸ“‹ Your version of the aiohttp/yarl/multidict distributions

$ python -m pip show aiohttp
Name: aiohttp
Version: 3.6.2
Summary: Async http client/server framework (asyncio)
Home-page: https://github.com/aio-libs/aiohttp
Author: Nikolay Kim
Author-email: fafhrd91@gmail.com
License: Apache 2
Location: /Users/keu/work/panda/mesh_network_service/.env37/lib/python3.7/site-packages
Requires: attrs, yarl, multidict, async-timeout, chardet
Required-by: 
$ python -m pip show multidict
Name: multidict
Version: 4.7.6
Summary: multidict implementation
Home-page: https://github.com/aio-libs/multidict
Author: Andrew Svetlov
Author-email: andrew.svetlov@gmail.com
License: Apache 2
Location: /Users/keu/work/panda/mesh_network_service/.env37/lib/python3.7/site-packages
Requires: 
Required-by: yarl, aiohttp
$ python -m pip show yarl
Name: yarl
Version: 1.4.2
Summary: Yet another URL library
Home-page: https://github.com/aio-libs/yarl/
Author: Andrew Svetlov
Author-email: andrew.svetlov@gmail.com
License: Apache 2
Location: /Users/keu/work/panda/mesh_network_service/.env37/lib/python3.7/site-packages
Requires: idna, multidict
Required-by: aiohttp

πŸ“‹ Additional context

Dreamsorcerer commented 2 months ago

You're awaiting on all tasks, including the one running the code... So, yeah, recursion.