[New-bugs-announce] [issue45894] exception lost when loop.stop() in finally
Amos Anderson
report at bugs.python.org
Wed Nov 24 15:35:15 EST 2021
New submission from Amos Anderson <nitroamos at gmail.com>:
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.
----------
components: asyncio
messages: 406957
nosy: Amos.Anderson, asvetlov, yselivanov
priority: normal
severity: normal
status: open
title: exception lost when loop.stop() in finally
type: behavior
versions: Python 3.10, Python 3.8
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue45894>
_______________________________________
More information about the New-bugs-announce
mailing list