testcontainers / testcontainers/testcontainers-python
Bug: file descriptor leak in HttpWaitStrategy
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 2.3k
- Forks
- 386
- Ø Merge
- 4 Std. 40 Min.
- Gemergte PRs (30 T.)
- 1
Beschreibung
Describe the bug
When using an HttpWaitStrategy in DockerContainer.waiting_for and the container image respond with an error code, the underlying HttpError is not correctly handled, leaking a file descriptor.
To Reproduce
Example with the https://hub.docker.com/r/typesense/typesense image, but should work with any image providing a HTTP server that takes a few seconds to start:
# mre.py
from testcontainers.core.container import DockerContainer
from testcontainers.core.wait_strategies import HttpWaitStrategy
print("starting")
with (
DockerContainer("typesense/typesense:30.2", command="--data-dir /home --api-key=whatever --enable-cors")
.with_exposed_ports(8108)
.waiting_for(HttpWaitStrategy(8108, "/health").for_status_code(200))
):
print("ok")
$ python -Werror::ResourceWarning mre.py
starting
Exception ignored while calling deallocator <function _TemporaryFileCloser.__del__ at 0x101984880>:
Traceback (most recent call last):
File "/Users/loic/.local/share/uv/python/cpython-3.14.4-macos-aarch64-none/lib/python3.14/tempfile.py", line 484, in __del__
_warnings.warn(self.warn_message, ResourceWarning)
ResourceWarning: Implicitly cleaning up <HTTPError 503: ''>
ok
Root Cause / fix
HttpError holds a reference to a (temporary) file storing the HTTP response, so it must be handled in a context manager to safely clean the file when the error is discarded.
Fixed MRE:
# mre.py
from contextlib import nullcontext
from typing import Union
from urllib.error import HTTPError, URLError
from testcontainers.core.container import DockerContainer
from testcontainers.core.wait_strategies import HttpWaitStrategy
class _FixedHttpWaitStrategy(HttpWaitStrategy):
def _handle_http_error(self, error: Union[URLError, HTTPError]) -> bool:
with error if isinstance(error, HTTPError) else nullcontext(error):
return super()._handle_http_error(error)
print("starting")
with (
DockerContainer("typesense/typesense:30.2", command="--data-dir /home --api-key=whatever --enable-cors")
.with_exposed_ports(8108)
.waiting_for(_FixedHttpWaitStrategy(8108, "/health").for_status_code(200))
):
print("ok")
Runtime environment
- MacOS 26.6.2
- Python 3.14.4
- testcontainers 4.14.2
I can work on a PR if you'd like!
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne bei src/testcontainers/core/wait_strategies.py rund um HttpWaitStrategy._handle_http_error, was der Issue als Stelle des Lecks identifiziert. Führe das bereitgestellte MRE mit -Werror::ResourceWarning gegen das Image typesense/typesense aus, um das Problem zu reproduzieren. Erledigt bedeutet, dass eine HTTP-Fehlerantwort sicher bereinigt wird und das MRE ohne die ResourceWarning abgeschlossen wird.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- docker, python
- Bereich
- testing-qa
- Issue-Typ
- Bug
- Schwierigkeit
- 2/5
- Geschätzter Aufwand
- 1-3 Stunden
- Aktivitätsstatus
- Aktiv
- Klarheit
- Klar beschrieben
- Anfängerfreundlichkeit
- 88/100