python / python/cpython

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

オープン
#117,344 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

tests
主要言語
Python
スター
77.2k
フォーク
36k
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. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

Lib/test/test_concurrent_futures/test_thread_pool.py にある、引用されている再利用テストと、Lib/concurrent/futures/thread.py にあるワーカーのライフサイクルコードから始め、対応する ProcessPoolExecutor のパスも調べます。この競合状態に、より強いテスト同期が必要なのか、それとも executor の変更が必要なのかを判断し、その後 concurrent-futures テストスイートを実行して、GIL が有効な実行と GIL が無効な実行の両方でワーカーの再利用が確実に機能することを確認します。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
backend, distributed-systems, testing-qa
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
説明が足りない
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。