Bug: HttpWaitStrategy ignores response predicates for accepted HTTP error statuses
Maintainer thường phản hồi trong vòng 1 ngày
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 85/100
Hướng nghiên cứu
Bắt đầu trong testcontainers/core/wait_strategies.py tại HttpWaitStrategy._handle_http_error(), hàm này trả về True mà không kiểm tra _response_predicate. Đọc _check_response() để xem cách đánh giá predicate thông thường. Chạy script tái hiện của issue để thấy ready sai, sau đó sửa nhánh lỗi để đánh giá predicate. Hoàn thành nghĩa là script ném ra TimeoutError.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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
- Ngôn ngữ chính
- Python
- Star
- 2.3k
- Fork
- 395
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Chuẩn bị môi trường
Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.
- Có Dockerfile hoặc tệp Docker Compose
- Có mẫu pull request
- Đọc 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.
Issue khác của testcontainers/testcontainers-python
-
Bug: LogMessageWaitStrategy: a container that exits before its message raises without its logs, and __enter__ then removes the containerCó thể đã có người làm @granzer69 đã nhận 25 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
testcontainers/testcontainers-python#1138 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Updating the documentationĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 67/100
testcontainers/testcontainers-python#1086 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Bug: [minio] _healthcheck calls for nested wait_container_is_ready resulting in long wait timeĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
testcontainers/testcontainers-python#578 · 5 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Bug: Reaper is force-removed at interpreter exit since 4.15.0, session containers are never reapedCó thể đã có người làm @pantafive đã nhận 25 ngày trước. Đang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 72/100
testcontainers/testcontainers-python#1140 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 35/100
testcontainers/testcontainers-python#1137 ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của testcontainers/testcontainers-python
Issue tương tự
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 77/100
canonical/paas-charm#407 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 5 ngày
-
bug
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100
FAIRmat-NFDI/pynxtools-spm#136 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
enhancement tooling
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
Maintainer thường phản hồi trong vòng 1 ngày
-
`read_nexus` entry point points to a function that no longer existsCó thể đã có người làm @RubelMozumder đã nhận hôm nay. Đang mởannotator bug
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 88/100
FAIRmat-NFDI/pynxtools#876 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[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 retryingCó thể đã có người làm @yipinghuang1991 đã nhận hôm nay. Đang mởcomp/agent P2 type/bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
NousResearch/hermes-agent#156837 ·
Maintainer thường phản hồi trong vòng 1 ngày