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 chris.jerdonek
Recipients carljm, chris.jerdonek, matrixise, socketpair, thehesiod, yselivanov
Date 2020-05-06.11:22:08
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1588764128.68.0.999672086231.issue31033@roundup.psfhosted.org>
In-reply-to
Content
I just posted a draft, proof-of-concept PR for one aspect of this issue: https://github.com/python/cpython/pull/19951

Namely, the PR makes it so that cancelled tasks have full tracebacks, all the way to where the code first gets interrupted. I did the C implementation, but I still need to do the pure Python implementation. (Note that it doesn't show where `task.cancel()` was *called*, but rather the first line of code that is cancelled. That other aspect can be handled in a separate PR.)

As an example, for this code--

    import asyncio

    async def nested():
        await asyncio.sleep(1)

    async def run():
        task = asyncio.create_task(nested())
        await asyncio.sleep(0)
        task.cancel()
        await task

    loop = asyncio.new_event_loop()
    try:
        loop.run_until_complete(run())
    finally:
        loop.close()

Python currently (before the PR) does this:

Traceback (most recent call last):
  File "/.../cpython/test-cancel.py", line 15, in <module>
    loop.run_until_complete(run())
  File "/.../cpython/Lib/asyncio/base_events.py", line 642, in run_until_complete
    return future.result()
asyncio.exceptions.CancelledError

With the PR, it looks like this. In particular, you can see that it includes "await asyncio.sleep(1)", as well as the intermediate await:

Traceback (most recent call last):
  File "/.../cpython/test-cancel.py", line 5, in nested
    await asyncio.sleep(1)
  File "/.../cpython/Lib/asyncio/tasks.py", line 657, in sleep
    return await future
asyncio.exceptions.CancelledError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/.../cpython/test-cancel.py", line 11, in run
    await task
asyncio.exceptions.CancelledError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/.../cpython/test-cancel.py", line 15, in <module>
    loop.run_until_complete(run())
  File "/.../cpython/Lib/asyncio/base_events.py", line 642, in run_until_complete
    return future.result()
asyncio.exceptions.CancelledError
History
Date User Action Args
2020-05-06 11:22:08chris.jerdoneksetrecipients: + chris.jerdonek, carljm, socketpair, yselivanov, matrixise, thehesiod
2020-05-06 11:22:08chris.jerdoneksetmessageid: <1588764128.68.0.999672086231.issue31033@roundup.psfhosted.org>
2020-05-06 11:22:08chris.jerdoneklinkissue31033 messages
2020-05-06 11:22:08chris.jerdonekcreate