Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Occasional incorrect chaining of CancelledError when calling 'cancel' on result of 'asyncio.gather' #97907

Open
vladima opened this issue Oct 5, 2022 · 5 comments
Labels
topic-asyncio type-bug An unexpected behavior, bug, or error

Comments

@vladima
Copy link
Contributor

vladima commented Oct 5, 2022

import asyncio

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

async def f2():
    return 42

async def main():
    # case 1
    gfut = asyncio.gather(f1(), f2(), return_exceptions=True)
    # case 2
    #gfut = asyncio.gather(f2(), f1(), return_exceptions=True)
    await asyncio.sleep(0.1)
    gfut.cancel("my message")
    await gfut

asyncio.run(main())

# case 1

#Traceback (most recent call last):
#  File "/Users/vladima/Sources/gather.py", line 14, in main
#    print(await gfut)
#asyncio.exceptions.CancelledError
#
#During handling of the above exception, another exception occurred:
#
#Traceback (most recent call last):
#  File "/Users/vladima/Sources/gather.py", line 16, in <module>
#    asyncio.run(main())
#  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/runners.py", line 44, in run
#    return loop.run_until_complete(main)
#  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/base_events.py", line 646, in run_until_complete
#    return future.result()
#asyncio.exceptions.CancelledError

# case 2
#Traceback (most recent call last):
#  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/tasks.py", line 605, in sleep
#    return await future
#asyncio.exceptions.CancelledError: my message

#During handling of the above exception, another exception occurred:

#Traceback (most recent call last):
#  File "/Users/vladima/Sources/gather.py", line 14, in main
#    print(await gfut)
#asyncio.exceptions.CancelledError
#
#During handling of the above exception, another exception occurred:
#
#Traceback (most recent call last):
#  File "/Users/vladima/Sources/gather.py", line 16, in <module>
#    asyncio.run(main())
#  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/runners.py", line 44, in run
#    return loop.run_until_complete(main)
#  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/base_events.py", line 646, in run_until_complete
#    return future.result()
#asyncio.exceptions.CancelledError

Currently traceback and description parts of CancelledError depend upon the order of arguments that were passed into asyncio.gather. The reason I believe is this: here fut would be bound to the last item in children which might or might not correspond to the cancelled task and instead it should probably be one of tasks that were actually cancelled (which is still not ideal since there might be many of them but I guess with gather it is as good as it can be).

@vladima vladima added the type-bug An unexpected behavior, bug, or error label Oct 5, 2022
@ezio-melotti ezio-melotti moved this to Todo in asyncio Oct 13, 2022
@akulakov
Copy link
Contributor

Note that msg argument to cancel() is deprecated in 3.11 and will be removed in the future, so once that happens it will be just that traceback is different - not sure if that's an issue that needs a fix.

@gvanrossum
Copy link
Member

Actually on the final 3.11 release cancel(msg) will be undeprecated — we had a last minute change of heart. So if there’s an issue it may have to be addressed — in 3.12, probably.

@akulakov
Copy link
Contributor

@gvanrossum in that case should I remove the deprecation warning from latest dev branch, or that's already planned to be done by someone?

@gvanrossum
Copy link
Member

We already removed it.
#98006

@gvanrossum
Copy link
Member

On the original issue, I suppose we could do better -- a PR would be appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-asyncio type-bug An unexpected behavior, bug, or error
Projects
Status: Todo
Development

No branches or pull requests

4 participants