python / python/cpython

`arraymodule`, `_decimal`: possible lazy import caches reference leaks in free-threaded builds

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

还没有人认领这个 Issue。

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

描述

Bug description:

As pointed out by @htjworld in https://github.com/python/cpython/pull/148088#issuecomment-4196296604:

I was wondering about the lazy init pattern in a free-threaded build. If two threads enter build_struct_time concurrently with st->time_struct_time == NULL, both would call PyImport_ImportModuleAttrString and one overwrite would leave a reference with an incremented refcount that never gets decref'd.

I noticed arraymodule.c and _decimal.c have the same pattern, so maybe this is just accepted. But _asynciomodule.c initializes eagerly in module_init() which avoids this structurally. Was lazy chosen deliberately here over eager init in init_state()?

While I've addressed this in #148088, I think that this is a valid concern, and import caches should use CAS, just like:

https://github.com/python/cpython/blob/7e0a0be4097f9d29d66fe23f5af86f18a34ed7dd/Modules/_sre/sre.c#L1173-L1188

If there's interest, I can do it.

(It seems similar to #149000, so perhaps a more thorough check warranted.)

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

贡献指南

打开贡献指南

从这里开始

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

调研方向

首先比较 arraymodule.c 和 _decimal.c 中的延迟初始化与 _asynciomodule.c 中的急切初始化,以及 Modules/_sre/sre.c 中的 CAS 模式。检查 free-threaded 路径中的并发初始化和引用泄漏;当受影响的导入缓存能够在该并发模式下安全运行,且相关行为已由验证覆盖时,即视为完成。

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

评估

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

把新 issue 发到你的邮箱

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