[Async-sig] task.result() and exception traceback display

Chris Jerdonek chris.jerdonek at gmail.com
Mon Dec 25 00:55:47 EST 2017


Hi,

I noticed that if a task in asyncio raises an exception, then the
displayed traceback can be "polluted" by intermediate calls to
task.result().  Also, the calls to task.result() can appear out of
order relative to each other and to other lines.

Here is an example:

    import asyncio

    async def raise_error():
        raise ValueError()

    async def main():
        task = asyncio.ensure_future(raise_error())

        try:
            await task  # call 1
        except Exception:
            pass

        try:
            task.result()  # call 2
        except Exception:
            pass

        task.result()  # call 3

    asyncio.get_event_loop().run_until_complete(main())

The above outputs--

    Traceback (most recent call last):
      File "test.py", line 24, in <module>
        asyncio.get_event_loop().run_until_complete(main())
      File "/Users/.../3.6.4rc1/lib/python3.6/asyncio/base_events.py",
          line 467, in run_until_complete
        return future.result()
      File "test.py", line 21, in main
        task.result()  # call 3
      File "test.py", line 17, in main
        task.result()  # call 2
      File "test.py", line 12, in main
        await task  # call 1
      File "test.py", line 5, in raise_error
        raise ValueError()
    ValueError

Notice that the "call 2" line appears in the traceback, even though it
doesn't come into play in the exception.  Also, the lines don't obey
the "most recent call last" rule.  If this rule were followed, it
should be something more like--

    Traceback (most recent call last):
      File "test.py", line 24, in <module>
        asyncio.get_event_loop().run_until_complete(main())
      File "/Users/.../3.6.4rc1/lib/python3.6/asyncio/base_events.py",
          line 467, in run_until_complete
        return future.result()
      File "test.py", line 12, in main
        await task  # call 1
      File "test.py", line 5, in raise_error
        raise ValueError()
      File "test.py", line 17, in main
        task.result()  # call 2
      File "test.py", line 21, in main
        task.result()  # call 3
    ValueError

If people agree there's an issue along these lines, I can file an
issue in the tracker. I didn't seem to find one when searching for
open issues with search terms like "asyncio traceback".

Thanks,
--Chris


More information about the Async-sig mailing list