Bug: HttpWaitStrategy ignores response predicates for accepted HTTP error statuses
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 85/100
Research direction
Start in testcontainers/core/wait_strategies.py at HttpWaitStrategy._handle_http_error(), which returns True without checking _response_predicate. Read _check_response() to see the normal predicate evaluation. Run the issue's reproduction script to see the false ready, then edit the error branch to evaluate the predicate. Done means the script raises TimeoutError.
Written by the indexing model from the issue text.
Description
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
- Dominant language
- Python
- Stars
- 2.3k
- Forks
- 397
- Avg merge
- 8d 16h
- Merged PRs (30d)
- 5
Getting set up
Starts the project's dev container in your browser, under your own GitHub account.
- Ships a Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from testcontainers/testcontainers-python
-
Feature: Stream bytes out on copy_from_containerPossibly taken @archit15singh claimed this 11 days ago. Open🚀 enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 67/100
-
Bug: LogMessageWaitStrategy: a container that exits before its message raises without its logs, and __enter__ then removes the containerPossibly taken @granzer69 claimed this 42 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 67/100
testcontainers/testcontainers-python#1086 · 1 comment ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
testcontainers/testcontainers-python#578 · 5 comments ·
-
Bug: Reaper is force-removed at interpreter exit since 4.15.0, session containers are never reapedPossibly taken @pantafive claimed this 42 days ago. Open
Difficulty 3/5 1-2 days Newbie friendliness 72/100
All issues in testcontainers/testcontainers-python
Similar issues
-
bug feature request
Difficulty 2/5 1-3 hours Newbie friendliness 67/100
Maintainers usually reply within 1 day
-
Difficulty 1/5 1-3 hours Newbie friendliness 66/100
Maintainers usually reply within 3 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
Qiskit/qiskit-obp#156 ·
-
action-item enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
getcommunityone/open-navigator#1658 ·
Maintainers usually reply within 1 day
-
review-panel severity:low
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
kristovatlas/coin-accounting#636 ·
Maintainers usually reply within 1 day