python / python/cpython

[Free-threading] `assert(!_Py_IsImmortal(op))` failure when BRC merge queue races with `sys.intern`

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

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

interpreter-core 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

Bug description:

In the free-threaded build, there is a race condition where an object can be queued for Biased Reference Counting (BRC) merging, but become immortal before the queue is processed. This causes a fatal C-level assertion failure:

assert.h assertion failed at Objects/object.c:467 in Py_ssize_t _Py_ExplicitMergeRefcount(PyObject *, Py_ssize_t): !_Py_IsImmortal(op)

*** Check failure stack trace: ***
    @     _Py_ExplicitMergeRefcount
    @     merge_queued_objects
    @     _Py_brc_merge_refcounts
    @     _Py_HandlePending
    @     _PyEval_EvalFrameDefault

Reproduction. Fails when running under ASAN/TSAN and the free-threading build:

import threading
import sys
def test_race():
    # 1 million strings will trap Thread A inside map() for milliseconds,
    # giving Thread B plenty of time to queue references to strings that 
    # Thread A hasn't interned yet.
    strings = ["race_string_massive_" + str(j) for j in range(1000000)]
    
    # Give Thread B a copy of the list.
    shared = list(strings)
    
    def thread_b_func():
        # Thread B clears its list, reducing the shared refcount to 0.
        # Since Thread B is not the owner, they are placed in Thread A's merge queue.
        shared.clear()
    tb = threading.Thread(target=thread_b_func)
    tb.start()
    # Thread A executes entirely in C for milliseconds.
    # Strings are queued by Thread B, AND THEN made immortal by sys.intern!
    list(map(sys.intern, strings))
    tb.join()
if __name__ == "__main__":
    test_race()
CPython versions tested on:

3.14

Operating systems tested on:

Linux

Linked PRs
  • gh-152277

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 Objects/object.c tại _Py_ExplicitMergeRefcount và lần theo đường dẫn gọi được đề cập của merge_queued_objects, _Py_brc_merge_refcounts và _Py_HandlePending. Tái hiện race condition bằng ASAN hoặc TSAN với bản build free-threaded, sau đó so sánh hành vi và cách giải quyết trong PR được liên kết gh-152277.

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ó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
30/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.