python / python/cpython

Data race reading `_thread.RLock` recursion count in `repr()` under free-threading

未关闭
#154,928 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

extension-modules type-bug
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

Bug report

Bug description:

This is a follow-up to #153292 (data race in repr() of _thread.RLock), which was fixed by making rlock_repr read the lock's owner with an atomic load. The fix covered the owner (self->lock.thread) field, but rlock_repr still reads self->lock.level with a plain (non-atomic) load to compute the recursion count:

https://github.com/python/cpython/blob/22a6c51c94a4fde986b8964f1d36d5ec3ac20dcc/Modules/_threadmodule.c#L1287-L1305

self->lock.level is written by acquire / release / _acquire_restore, e.g.:

https://github.com/python/cpython/blob/22a6c51c94a4fde986b8964f1d36d5ec3ac20dcc/Modules/_threadmodule.c#L1198-L1210

So on a free-threaded build, repr(rlock) concurrent with acquire() or release() is still a data race, now on the level field rather than the owner field the earlier fix addressed.

Reproducer:

import _thread
from threading import Thread, Barrier

shared_rlock = _thread.RLock()
state = (5, _thread.get_ident())

def chain1_thread():
    for _ in range(20000):
        try:
            shared_rlock._acquire_restore(state)
        except Exception:
            pass

def chain2_thread():
    for _ in range(20000):
        try:
            repr(shared_rlock)
        except Exception:
            pass

N_C1 = 2
N_C2 = 4
barrier = Barrier(N_C1 + N_C2)

def _c1():
    barrier.wait()
    chain1_thread()

def _c2():
    barrier.wait()
    chain2_thread()

threads  = [Thread(target=_c1) for _ in range(N_C1)]
threads += [Thread(target=_c2) for _ in range(N_C2)]
for t in threads: t.start()
for t in threads: t.join()

TSAN Report :

==================
WARNING: ThreadSanitizer: data race (pid=655652)
  Read of size 8 at 0x7fffb6610540 by thread T6:
    #0 rlock_repr /cpython/./Modules/_threadmodule.c:1295:28 
    #1 PyObject_Repr /cpython/Objects/object.c:784:11 
    #2 builtin_repr /cpython/Python/bltinmodule.c:2677:12 
    #3 _PyEval_EvalFrameDefault /cpython/Python/generated_cases.c.h:2712:35  

  Previous write of size 8 at 0x7fffb6610540 by thread T1:
    #0 _thread_RLock__acquire_restore_impl /cpython/./Modules/_threadmodule.c:1210:22 
    #1 _thread_RLock__acquire_restore /cpython/./Modules/clinic/_threadmodule.c.h:537:20 
    #2 method_vectorcall_O /cpython/Objects/descrobject.c:476:24 
    #3 _PyObject_VectorcallTstate /cpython/./Include/internal/pycore_call.h:144:11 
    #4 PyObject_Vectorcall /cpython/Objects/call.c:327:12 
    #5 _Py_VectorCallInstrumentation_StackRefSteal /cpython/Python/ceval.c:768:11 
    #6 _PyEval_EvalFrameDefault /cpython/Python/generated_cases.c.h:1906:35

SUMMARY: ThreadSanitizer: data race /cpython/./Modules/_threadmodule.c:1295:28 in rlock_repr
==================
CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs
  • gh-155381

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 Modules/_threadmodule.c 中的 rlock_repr 以及报告中描述的 acquire、release 和 _acquire_restore 实现开始。使用 ThreadSanitizer 在 free-threaded 构建上运行提供的 reproducer,然后检查相关测试,并验证并发的 repr() 和锁更新不再报告 race。

由索引模型根据 Issue 内容生成。

评估

技术栈
c, python
领域
operating-systems
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
描述清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。