python / python/cpython

[subinterpreters] Refleaks on Windows Under Specific Conditions

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

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

OS-windows pending topic-subinterpreters type-bug
主要言語
Python
スター
77.2k
フォーク
36k
PR マージ指標
PR 指標を取得中

説明

Bug report

Bug description:

One of my recent PRs, gh-117662, led to one of the Windows refleak buildbots failing^1. I'm fairly confident the PR revealed an existing source of refleaks (albeit an unlikely one), rather that introducing new leaks. I have resolved the failures with gh-117913, but the underlying potential source of refleaks remains.

I've been able to reproduce the leak conditions, but only on Windows (for now?). The following conditions are necessary:

  • interpreter created using a config with check_multi_interp_extensions=False (e.g. "legacy")
  • interpreter imports test.support.os_helper
try:
    import _interpreters
except ModuleNotFoundError:
    import _xxsubinterpreters as _interpreters
config = _interpreters.new_config('legacy')  # critically, check_multi_interp_extensions=False
interpid = _interpreters.create(config)
_interpreters.exec(interpid, 'import test.support.os_helper')
_interpreters.destroy(interpid)
# leaks a bunch of objects

When the bug is triggered, around 190 objects are leaked. At first I thought this was interpreter finalization failing silently, but now I think it is something else.

I suspect the underlying problem relates to legacy extension modules. In the specific example above, I'm pretty sure the _ctypes module is leaking and ~190 is how many objects it holds, directly or indirectly. The main clue there is that, when I run ./python -v ..., the _ctypes module is never noted as destroyed, whereas all other modules are. As to test.support.os_helper, _ctypes is one of the modules that gets indirectly imported. All that said, I haven't been able to reproduce the leak if the subinterpreter above imports _ctypes instead.

Why did we only see this on Windows? The only clue I can think of is that on Windows stdlib extension modules are all turned into builtin modules, IIRC. The logic of the check_multi_interp_extensions check (via _PyImport_CheckSubinterpIncompatibleExtensionAllowed() in Python/import.c) is specific to extension modules, not builtin modules.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Windows

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

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

はじめの一歩

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

調査の方向性

提供された _interpreters legacy-config スニペットを使って Windows のリークを再現し、次に Python/import.c の _PyImport_CheckSubinterpIncompatibleExtensionAllowed() 付近から調査を始めます。レポートに記載された詳細な import 出力を使って、test.support.os_helper と _ctypes のモジュールクリーンアップを比較します。reproducer がオブジェクトをリークしなくなり、関連する Windows の動作が適切な回帰テストでカバーされれば完了です。

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

評価

技術スタック
python
領域
operating-systems
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
38/100

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

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