python / python/cpython

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

未關閉
#97,907 5 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

topic-asyncio type-bug
主要語言
Python
星號
77.2k
分支
36k
PR 合併指標
PR 指標待擷取

描述

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

Linked PRs
  • gh-133243

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

先執行 issue 中的兩個 asyncio.gather 案例,然後檢查 Lib/asyncio/tasks.py 第 812 行附近,issue 在此處指出了依賴順序的 future 選取。完成的標準是:取消 traceback 和描述的行為不再取決於 gather 引數的順序;開始前請查看連結的 PR gh-133243。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
backend
Issue 類型
缺陷
難度
4/5
預估耗時
3-5 天
活躍度
停滯
描述清晰度
描述清楚
新手友好度
25/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。