python / python/cpython

`concurrent.futures.Executor.map` with `buffersize` should yield from buffer and raise after executor shutdown

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

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

stdlib topic-multiprocessing type-bug
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ả

Bug report

Bug description:

As pointed by @salomvary in this discussion, the behavior of concurrent.futures.Executor.map with buffersize is misleading when iterating after the executor's shutdown. There is 2 cases:

  1. Create the generator, shut down the executor, start iterating:
with ThreadPoolExecutor(1) as executor:
    iterator = executor.map(str, range(8), buffersize=2)

# start iterating after the shutdown
assert next(iterator) == "0"
assert next(iterator) == "1"
# raises StopIteration
next(iterator)

In this scenario the elements from the buffer are yielded, then the iteration is stopped.

  1. Create the generator, start the iteration, shut down the executor, continue the iteration
with ThreadPoolExecutor(1) as executor:
    iterator = executor.map(str, range(8), buffersize=2)
    # start iterating before the shutdown
    assert next(iterator) == "0"

# raises "RuntimeError: cannot schedule new futures after shutdown"
next(iterator)

In this scenario a RuntimeError is raised by the first post-shutdown next, leaving not-yet-yielded results in the buffer.


I would vote for a mix of both: yield from the buffer and then raise the exception.

(the fix would be a dozen rows)

CPython versions tested on:

3.14, 3.15

Operating systems tested on:

macOS, Linux, Windows

Linked PRs
  • gh-146395

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

Tái hiện cả hai kịch bản shutdown trong issue bằng concurrent.futures.Executor.map và tham số buffersize của nó. Kiểm tra đường dẫn lặp của Executor.map, sau đó bổ sung coverage cho việc yield các kết quả đã được đệm trước exception sau shutdown và xác minh rằng các ví dụ hiện có hoạt động như mô tả.

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ó
3/5
Thời gian dự kiến
1-2 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
30/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.