python / python/cpython

Data race in `faulthandler.enable()` and `faulthandler.disable()` with free-threading

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

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

extension-modules 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:

faulthandler.enable() and faulthandler.disable() read and write the global fatal_error state with no synchronization. enable() checks the enabled guard and then sets it,

https://github.com/python/cpython/blob/b18168cb32d545ed976b760983478cbd5dde5bdf/Modules/faulthandler.c#L536-L541

while disable() reads the same guard and tears the state back down, including Py_CLEAR(fatal_error.file),

https://github.com/python/cpython/blob/b18168cb32d545ed976b760983478cbd5dde5bdf/Modules/faulthandler.c#L641-L658

so two threads calling enable() and disable() concurrently race on the enabled flag, on the installed signal handlers, and on the fatal_error.file reference.

Reproducer:

import faulthandler, os
from threading import Thread, Event

def owner():
    f = open(os.devnull, 'w')
    for _ in range(200000):
        faulthandler.enable(file=f, all_threads=False)
        f.write('x')
        f.flush()
    f.close()

def toggler():
    for _ in range(200000):
        faulthandler.disable()

threads  = [Thread(target=owner)   for _ in range(4)]
threads += [Thread(target=toggler) for _ in range(4)]
for t in threads: t.start()
for t in threads: t.join()

With TSAN build, the owner thread's own f.write()/enable() raises ValueError: I/O operation on uninitialized object because a concurrent disable() dropped the last reference to f and finalized it.

TSAN Report:

WARNING: ThreadSanitizer: data race (pid=1671402)
  Write of size 4 at 0x555555e19478 by thread T5:
    #0 faulthandler_disable /cpython/./Modules/faulthandler.c:644:29 (python3.16t+0x57830e) 
    #1 faulthandler_disable_py_impl /cpython/./Modules/faulthandler.c:674:5  
    #2 faulthandler_disable_py /cpython/./Modules/clinic/faulthandler.c.h:299:12 
    #3 cfunction_vectorcall_NOARGS /cpython/Objects/methodobject.c:508:24  
    #4 _PyObject_VectorcallTstate /cpython/./Include/internal/pycore_call.h:144:11  
    #5 PyObject_Vectorcall /cpython/Objects/call.c:327:12 
...

  Previous read of size 4 at 0x555555e19478 by thread T4:
    #0 faulthandler_enable /cpython/./Modules/faulthandler.c:538:21  
    #1 faulthandler_py_enable_impl /cpython/./Modules/faulthandler.c:633:9 
    #2 faulthandler_py_enable /cpython/./Modules/clinic/faulthandler.c.h:278:20 
    #3 cfunction_vectorcall_FASTCALL_KEYWORDS /cpython/Objects/methodobject.c:465:24  
    #4 _PyObject_VectorcallTstate /cpython/./Include/internal/pycore_call.h:144:11  
    #5 PyObject_Vectorcall /cpython/Objects/call.c:327:12 
...

  Location is global '_PyRuntime' of size 405824 at 0x555555e16c80 

SUMMARY: ThreadSanitizer: data race /cpython/./Modules/faulthandler.c:644:29 in faulthandler_disable
==================
CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

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 trong Modules/faulthandler.c tại các dòng 536-541 của enable() và các dòng 641-658 của disable(), sau đó chạy trình tái hiện được cung cấp bằng bản build ThreadSanitizer. Theo dõi trạng thái enabled dùng chung, các trình xử lý tín hiệu và quyền sở hữu fatal_error.file; công việc được xem là hoàn tất khi các lệnh gọi enable()/disable() đồng thời không còn tạo ra race được báo cáo hoặc làm mất hiệu lực tệp của bên gọi.

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
operating-systems
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Ít trao đổi
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
46/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.