python / python/cpython

[subinterpreters] Refleaks on Windows Under Specific Conditions

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

还没有人认领这个 Issue。

OS-windows pending topic-subinterpreters type-bug
主要语言
Python
星标
77.2k
派生
36k
PR 合并指标
PR 指标待抓取

描述

Bug report

Bug description:

One of my recent PRs, gh-117662, led to one of the Windows refleak buildbots failing^1. I'm fairly confident the PR revealed an existing source of refleaks (albeit an unlikely one), rather that introducing new leaks. I have resolved the failures with gh-117913, but the underlying potential source of refleaks remains.

I've been able to reproduce the leak conditions, but only on Windows (for now?). The following conditions are necessary:

  • interpreter created using a config with check_multi_interp_extensions=False (e.g. "legacy")
  • interpreter imports test.support.os_helper
try:
    import _interpreters
except ModuleNotFoundError:
    import _xxsubinterpreters as _interpreters
config = _interpreters.new_config('legacy')  # critically, check_multi_interp_extensions=False
interpid = _interpreters.create(config)
_interpreters.exec(interpid, 'import test.support.os_helper')
_interpreters.destroy(interpid)
# leaks a bunch of objects

When the bug is triggered, around 190 objects are leaked. At first I thought this was interpreter finalization failing silently, but now I think it is something else.

I suspect the underlying problem relates to legacy extension modules. In the specific example above, I'm pretty sure the _ctypes module is leaking and ~190 is how many objects it holds, directly or indirectly. The main clue there is that, when I run ./python -v ..., the _ctypes module is never noted as destroyed, whereas all other modules are. As to test.support.os_helper, _ctypes is one of the modules that gets indirectly imported. All that said, I haven't been able to reproduce the leak if the subinterpreter above imports _ctypes instead.

Why did we only see this on Windows? The only clue I can think of is that on Windows stdlib extension modules are all turned into builtin modules, IIRC. The logic of the check_multi_interp_extensions check (via _PyImport_CheckSubinterpIncompatibleExtensionAllowed() in Python/import.c) is specific to extension modules, not builtin modules.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Windows

贡献指南

打开贡献指南

从这里开始

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

调研方向

使用提供的 _interpreters legacy-config 代码片段重现 Windows 泄漏,然后从 Python/import.c 中的 _PyImport_CheckSubinterpIncompatibleExtensionAllowed() 附近开始。使用报告中描述的详细 import 输出,比较 test.support.os_helper 和 _ctypes 的模块清理情况。当 reproducer 不再泄漏对象,并且相关的 Windows 行为已由适当的回归测试覆盖时,即表示完成。

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

评估

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

把新 issue 发到你的邮箱

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