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.

Author Amos.Anderson
Recipients Amos.Anderson, asvetlov, yselivanov
Date 2021-11-24.20:35:15
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1637786115.42.0.981078468976.issue45894@roundup.psfhosted.org>
In-reply-to
Content
I found a case where an exception is lost if the loop is stopped in a `finally`.


```
import asyncio
import logging


logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger()


async def method_that_raises():
    loop = asyncio.get_event_loop()
    try:
        logger.info("raising exception")
        raise ValueError("my exception1")
    # except Exception as e:
    #     logger.info("in catcher")
    #     logger.exception(e)
    #     raise
    finally:
        logger.info("stopped")
        loop.stop()
        # await asyncio.sleep(0.5)

        return


async def another_level():
    try:
        await method_that_raises()
    except Exception as e:
        logger.info("trapping from another_level")
        logger.exception(e)


if __name__ == "__main__":
    logger.info("start")
    try:
        asyncio.run(another_level())
    except Exception as e:
        logger.exception(e)
    logger.info("done")
```

gives this output in python 3.10.0 and 3.8.10 (tested in Ubuntu Windows Subsystem Linux) and 3.8.11 in Windows:

```
INFO:root:start
DEBUG:asyncio:Using selector: EpollSelector
INFO:root:raising exception
INFO:root:stopped
INFO:root:done
```
i.e., no evidence an exception was raised (other than the log message included to prove one was raised)

If I remove the `return`, then the exception propagates as expected.

I believe the exception should be propagated regardless of whether there's a `return` in the `finally` block.
History
Date User Action Args
2021-11-24 20:35:15Amos.Andersonsetrecipients: + Amos.Anderson, asvetlov, yselivanov
2021-11-24 20:35:15Amos.Andersonsetmessageid: <1637786115.42.0.981078468976.issue45894@roundup.psfhosted.org>
2021-11-24 20:35:15Amos.Andersonlinkissue45894 messages
2021-11-24 20:35:15Amos.Andersoncreate