Requiring A Current Thread State for PyThreadState_Clear(), etc. is Problematic
まだ誰も着手していません。
評価
調査の方向性
まず PyThreadState_Clear() と PyInterpreterState_Clear() を追跡し、gh-101308 でまとめられたライフサイクルのケースも含めて、現在のスレッド状態が必要となる箇所を特定します。完了の条件は、クリア用 API が GIL 要件を維持しつつ、無効化されるスレッド状態に依存せず、列挙されたライフサイクル不変条件も引き続き満たされることです。
索引モデルが issue の本文から書いたものです。
説明
In PyThreadState_Clear() and PyInterpreterState_Clear() the code expects a "current" thread state (i.e. set by PyThreadState_Swap()) to be set. Presumably this is for the following:
- to ensure the GIL is held
- to use for logging audit events (actually only needs the current interpreter?)
- to use in object finalizers for objects held by the target thread/interpreter state
For PyThreadState_Clear(), this is a problem when the current thread state is also the target thread state, since the function is progressively rendering the thread state unusable. For PyInterpreterState_Clear() the "current" thread state must belong to the target interpreter, but will be rendered unusable before it's potentially needed.
(FWIW, it isn't clear to me yet why we don't see frequent crashes from this already (at least when we're clearing the main interpreter during finalization). I do suspect that this contributes to some of the occasional CI failures we see relative to finalization.)
Ideally auditing and the various object finalizers could work without having a current thread state, especially in the case where the current thread state could only possibly be the one getting cleared. Could we split up PyThreadState so at least the problematic part is guaranteed not be used in PyThreadState_Clear() or PyInterpreterState_Clear()? Are there other complexities we have to deal with?
(Note that we'd still require that the GIL be held by the current OS thread when PyThreadState_Clear() or PyInterpreterState_Clear() are called.)
FTR, in gh-101308 I noted various cases where the lifecycle of thread states has an unexpected status. Pretty much all of them relate `PyThreadState_Clear()` requiring a current thread state.
Dealing with the main issue above would make it trivial to address the following:
- an "active" thread state (i.e. "current", holding the GIL) must be "bound" (to an OS thread) already
- a "bound" thread state must be "alive" (initialized but not finalizing)
- binding a thread state should be done separately from
PyThreadState_New() - the thread state passed to
PyThreadState_Clear()should no longer be "active" or "bound" - the current thread state must belong to the target interpreter of
PyThreadState_Clear() - the current thread state must belong to the target interpreter of
PyInterpreterState_Clear()(for the main interpreter it must be the main thread)
- 主要言語
- Python
- スター
- 77.2k
- フォーク
- 36k
- 平均マージ
- 1日 9時間
- マージ済み PR(30日)
- 558
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
python/cpython のほかの issue
-
docs pending
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
-
stdlib type-feature
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
-
stdlib type-feature
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
-
build type-bug
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
-
stdlib topic-email type-feature
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
PolicyEngine/policyengine-us#9559 ·
-
priority: p3
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
googleapis/librarian#7636 ·
-
from:qa priority:P2 reliability tech-debt
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
spec-kitty/spec-kitty#4874 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100