[issue40466] asyncio.ensure_future() breaks implicit exception chaining
Chris Jerdonek
report at bugs.python.org
Fri May 1 05:57:11 EDT 2020
New submission from Chris Jerdonek <chris.jerdonek at gmail.com>:
This issue is about how if a coroutine is wrapped in a Task with asyncio.ensure_future(), then portions of the exception chain can be lost.
Specifically, if you run the following code, then ValueError will be raised with exc.__context__ equal to the KeyError:
import asyncio
async def raise_error():
raise ValueError
async def main():
try:
raise KeyError
except Exception as exc:
future = raise_error()
# Uncommenting the next line makes exc.__context__ None below.
# future = asyncio.ensure_future(future)
try:
await future
except Exception as exc:
print(f'error: {exc!r}, context: {exc.__context__!r}')
raise
asyncio.get_event_loop().run_until_complete(main())
However, if you uncomment the `asyncio.ensure_future()` line, then the ValueError will be raised with no __context__.
I originally raised this issue a couple years ago here:
https://mail.python.org/pipermail/async-sig/2017-November/000403.html
There it was suggested that this was a special case of this issue:
https://bugs.python.org/issue29587
However, after writing code to fix that, this issue still exists.
----------
components: asyncio
messages: 367832
nosy: asvetlov, chris.jerdonek, yselivanov
priority: normal
severity: normal
status: open
title: asyncio.ensure_future() breaks implicit exception chaining
type: behavior
versions: Python 3.6, Python 3.7, Python 3.8, Python 3.9
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue40466>
_______________________________________
More information about the Python-bugs-list
mailing list