python / python/cpython

`atexit` module breaks refleak check

未关闭
#139,289 5 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

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

描述

Bug report

Bug description:

If the test registers a callback with atexit module, refleak test will consider it a leak.

    def test_refleak(self):
        atexit.register(lambda: None)

This is the root cause of the buildbot failure of https://github.com/python/cpython/pull/139185. It's a couple of coincidences that caused the seemingly unrelated issue. Basically, clearing the module cache in test_zipimport made the module-level code in rlcompleter, which includes a register to atexit, run more than once.

There are a few possible approaches to fix this, but I did not find the solution that I'm immediately happy with.

I consider this a test framework bug as this should be a valid behavior. The ideal solution is to keep the atexit callbacks before running test in a loop and restore it after. However, atexit does not provide such an interface, public or private. You can either clear it or run it. The callbacks are stored in a state of the interpreter and we don't have any access to it.

Without that, we either need to somehow calculate the allocated memory by atexit, or hope that no one writes a test that writes to atexit. Of course there might be other ways that I did not think of.

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Linked PRs
  • gh-139305
  • gh-139357
  • [3.14] gh-139305 was backported as part of gh-139280

贡献指南

打开贡献指南

从这里开始

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

调研方向

从 refleak 测试框架以及注册 atexit 回调的 test_refleak 示例开始。检查 test_zipimport 清除模块缓存如何导致 rlcompleter 的模块级注册被重复执行,并查看关联的 PR gh-139305 和 gh-139357,了解已有的工作。完成的标准是,有效的 atexit 注册不再显示为 refleaks。

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

评估

技术栈
python
领域
testing-qa
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
需要澄清
新手友好度
20/100

把新 issue 发到你的邮箱

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