Requiring A Current Thread State for PyThreadState_Clear(), etc. is Problematic

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

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

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
25/100
issue の種類
バグ
明瞭さ
説明が足りない
活発さ
停滞
技術スタック
python
領域
backend

調査の方向性

まず PyThreadState_Clear() と PyInterpreterState_Clear() を追跡し、gh-101308 でまとめられたライフサイクルのケースも含めて、現在のスレッド状態が必要となる箇所を特定します。完了の条件は、クリア用 API が GIL 要件を維持しつつ、無効化されるスレッド状態に依存せず、列挙されたライフサイクル不変条件も引き続き満たされることです。

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

説明

3.12 interpreter-core topic-subinterpreters type-bug

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

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

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

はじめの一歩

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

python/cpython のほかの issue

python/cpython の issue をすべて見る

似ている issue

Python の issue をもっと見る

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

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