TaskGroup: tg.cancel() shouldn't block create_task() allow cooperative cancellation
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Python
- Star
- 77.2k
- Fork
- 35.9k
- Chỉ số merge pull request
- Chỉ số pull request đang chờ
Mô tả
Currently, when a TaskGroup is explicitly cancelled via tg.cancel(), any subsequent call to tg.create_task() raises:
RuntimeError: TaskGroup <TaskGroup cancelling> is shutting down
This contradicts the TODO in the test test_taskgroup_cancel_before_create_task, which states:
This behavior is not ideal. We'd rather have no exception raised, and the child task run until the first await.
Proposed change:
After an explicit tg.cancel() (not after a failure-induced abort), create_task() should succeed. The new task should run its synchronous code up to the first await, then receive CancelledError
For failure-driven aborts (e.g., an exception in another task), the existing RuntimeError is preserved to prevent unsafe task creation during error cleanup.
A PR implementing this change (with a new _explicitly_cancelled flag) is ready and passes all existing tests.
This resolves the TODO and makes TaskGroup behavior more intuitive when voluntarily cancelled.
Linked PRs
- gh-150357
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu với TODO test_taskgroup_cancel_before_create_task và các entry point cancel/create_task của TaskGroup được mô tả trong issue. So sánh tg.cancel() tường minh với hành vi abort do lỗi kích hoạt. Hoàn thành khi một task được tạo sau khi hủy tường minh chạy qua mã đồng bộ của nó trước khi nhận CancelledError, còn các lần abort do lỗi kích hoạt vẫn raise RuntimeError.
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
- Tính năng
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 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