testcontainers / testcontainers/testcontainers-python

Bug: file descriptor leak in HttpWaitStrategy

Aperta Adatta ai principianti
#1,115 0 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Lingua principale
Python
Stelle
2.3k
Fork
386
Merge medio
4h 40m
PR unite (30g)
1

Descrizione

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

The issue is in https://github.com/testcontainers/testcontainers-python/blob/5c70d540e0df919e2de77ce8cc75b5c50cd772e7/src/testcontainers/core/wait_strategies.py#L428-L437

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!

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia da src/testcontainers/core/wait_strategies.py, intorno a HttpWaitStrategy._handle_http_error, che l’issue identifica come il punto in cui si verifica la perdita. Esegui l’MRE fornito con -Werror::ResourceWarning sull’immagine typesense/typesense per riprodurla. Il lavoro è completato quando una risposta di errore HTTP viene ripulita correttamente e l’MRE termina senza la ResourceWarning.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
docker, python
Ambito
testing-qa
Tipo di issue
Bug
Difficoltà
2/5
Tempo stimato
1-3 ore
Stato di attività
Attiva
Chiarezza
Specificata chiaramente
Idoneità per principianti
88/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.