python / python/cpython

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

未關閉
#117,344 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

tests
主要語言
Python
星號
77.2k
分支
35.9k
PR 合併指標
PR 指標待擷取

描述

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

貢獻指南

開啟貢獻指南

從這裡開始

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

研究方向

從 Lib/test/test_concurrent_futures/test_thread_pool.py 中引用的重用測試以及 Lib/concurrent/futures/thread.py 中的 worker 生命週期程式碼開始;同時檢查對應的 ProcessPoolExecutor 路徑。確定這個競爭條件是否需要更強的測試同步,還是需要修改 executor,然後執行 concurrent-futures 測試套件,並確保 worker 重用在啟用 GIL 和停用 GIL 的執行過程中都可靠。

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

評估

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

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

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