python / python/cpython

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

Đang mở
#97,907 5 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

topic-asyncio type-bug
Ngôn ngữ chính
Python
Star
77.2k
Fork
36k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu bằng cách chạy hai trường hợp asyncio.gather trong issue, sau đó kiểm tra Lib/asyncio/tasks.py quanh dòng 812, nơi issue xác định việc chọn future phụ thuộc vào thứ tự. Hoàn tất khi hành vi của traceback hủy và mô tả không còn phụ thuộc vào thứ tự của các đối số gather; hãy xem xét PR được liên kết gh-133243 trước khi bắt đầu.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
backend
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Đặc tả rõ ràng
Mức phù hợp với người mới
25/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.