python / python/cpython

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

Đang mở
#157,176 3 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

3.14 3.15 3.16 type-bug
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu với reproducer trong ASan/UBSan pydebug build, sau đó kiểm tra Objects/structseq.c và các đường dẫn cấp phát trong Objects/typeobject.c, Python/gc.c và Modules/timemodule.c. So sánh hành vi với PR được liên kết gh-157179. Được xem là hoàn tất khi chu kỳ không còn tạo ra các leak của LeakSanitizer trong quá trình tắt interpreter.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
backend
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Đặc tả rõ ràng
Mức phù hợp với người mới
25/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.