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 ファイルあり
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- 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 日以内に返信