python / python/cpython

Data race on the GC debug flag (gc.set_debug/get_debug) in free-threading builds

Đang mở
#153,014 0 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 topic-free-threading 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

In a free-threading build (--disable-gil), the garbage collector debug flag
gcstate->debug is read and written without synchronisation:

  • Write: gc_set_debug_impl() (Modules/gcmodule.c) does a plain
    gcstate->debug = flags. Unlike gc_set_threshold_impl(), which runs its
    free-threading branch under _PyEval_StopTheWorld(), gc.set_debug() takes
    no lock and does not stop the world.
  • Read: gc_get_debug_impl() returns gcstate->debug directly, and the
    collector in Python/gc_free_threading.c reads gcstate->debug /
    interp->gc.debug in several places while walking the graph.

So one thread calling gc.set_debug() concurrently with another calling
gc.get_debug() or triggering a collection is an unsynchronised read/write of
the same int. The flag is only an int, so the effect stays benign at the
Python level, but it is undefined behaviour under C11 and ThreadSanitizer
reports it as a data race.

This is the same class of issue already fixed for sys dlopenflags
(gh-151644) and gc.get_stats() (gh-151646). gc.enable() / gc.disable()
in the same file already access gcstate->enabled atomically; the debug flag
was missed.

ThreadSanitizer output

Built with ./configure --with-thread-sanitizer --disable-gil and stressed
with concurrent gc.set_debug() / gc.get_debug() plus a thread churning
cyclic garbage so the collector runs:

WARNING: ThreadSanitizer: data race
  Write of size 4 at 0x...6c by thread T2:
    #0 gc_set_debug gcmodule.c.h:186
  Previous write of size 4 at 0x...6c by thread T1:
    #0 gc_set_debug gcmodule.c.h:186
  Location is global '_PyRuntime'

How to reproduce

  1. ./configure --with-thread-sanitizer --disable-gil && make
  2. Run a script that starts a few threads calling gc.set_debug(...) /
    gc.get_debug() in a loop, plus a thread that builds reference cycles and
    calls gc.collect().
  3. TSan reports the write/write race on gcstate->debug.

Suggested fix

Access the flag with FT_ATOMIC_STORE_INT_RELAXED / FT_ATOMIC_LOAD_INT_RELAXED
in gc_set_debug_impl() / gc_get_debug_impl() and in the collector reads,
matching how gcstate->enabled and dlopenflags are already handled. Relaxed
ordering is correct for an independent int flag. These wrappers compile to a
plain load/store in the default (GIL) build, so there is no change there.

Linked PRs
  • gh-153015

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

Trước tiên, hãy xem xét PR được liên kết gh-153015, sau đó kiểm tra gc_set_debug_impl() và gc_get_debug_impl() trong Modules/gcmodule.c cũng như các lần đọc gcstate-debug trong Python/gc_free_threading.c. Biên dịch lại với --with-thread-sanitizer --disable-gil và chạy trình tái hiện đồng thời của gc.set_debug(), gc.get_debug() và quá trình thu gom; hoàn tất khi race đã được báo cáo không còn xuất hiện.

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

Đánh giá

Công nghệ
c, python
Lĩnh vực
backend
Loại issue
Lỗi
Độ khó
3/5
Thời gian dự kiến
1-2 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.