python / python/cpython

Memory leak on interpreter shutdown when reference cycle exists between `structseq` type and its instance

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

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

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

説明

Bug report

Bug description:
Environment
  • Commit/Branch: main (rev: 2d9fb5f2f70b18ce9e5d9f7f76fd5ef3f118d838)
  • OS: Linux
  • Build flags:
    CC=clang CXX=clang++ LDFLAGS='-fuse-ld=lld' ./configure --with-address-sanitizer --with-undefined-behavior-sanitizer --with-pydebug && make -j$(nproc)
    
Steps to reproduce

Run:

./python -c "import time; t = time.gmtime(); type(t).refcyle = t;"

Note: Explicitly breaking the cycle avoids the leak:

./python -c "import time; t = time.gmtime(); type(t).refcyle = t; del type(t).refcyle"
Expected behavior

The garbage collector or interpreter finalization breaks the reference cycle during shutdown; no memory leaks reported by LeakSanitizer.

Actual behavior

LeakSanitizer detects memory leaks (indirect leaks of the type, its dict, descriptors, and the instance):

=================================================================
==1164172==ERROR: LeakSanitizer: detected memory leaks

Indirect leak of 1472 byte(s) in 1 object(s) allocated from:
    #0 0x55571ba2f651 in malloc
    #1 0x55571c0c8db7 in _PyMem_DebugRawAlloc Objects/obmalloc.c:3103:24
    #2 0x55571c130728 in _PyObject_MallocWithType Include/internal/pycore_object_alloc.h:46:17
    #3 0x55571c130728 in _PyType_AllocNoTrack Objects/typeobject.c:2495:19
    #4 0x55571c13040d in PyType_GenericAlloc Objects/typeobject.c:2526:21
    #5 0x55571c1357f9 in type_from_slots_or_spec Objects/typeobject.c:5591:30
    #6 0x55571c1148ec in _PyStructSequence_NewType Objects/structseq.c:780:28
    #7 0x55571ca6ea5c in time_exec Modules/timemodule.c:2113:31
...
Indirect leak of 176 byte(s) in 1 object(s) allocated from:
    #0 0x55571ba2f651 in malloc
    #1 0x55571c0c8db7 in _PyMem_DebugRawAlloc Objects/obmalloc.c:3103:24
    #2 0x55571c5d4028 in _PyObject_MallocWithType Include/internal/pycore_object_alloc.h:46:17
    #3 0x55571c5d4028 in gc_alloc Python/gc.c:2013:17
    #4 0x55571c5d4440 in _PyObject_GC_NewVar Python/gc.c:2055:25
    #5 0x55571c110247 in PyStructSequence_New Objects/structseq.c:77:11
    #6 0x55571ca69e1d in tmtotuple Modules/timemodule.c:463:19
    #7 0x55571ca67616 in time_gmtime Modules/timemodule.c:541:12
...
SUMMARY: AddressSanitizer: 6946 byte(s) leaked in 50 allocation(s).

Full LeakSanitizer output - logs.txt

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs
  • gh-157179

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

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

はじめの一歩

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

調査の方向性

ASan/UBSan pydebug build で reproducer から始め、Objects/structseq.c と、Objects/typeobject.c、Python/gc.c、Modules/timemodule.c の割り当てパスを調べます。リンクされた PR gh-157179 と動作を比較します。インタープリターのシャットダウン中に、そのサイクルによって LeakSanitizer のリークが発生しなくなれば完了です。

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

評価

技術スタック
python
領域
backend
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
25/100

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

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