python / python/cpython

`tracemalloc` aborts when run from threads in no-gil

未关闭
#126,315 6 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

3.13 3.14 3.15 interpreter-core topic-free-threading type-crash
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

Crash report

What happened?

It's possible to get two different aborts from failed assertions, only with PYTHON_GIL=0, using the code below:

from threading import Thread
import _tracemalloc

alive = [
    Thread(target=_tracemalloc.start, args=(
        (memoryview(bytearray(b"abc\xe9\xff")),))),
    Thread(target=_tracemalloc.start, args=()),
    Thread(target=_tracemalloc.get_traceback_limit, args=()),
    Thread(target=_tracemalloc.start, args=()),
    Thread(target=_tracemalloc.stop, args=()),
    Thread(target=_tracemalloc.reset_peak, args=()),
    Thread(target=_tracemalloc.get_tracemalloc_memory, args=())
]

for obj in alive:
    print('START', obj)
    try:
        obj.start()
    except Exception:
        pass

The errors are:

python: Python/tracemalloc.c:445: tracemalloc_remove_trace: Assertion `tracemalloc_config.tracing' failed.
Aborted

And:

python: Python/tracemalloc.c:469: tracemalloc_add_trace: Assertion `tracemalloc_config.tracing' failed.
Aborted

Found with fusil by @vstinner.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Output from running 'python -VV' on the command line:

Python 3.14.0a1+ experimental free-threading build (heads/main:d467d9246c, Nov 1 2024, 09:05:56) [GCC 11.4.0]

贡献指南

打开贡献指南

从这里开始

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

调研方向

首先使用实验性的 free-threading 构建和 PYTHON_GIL=0 重现提供的线程示例。检查 Python/tracemalloc.c 中 tracemalloc_remove_trace 和 tracemalloc_add_trace 附近的代码,然后验证在并发使用 tracemalloc 操作时该示例不再中止。

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

评估

技术栈
python
领域
devtools
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
冷清
描述清晰度
基本清楚
新手友好度
45/100

把新 issue 发到你的邮箱

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