python / python/cpython

Worker management race conditions in `ThreadPoolExecutor` and `ProcessPoolExecutor`

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

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

tests
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ả

We have tests that ThreadPoolExecutor and ProcessPoolExecutor reuse workers. The test wait for the previous task to complete before submitting the next task.

https://github.com/python/cpython/blob/29829b58a8328a7c2ccacaa74c1d7d120a5e5ca5/Lib/test/test_concurrent_futures/test_thread_pool.py#L44-L50

The problem is that the tasks are potentially completed before the thread or process is marked as idle. Calling Future.result() doesn't guarantee that there is an idle worker. This happens much more frequently with the GIL disabled, but it can happen with the GIL enabled too if you have an unlucky GIL switch.

https://github.com/python/cpython/blob/29829b58a8328a7c2ccacaa74c1d7d120a5e5ca5/Lib/concurrent/futures/thread.py#L81-L95

For example, you can reliably trigger the problem with the GIL enabled by adding a short time.sleep(0.01) immediately after the work_item.run() call in:
https://github.com/python/cpython/blob/29829b58a8328a7c2ccacaa74c1d7d120a5e5ca5/Lib/concurrent/futures/thread.py#L92

It's not clear to me if this is just a problem with the unit tests or an actual bug with the executor implementation.

Linked PRs
  • gh-117355

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 với các bài kiểm thử tái sử dụng được nêu trong Lib/test/test_concurrent_futures/test_thread_pool.py và mã vòng đời worker trong Lib/concurrent/futures/thread.py; đồng thời kiểm tra các nhánh tương ứng của ProcessPoolExecutor. Xác định xem điều kiện tranh chấp có yêu cầu đồng bộ hóa kiểm thử chặt chẽ hơn hay thay đổi executor, sau đó chạy bộ kiểm thử concurrent-futures và đảm bảo việc tái sử dụng worker hoạt động đáng tin cậy trong cả quá trình thực thi có bật GIL và quá trình thực thi tắt GIL.

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, distributed-systems, testing-qa
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ần làm rõ
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.