Race condition in send()
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 45/100
- Issue 类型
- 缺陷
- 描述清晰度
- 基本清楚
- 活跃度
- 冷清
- 技术栈
- python
- 领域
- backend, networking
调研方向
在缓存和适配器流程中追踪 adapter.send()、conditional_headers()、build_response()、update_cached_response() 和 _load_from_cache()。使用并发请求复现所描述的双进程序列,然后验证收到 304 的请求仍然会返回缓存的响应或正常的缓存未命中,而不是将 304 暴露给调用方。
由索引模型根据 Issue 内容生成。
描述
I spotted this while using tox -vvv -p all, so via pip, since pip==26.1.
The issue with tox -p all is that it can run multiple versions of Python (in my config from 3.10 to 3.14), in parallel, I bet some with zstd some without.
TL;DR:
- pip wants to query /simple/setuptools/
- Cache is checked, an etag is found in the cache.
- The HTTP request with an If-None-Match is sent.
- In the meantime the cache gets modified, from
Accept-Encoding: gzip, deflatetoAccept-Encoding: gzip, deflate, zstdor the other way around, by another process. - The HTTP response arrives, it's a
304 Not Modified, the cache has to be updated, but this time it does no longer loads, and the 304 itself is returned.
In details:
It all starts in those two lines of adapter.send():
request.headers.update(self.controller.conditional_headers(request))
resp = super().send(request, stream, timeout, verify, cert, proxies)
Here conditional_headers does a _load_from_cache which can succeed, let say it succeed.
Then super().send() will eventually call adapter.build_response() with a 304 Not Modified so build_response calls self.controller.update_cached_response which runs another _load_from_cache, which can fail if the cache was modified by another process. Let's say it fails it runs this code:
if not cached_response:
# we didn't have a cached response
return response
So the 304 response is returned, up to the user. In case of pip an exception is raised because the answer don't have a Content-Type. But pip or not, the user just did a get with no If-None-Match so he does not expect a 304, either a cache hit (hopefully 200 OK) or a cache miss (hopefully 200 OK) but never a 304.
The chronology with two processes
It think it looks like this, [1] and [2] denoting two distinct processes:
- [1] runs a
requests.get("https://pypi.org/simple/setuptools/") - [2] runs a
requests.get("https://pypi.org/simple/setuptools/") - [1] Read the cache, extract an ETag from the cache
- [2] Read the cache, extract an ETag from the cache
- [1] Sends the HTTP request with If-None-Match
- [2] Sends the HTTP request with If-None-Match
- [1] Gets a response,
adapter.build_responsecallsself.controller.update_cached_response(), changing the headers on disk. TheAccept-Encodingin my case. - [1] Returns the cached response.
- [2] Gets a response,
adapter.build_responsecallsself.controller.update_cached_response()which calls_load_from_cachewhich refuses to load because the headers has changed (Varytells the answer varies onAccept-Encodingand theAccept-Encodingchanged),update_cached_responsereturns the 304 response.build_responsereturns the 304 response. The user gets a304, which is unexpected.
- 主要语言
- Python
- 星标
- 505
- 派生
- 153
- 平均合并
- 5 天 6 小时
- 30 天内合并 PR
- 5
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
psf/cachecontrol 的其他 Issue
-
Cache Deserialization (part two) -- warning message incorrect?可能重新可做 关联的 PR 已关闭且未合并。 未关闭
难度 2/5 1-3 小时 新手友好度 65/100
psf/cachecontrol#209 · 5 条评论 ·
-
HTTP 302 Found responses with Cache Headers not being cached可能已有人在做 @cburroughs 于 280 天前认领。 未关闭
难度 3/5 1-2 天 新手友好度 42/100
psf/cachecontrol#384 · 18 条评论 ·
-
Head request is not cached even with cacheable_methods=("GET", HEAD") without a previous GET可能重新可做 关联的 PR 已关闭且未合并。 未关闭
难度 3/5 1-2 天 新手友好度 35/100
psf/cachecontrol#337 · 6 条评论 ·
-
Investigate failure to load caches above `2^32 - 1` bytes可能重新可做 @woodruffw 于 853 天前认领,目前没有进行中的 PR。 未关闭
psf/cachecontrol#336 · 4 条评论 · 1 个 reaction · 已指派 1 人 ·
-
The new SeparateBodyFileCache design makes updates/reads non-atomic可能重新可做 关联的 PR 已关闭且未合并。 未关闭bug
难度 5/5 一周以上 新手友好度 28/100
psf/cachecontrol#324 · 4 条评论 ·
相似的 Issue
-
Atomic append fails to create a missing file可能已有人在做 @harbinresearcher 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 70/100
fabiocaccamo/python-fsutil#201 ·
-
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复
-
task
难度 2/5 1-3 小时 新手友好度 78/100
danparshall/claude_researcher#87 ·
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 62/100
GhostManager/Ghostwriter#998 · 1 条评论 ·
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 85/100
datajoint/datajoint-python#1586 ·
维护者通常 1 天内回复