Data race between `sys._current_exceptions()` and a concurrently attaching thread
Chưa có ai nhận issue này.
- 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 description:
In the free-threaded build, sys._current_exceptions() iterates every thread state under stop-the-world + HEAD_LOCK and reads t->thread_id.
But a newly started thread sets tstate->thread_id in bind_tstate() without holding HEAD_LOCK. Its PyThreadState is already on the interpreter's thread list by this point.
A thread that is still attaching is not yet stop-the-world-stoppable. _PyEval_StopTheWorldAll() therefore does not pause it, and its unlocked write to tstate->thread_id races the stop-the-world reader in _PyThread_CurrentExceptions.
Reproducer:
import sys
import threading
stop = threading.Event()
def reader(): # keep stop-the-world almost always active
for _ in range(20000):
sys._current_exceptions()
def churn(): # continuously attach new threads (bind_tstate)
for _ in range(20000):
t = threading.Thread(target=lambda: None)
t.start()
t.join()
readers = [threading.Thread(target=reader) for _ in range(8)]
churners = [threading.Thread(target=churn) for _ in range(4)]
for t in readers + churners: t.start()
for t in readers + churners: t.join()
TSAN Report:
WARNING: ThreadSanitizer: data race (pid=94157)
Read of size 8 at 0x0001081180b0 by thread T1:
#0 _PyThread_CurrentExceptions pystate.c:2817
#1 sys__current_exceptions sysmodule.c.h:1180
#2 cfunction_vectorcall_NOARGS methodobject.c:508
#3 PyObject_Vectorcall call.c:327
#4 _Py_VectorCallInstrumentation_StackRefSteal ceval.c:768
#5 _PyEval_EvalFrameDefault generated_cases.c.h:1906
...
Previous write of size 8 by thread T9:
#0 bind_tstate pystate.c:178
#1 _PyThreadState_Bind pystate.c:2660
#2 thread_run _threadmodule.c:384
#3 pythread_wrapper thread_pthread.h:234
SUMMARY: ThreadSanitizer: data race pystate.c:2817 in _PyThread_CurrentExceptions
CPython versions tested on:
CPython main branch
Operating systems tested on:
macOS
Linked PRs
- gh-152755
- gh-156202
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu với bind_tstate() và _PyThread_CurrentExceptions trong Python/pystate.c, sau đó xem lại các PR được liên kết gh-152755 và gh-156202. Chạy trình tái hiện free-threaded được cung cấp dưới ThreadSanitizer và xác minh rằng race condition được báo cáo giữa thao tác ghi và đọc thread_id đã được giải quyết mà không phát sinh lỗi mớ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
- Đình trệ
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức phù hợp với người mới
- 25/100