Bug: HttpWaitStrategy ignores response predicates for accepted HTTP error statuses
维护者通常 1 天内回复
评估
调研方向
从 testcontainers/core/wait_strategies.py 的 HttpWaitStrategy._handle_http_error() 开始,它在未检查 _response_predicate 的情况下直接返回 True。阅读 _check_response() 以了解正常的谓词求值方式。运行该 issue 的复现脚本以看到错误的 ready,然后修改错误分支使其对谓词求值。完成意味着脚本抛出 TimeoutError。
由索引模型根据 Issue 内容生成。
描述
Describe the bug
HttpWaitStrategy.for_response_predicate() is not evaluated when an HTTP endpoint returns an explicitly accepted error status, such as 404 or 503. The strategy reports ready even when the response body does not match the predicate.
This affects both for_status_code(...) and for_status_code_matching(...). HTTP 200 responses do evaluate the body predicate. A caller that deliberately accepts an error status still needs the additional body condition to pass; accepting the status should not bypass that condition.
Found while testing HTTP readiness behavior on main at 6146aa1170f415fcc825ed9bddc4eacbeb937a12 (package version 4.15.0). I searched issues and PRs for HttpWaitStrategy, for_response_predicate, response predicate, and HTTPError and did not find this behavior reported. The HTTPError resource-cleanup work in #1115 is related code but a different problem.
Reproduction
This endpoint always returns 503 with body starting. The status is deliberately accepted, but the body predicate requires ready.
"""Reproduce a false-positive HTTP readiness result (requires Docker)."""
from testcontainers.core.container import DockerContainer
from testcontainers.core.wait_strategies import HttpWaitStrategy
server = """
from http.server import BaseHTTPRequestHandler, HTTPServer
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(503)
self.end_headers()
self.wfile.write(b"starting")
HTTPServer(("0.0.0.0", 8080), Handler).serve_forever()
"""
strategy = (
HttpWaitStrategy(8080)
.for_status_code(503)
.for_response_predicate(lambda body: body == "ready")
.with_startup_timeout(2)
.with_poll_interval(0.1)
)
try:
with (
DockerContainer("python:3.12-alpine")
.with_exposed_ports(8080)
.with_command(["python", "-u", "-c", server])
.waiting_for(strategy)
):
raise AssertionError("BUG: marked ready even though the body is always 'starting'")
except TimeoutError:
print("Correct: the body never matched, so readiness timed out.")
Expected behavior
The wait should continue polling and raise TimeoutError because the body never matches.
Actual behavior
The context manager enters immediately after the first HTTP response; the assertion fails. In a second reproduction, an endpoint returning starting on request 1 and ready on request 2 receives only one request before the wait finishes.
Root cause and proposed fix
urlopen() raises HTTPError for error statuses. _handle_http_error() accepts the status and returns True without evaluating _response_predicate. The normal response path in _check_response() does evaluate it.
Evaluate the body predicate for accepted HTTP errors while the existing with e: block still owns the response. This preserves status-only waits and response cleanup.
Runtime environment
- macOS / Apple silicon; Colima Docker runtime
- Regression tests reproduced on Python 3.12.14
- Patch validated on Python 3.10.22, 3.12.14, 3.13.17, and 3.14.9
- Real HTTP server in
python:3.12-alpine
- 主要语言
- Python
- 星标
- 2.3k
- 派生
- 395
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。
- 提供 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
testcontainers/testcontainers-python 的其他 Issue
-
Bug: LogMessageWaitStrategy: a container that exits before its message raises without its logs, and __enter__ then removes the container可能已有人在做 @granzer69 于 25 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 76/100
testcontainers/testcontainers-python#1138 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 67/100
testcontainers/testcontainers-python#1086 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 68/100
testcontainers/testcontainers-python#578 · 5 条评论 ·
维护者通常 1 天内回复
-
Bug: Reaper is force-removed at interpreter exit since 4.15.0, session containers are never reaped可能已有人在做 @pantafive 于 25 天前认领。 未关闭
难度 3/5 1-2 天 新手友好度 72/100
testcontainers/testcontainers-python#1140 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 35/100
testcontainers/testcontainers-python#1137 ·
维护者通常 1 天内回复
查看 testcontainers/testcontainers-python 的全部 Issue
相似的 Issue
-
bug
难度 2/5 1-3 小时 新手友好度 77/100
canonical/paas-charm#407 · 1 条评论 ·
维护者通常 5 天内回复
-
bug
难度 1/5 1 小时以内 新手友好度 90/100
FAIRmat-NFDI/pynxtools-spm#136 ·
维护者通常 1 天内回复
-
enhancement tooling
难度 2/5 1-3 小时 新手友好度 84/100
维护者通常 1 天内回复
-
`read_nexus` entry point points to a function that no longer exists可能已有人在做 @RubelMozumder 今天认领。 未关闭annotator bug
难度 1/5 1 小时以内 新手友好度 88/100
FAIRmat-NFDI/pynxtools#876 · 1 条评论 ·
维护者通常 1 天内回复
-
[Bug]: HTTP 499 (Client Closed Request) is classified as a non-retryable format_error, so a mid-flight abort fails the turn over instead of retrying可能已有人在做 @yipinghuang1991 今天认领。 未关闭comp/agent P2 type/bug
难度 2/5 1-3 小时 新手友好度 82/100
NousResearch/hermes-agent#156837 ·
维护者通常 1 天内回复