This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: aexit called after loop close
Type: Stage:
Components: asyncio Versions: Python 3.8, Python 3.7, Python 3.6
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: asvetlov, pdxjohnny, yselivanov
Priority: normal Keywords:

Created on 2018-10-16 15:49 by pdxjohnny, last changed 2022-04-11 14:59 by admin.

Files
File name Uploaded Description Edit
test.py pdxjohnny, 2018-10-16 15:49
Messages (1)
msg327839 - (view) Author: John Andersen (pdxjohnny) * Date: 2018-10-16 15:49
aexit called after loop close on death by signal.

This seems odd, the __aexit__ method must be running in a loop because
it is an async function. However if one calls shield then it dies.

'''
$ python3.7 test.py
Hello!

# Ctrl-C before 5 seconds is up
$ python3.7 test.py
^CException ignored in: <coroutine object func at 0x7f0890f08148>
Traceback (most recent call last):
  File "test.py", line 18, in func
    await asyncio.sleep(5)
  File "test.py", line 14, in __aexit__
    await asyncio.shield(self.other())
  File "/usr/lib/python3.7/asyncio/tasks.py", line 765, in shield
    inner = ensure_future(arg, loop=loop)
  File "/usr/lib/python3.7/asyncio/tasks.py", line 577, in ensure_future
    task = loop.create_task(coro_or_future)
  File "/usr/lib/python3.7/asyncio/base_events.py", line 384, in create_task
    self._check_closed()
  File "/usr/lib/python3.7/asyncio/base_events.py", line 461, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
sys:1: RuntimeWarning: coroutine 'Test.other' was never awaited
'''
import os
import signal
import asyncio

class Test(object):

    async def other(self):
        print('Hello!')

    async def __aenter__(self):
        pass

    async def __aexit__(self, a, b, c):
        await asyncio.shield(self.other())

async def func():
    async with Test():
        await asyncio.sleep(5)

def main():
    loop = asyncio.get_event_loop()
    try:
        loop.run_until_complete(func())
    except KeyboardInterrupt:
        pass
    loop.run_until_complete(loop.shutdown_asyncgens())
    loop.close()

if __name__ == '__main__':
    main()
History
Date User Action Args
2022-04-11 14:59:07adminsetgithub: 79181
2018-10-16 15:49:58pdxjohnnysetfiles: + test.py
2018-10-16 15:49:11pdxjohnnycreate