python / python/cpython

test_httpservers leaves urllib.request._opener installed for the rest of the test run

未关闭
#156,031 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

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

描述

Bug report

Bug description:

test_httpservers installs a process-wide default URL opener and never removes it, so regrtest reports it as altering the execution environment:

Warning -- urllib.requests._opener was modified by test_httpservers
Warning --   Before: None
Warning --   After:  <urllib.request.OpenerDirector object at 0x10d5f0f80>
test_httpservers failed (env changed)

Repro on current main, takes a couple of seconds:

./python.exe -m test test_urllib test_httpservers

The leak comes from CommandLineRunTimeTestCase.fetch_file():

def fetch_file(self, path, context=None):
    req = urllib.request.Request(path, method='GET')
    with urllib.request.urlopen(req, context=context) as res:
        return res.read()

context defaults to None, and the plain HTTP tests call it without one. urlopen() only builds a throwaway opener when a context is passed; otherwise it installs the module global:

https://github.com/python/cpython/blob/main/Lib/urllib/request.py#L177-L186

global _opener
if context:
    https_handler = HTTPSHandler(context=context)
    opener = build_opener(https_handler)
elif _opener is None:
    _opener = opener = build_opener()
else:
    opener = _opener

So after test_httpservers runs, urllib.request._opener is a live OpenerDirector for the rest of the process, and any later test that goes through urlopen() silently gets it instead of a fresh one.

Why this has not shown up in CI: save_env.py only watches the resource when urllib.request is already imported, since try_get_module() raises SkipTestEnvironment otherwise. Under -j, and when running test_httpservers on its own, the module is not in sys.modules at the point regrtest snapshots the environment, so the resource is never tracked and the leak is invisible. It only appears in a sequential run, after some earlier test has imported urllib.request.

test_urllib already guards against this with self.addCleanup(urllib.request.urlcleanup) in four places, and urlcleanup() resets _opener, so the same one-liner fits here.

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS


sorry if I've got any of the details wrong here. I traced this myself and used Claude Code as a second pair of eyes on the reasoning, so any mistakes are mine and I'd rather be told than not. college freshman, just trying to be useful where I can :)

Linked PRs
  • gh-156032

贡献指南

打开贡献指南

从这里开始

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

调研方向

从 CommandLineRunTimeTestCase.fetch_file() 开始,将其清理操作与 test_urllib 中四处使用 self.addCleanup(urllib.request.urlcleanup) 的地方进行比较。运行 ./python.exe -m test test_urllib test_httpservers,并确认 test_httpservers 不再让 urllib.request._opener 保持已安装状态,也不再触发环境变更警告。

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

评估

技术栈
python
领域
testing-qa
Issue 类型
缺陷
难度
1/5
预计耗时
1 小时以内
活跃度
停滞
描述清晰度
描述清楚
新手友好度
25/100

把新 issue 发到你的邮箱

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