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.

Title: Coroutine hangs if it performs async operations when handling exception sent using throw()
Type: behavior Stage:
Components: asyncio Versions: Python 3.8, Python 3.7
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: asvetlov, chris.jerdonek, salgado, yselivanov
Priority: normal Keywords:

Created on 2020-04-02 07:07 by salgado, last changed 2022-04-11 14:59 by admin.

Messages (1)
msg365572 - (view) Author: Guilherme Salgado (salgado) * Date: 2020-04-02 07:07
A coroutine will hang forever if it that catches an exception sent via its throw() method and then makes async calls while handling that exception. The same coroutine will complete just fine if the exception is instead raised from within it. Here's a script that demonstrates that:

import asyncio
import sys

async def sleep_on_exc(inject):
    if inject:
        await asyncio.sleep(0.2)
        if not inject:
            print("Raising KeyboardInterrupt")
            raise KeyboardInterrupt()
    except KeyboardInterrupt:
        print("I'm not done yet")
        await asyncio.sleep(0.1)
        print("Now I'm done")

async def inject_exc(coro):
    await asyncio.sleep(0.1)
    print("Injecting KeyboardInterrupt")

coro = sleep_on_exc(sys.argv[1] == "inject")
loop = asyncio.get_event_loop()

$ python raise
Raising KeyboardInterrupt
I'm not done yet
Now I'm done

$ python inject
Injecting KeyboardInterrupt
I'm not done yet                     # It hangs forever here until you Ctrl-C
^CTraceback (most recent call last):
Date User Action Args
2022-04-11 14:59:28adminsetgithub: 84333
2020-05-17 11:27:49chris.jerdoneksetnosy: + chris.jerdonek
2020-04-02 07:15:06salgadosetversions: + Python 3.8
2020-04-02 07:07:01salgadocreate