Skip to content

test(core): drop vacuous URLError wait-strategy case - #1116

Open
cpruijsen wants to merge 3 commits into
testcontainers:mainfrom
cpruijsen:fix/issue-1115
Open

cpruijsen wants to merge 3 commits into
testcontainers:mainfrom
cpruijsen:fix/issue-1115

Conversation

@cpruijsen

Copy link
Copy Markdown

HttpWaitStrategy leaks a file descriptor per failed request. A wait strategy polls, so a container
that takes a while to become healthy leaks one descriptor per attempt, and a suite waiting on several
containers can exhaust the limit.

urlopen is used in a with, so the success path closes. HTTPError is the leak: it is both an
exception and a response file object, so raising it hands back an open descriptor that the except
clause discards without closing.

src/testcontainers/core/wait_strategies.py now splits the handler. HTTPError is handled inside
with e:, which closes the wrapped response on the way out, and URLError, which carries no file
object, keeps the plain path.

Behaviour is otherwise unchanged: both still go to _handle_http_error and the strategy retries as
before.

The test asserts no descriptors are left open across repeated failing probes.

Fixes #1115

HTTPError wraps the temporary file holding the response body. When a
container's HTTP endpoint answered with an error status while the
HttpWaitStrategy was still polling, the exception was discarded without
closing it, leaking a file descriptor (and a ResourceWarning under
-Werror) on every failed attempt.

Use the caught HTTPError as a context manager so the response file is
released on every path. URLError carries no file object and keeps the
existing handling.

Closes testcontainers#1115
The split except clause for HTTPError/URLError in _try_http_request was
missing coverage for a bare URLError.
The assertion returned False both with and without the HTTPError close
fix, so it did not lock in the leak change.
@codecov

codecov Bot commented Sep 14, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 75.00000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 86.39%. Comparing base (5c70d54) to head (f703e06).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/testcontainers/core/wait_strategies.py 75.00% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1116      +/-   ##
==========================================
+ Coverage   86.15%   86.39%   +0.24%     
==========================================
  Files          16       16              
  Lines        1769     1772       +3     
  Branches      198      198              
==========================================
+ Hits         1524     1531       +7     
+ Misses        185      181       -4     
  Partials       60       60              
Files with missing lines Coverage Δ
src/testcontainers/core/wait_strategies.py 78.28% <75.00%> (+1.54%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: file descriptor leak in HttpWaitStrategy

2 participants