test_httpservers leaves urllib.request._opener installed for the rest of the test run
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Python
- Star
- 77.2k
- Fork
- 35.9k
- Chỉ số merge pull request
- Chỉ số pull request đang chờ
Mô tả
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
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu từ CommandLineRunTimeTestCase.fetch_file() và so sánh việc dọn dẹp của nó với bốn lần sử dụng self.addCleanup(urllib.request.urlcleanup) trong test_urllib. Chạy ./python.exe -m test test_urllib test_httpservers và xác nhận rằng test_httpservers không còn để urllib.request._opener được cài đặt hoặc kích hoạt cảnh báo thay đổi môi trường.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- python
- Lĩnh vực
- testing-qa
- Loại issue
- Lỗi
- Độ khó
- 1/5
- Thời gian dự kiến
- Dưới một giờ
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức phù hợp với người mới
- 25/100