Skip to content

fix(wait-strategy): close HTTPError in HttpWaitStrategy to avoid fd leak - #1118

Open
BrunosGits wants to merge 1 commit into
testcontainers:mainfrom
BrunosGits:fix/1115-http-fd-leak
Open

BrunosGits wants to merge 1 commit into
testcontainers:mainfrom
BrunosGits:fix/1115-http-fd-leak

Conversation

@BrunosGits

Copy link
Copy Markdown

Summary

Fixes #1115: when an endpoint polled by HttpWaitStrategy answers with an error status, the raised HTTPError was never closed, leaking a file descriptor per poll (surfaces as ResourceWarning: Implicitly cleaning up <HTTPError ...> from tempfile.del).

Root cause

In _try_http_request, urlopen raises HTTPError for error statuses, so the with-block never owns it. The except arm forwards the error to _handle_http_error, which inspected error.code and returned without closing it. HTTPError wraps the response body fp, so each failed poll held an fd until GC.

Changes

  • src/testcontainers/core/wait_strategies.py: _handle_http_error now closes the error in a finally block (isinstance-guarded, so the plain URLError path is untouched; the debug log runs before close).
  • tests/core/test_wait_strategies.py: 2 regression tests - a Mock(spec=HTTPError) close assertion parametrized over unexpected/expected status codes, and a live 127.0.0.1 503-server behavioral check.

Testing

  • New close assertions fail pre-fix (close called 0 times) and pass post-fix.
  • tests/core/test_wait_strategies.py: 88 passed; tests/core/test_labels.py: 9 passed.
  • ruff check + ruff format clean.

Closes #1115

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

1 participant