New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Asyncio: GeneratorExit + strange exception #74269
Comments
How to reproduce: Run the following program: import asyncio
async def handle_connection(reader, writer):
try:
await reader.readexactly(42)
except BaseException as err:
print('Interesting: %r.' % err)
raise
finally:
writer.close()
loop = asyncio.get_event_loop()
coro = asyncio.start_server(handle_connection, '127.0.0.1', 8888)
server = loop.run_until_complete(coro)
try:
loop.run_forever()
except KeyboardInterrupt:
print('KeyboardInterrupt catched.')
server.close()
loop.run_until_complete(server.wait_closed())
loop.close() =========================
========================= ^CKeyboardInterrupt catched.
Interesting: GeneratorExit().
Exception ignored in: <coroutine object handle_connection at 0x7fa6a1d91b48>
Traceback (most recent call last):
File "bug.py", line 12, in handle_connection
writer.close()
File "/usr/lib/python3.5/asyncio/streams.py", line 306, in close
return self._transport.close()
File "/usr/lib/python3.5/asyncio/selector_events.py", line 591, in close
self._loop.call_soon(self._call_connection_lost, None)
File "/usr/lib/python3.5/asyncio/base_events.py", line 567, in call_soon
handle = self._call_soon(callback, args)
File "/usr/lib/python3.5/asyncio/base_events.py", line 576, in _call_soon
self._check_closed()
File "/usr/lib/python3.5/asyncio/base_events.py", line 356, in _check_closed
raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
Task was destroyed but it is pending!
task: <Task pending coro=<handle_connection() done, defined at bug.py:4> wait_for=<Future pending cb=[Task._wakeup()]>> ========================= This is almost canonical example of asyncio usage. So I have two questions:
===== |
I've also been affected by this and found that if you use asyncio.run() the coroutine is interrupted with a CancelledError as you'd expect. The following script shows that ======================= import asyncio
async def handle_connection(reader, writer):
try:
await reader.readexactly(42)
except BaseException as err:
print('Interesting: %r.' % err)
raise
finally:
writer.close()
async def main():
listener = await asyncio.start_server(handle_connection, '127.0.0.1', 8888)
try:
async with listener:
await listener.serve_forever()
except KeyboardInterrupt:
print('KeyboardInterrupt')
asyncio.run(main()) ============================================= |
Not able to reproduce on main and main has new signal handling which won't be backported so closing. |
The original example doesn't cause any errors for me, even on Python 3.8. But, we see a lot of these errors in aiohttp's test suite and have struggled to track them down: |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: