python / python/cpython

ProcessPoolExecutor: pending futures never complete if executor is gc'd while a max_tasks_per_child= worker exits

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

@gpshead がすでに取り組んでいます。

2026年7月3日 から。

stdlib topic-multiprocessing type-bug
主要言語
Python
スター
77.2k
フォーク
35.9k
PR マージ指標
PR 指標を取得中

説明

Bug report

Bug description:

If the last reference to a ProcessPoolExecutor is dropped without calling shutdown() while queued work remains, and a worker exits upon reaching its max_tasks_per_child limit, the remaining futures never complete and interpreter exit hangs joining the executor manager thread.

from concurrent.futures import ProcessPoolExecutor

def work(i):
    return i * i

def make_futures():
    exe = ProcessPoolExecutor(1, max_tasks_per_child=1)
    return [exe.submit(work, i) for i in range(4)]

if __name__ == "__main__":
    futs = make_futures()
    print([f.result(timeout=30) for f in futs])

Result: TimeoutError after 30 seconds, then the process hangs at exit.

The executor manager thread replaces a worker that exited at its task limit through a weakref to the executor:

if executor := self.executor_reference():
    if process_exited:
        with self.shutdown_lock:
            executor._replace_dead_worker()

Once the executor object has been collected, the weakref returns None and no replacement can ever be spawned. The manager thread then waits forever: pending work items remain (their calls are queued but no worker exists), so it never reaches the empty-pending exit condition, the futures are never resolved and never marked broken, and _python_exit() blocks joining the manager thread at interpreter exit.

Dropping an executor without shutdown() while holding its futures is otherwise supported: with max_tasks_per_child unset, the same program completes normally because the workers stay alive and drain the queue. Only the worker-exits-at-limit case strands work.

Suggested direction: when the manager observes a process exit while the executor weakref is dead, and no live workers remain while work items are pending, fail the pending work items with BrokenProcessPool instead of waiting forever.

Found independently by Claude Fable 5 while I had it test and update the gh-115634 fix (GH-140900).
This reproduces identically before and after that fix -- the replacement path cannot help because there is no executor left to spawn through. The relevant code shape is unchanged since max_tasks_per_child was added in 3.11.

See also #83386 for an older exit-hang of a different mechanism.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs
  • gh-152978
  • gh-153363

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

評価

この issue はまだ評価されていません。

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

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