Message 367832 - Python tracker

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.

Content
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.