python / python/cpython

`arraymodule`, `_decimal`: possible lazy import caches reference leaks in free-threaded builds

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

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

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

説明

Bug description:

As pointed out by @htjworld in https://github.com/python/cpython/pull/148088#issuecomment-4196296604:

I was wondering about the lazy init pattern in a free-threaded build. If two threads enter build_struct_time concurrently with st->time_struct_time == NULL, both would call PyImport_ImportModuleAttrString and one overwrite would leave a reference with an incremented refcount that never gets decref'd.

I noticed arraymodule.c and _decimal.c have the same pattern, so maybe this is just accepted. But _asynciomodule.c initializes eagerly in module_init() which avoids this structurally. Was lazy chosen deliberately here over eager init in init_state()?

While I've addressed this in #148088, I think that this is a valid concern, and import caches should use CAS, just like:

https://github.com/python/cpython/blob/7e0a0be4097f9d29d66fe23f5af86f18a34ed7dd/Modules/_sre/sre.c#L1173-L1188

If there's interest, I can do it.

(It seems similar to #149000, so perhaps a more thorough check warranted.)

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

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

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

はじめの一歩

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

調査の方向性

まず、arraymodule.c と _decimal.c の遅延初期化を、_asynciomodule.c の先行初期化および Modules/_sre/sre.c の CAS パターンと比較します。free-threaded パスについて、同時初期化と参照リークを確認します。この作業は、影響を受ける import キャッシュがその並行性パターンの下で安全であり、関連する動作が検証によってカバーされれば完了です。

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

評価

技術スタック
c, python
領域
backend
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
静か
明瞭さ
おおむね明確
初心者へのやさしさ
48/100

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

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