python / python/cpython

Add a typed exception for concurrent.futures executor shutdown

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

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

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

説明

Proposal:

concurrent.futures.Executor.submit() implementations currently raise a generic RuntimeError when work is submitted after executor shutdown. For example, ThreadPoolExecutor.submit() raises one of these messages:

RuntimeError("cannot schedule new futures after shutdown")
RuntimeError("cannot schedule new futures after interpreter shutdown")

This makes it difficult for callers to distinguish expected executor lifecycle failures from unrelated RuntimeErrors without matching exception message strings:

try:
    future = executor.submit(fn)
except RuntimeError as exc:
    if str(exc) in {
        "cannot schedule new futures after shutdown",
        "cannot schedule new futures after interpreter shutdown",
    }:
        handle_executor_shutdown(exc)
    else:
        raise

It would be useful to expose a typed exception for this condition, for example:

class ExecutorShutdownError(RuntimeError):
    pass

Then executor implementations could raise ExecutorShutdownError for explicit shutdown and interpreter-shutdown submission failures. Because it would subclass RuntimeError, existing callers catching RuntimeError would remain compatible, while callers that need precise handling could avoid brittle string matching:

try:
    future = executor.submit(fn)
except concurrent.futures.ExecutorShutdownError:
    handle_executor_shutdown()

This would mirror the existing benefit of typed executor failures such as BrokenExecutor, where callers can distinguish executor state from arbitrary runtime failures without inspecting exception text.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

None. I searched existing CPython issues for ExecutorShutdownError, cannot schedule new futures after shutdown, cannot schedule new futures after interpreter shutdown, and related concurrent.futures RuntimeError shutdown exception type wording. I found issues about shutdown behavior itself, but not this typed-exception API request.

Linked PRs
  • gh-153003

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

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

はじめの一歩

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

調査の方向性

まず concurrent.futures.Executor.submit() の実装と既存の BrokenExecutor API を読みます。リンクされた PR gh-153003 と関連するテストを確認します。完了条件は、シャットダウン中の submit が専用の例外を公開しつつ RuntimeError との互換性を維持し、明示的なシャットダウンとインタープリターのシャットダウンのケースをカバーすることです。

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

評価

技術スタック
python
領域
backend-api-design
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

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

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