python / python/cpython

Cross-interpreter memory leak in _xidata_release when originating interpreter is destroyed

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

还没有人认领这个 Issue。

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

描述

Bug report

Bug description:

Bug report

Bug summary
There is a known memory leak in Python/crossinterp.c related to the teardown of sub-interpreters when they share data.

When a sub-interpreter packages data into _PyXIData_t and passes it to a receiving interpreter, the receiver is expected to call _PyXIData_Release to free it. However, if the originating interpreter is destroyed before the receiver calls release, the original interpreter's memory context is lost.

In _xidata_release (Python/crossinterp.c around line 1049), the code correctly identifies this state:

    PyInterpreterState *interp = _PyInterpreterState_LookUpID(
                                        _PyXIData_INTERPID(xidata));
    if (interp == NULL) {
        // The interpreter was already destroyed.
        // This function shouldn't have been called.
        // XXX Someone leaked some memory...

Because the originating interpreter is gone, Python intentionally leaks xidata->obj to avoid a use-after-free segfault.

Proposed Solution Architecture
To resolve this without crashing, we would need to track cross-interpreter data at the interpreter level:

  1. Modify PyInterpreterState to include a thread-safe registry tracking all active _PyXIData_t instances created by that interpreter.
  2. Register instances during _PyCode_GetXIData and deregister them during _xidata_release.
  3. Modify Py_EndInterpreter teardown to check this registry. If outstanding shared data exists, the shutting-down interpreter must either block until it is released, or forcefully invalidate/free the shared data (using an is_invalidated flag) before tearing down its own obmalloc state.

I'm opening this issue to get feedback on the proposed architecture before starting a Pull Request.

CPython versions tested on:
main branch

CPython versions tested on:

CPython main branch

Operating systems tested on:

Windows

Linked PRs
  • gh-153745

贡献指南

打开贡献指南

从这里开始

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

调研方向

从 Python/crossinterp.c 中的 _xidata_release 附近开始,然后跟踪 _PyCode_GetXIData 和 Py_EndInterpreter 的 teardown 路径。在继续之前查看链接的 gh-153745 工作,因为此 issue 请求架构方面的反馈,并未定义确定的实现方案或完成测试。

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

评估

技术栈
c, python
领域
backend
Issue 类型
缺陷
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
需要澄清
新手友好度
25/100

把新 issue 发到你的邮箱

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