testcontainers / testcontainers/testcontainers-python

Feature: Timeout on command execution

Aperta
#972 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

What are you trying to do?

The DockerContainer.exec() method does not support a timeout parameter, even though the documentation explicitly describes this functionality:

with GenericContainer("alpine:latest") as container:
    try:
        exit_code, output = container.exec(
            ["sleep", "10"],
            timeout=5  # Timeout in seconds
        )
    except TimeoutError:
        print("Command timed out")

However, the actual implementation simply forwards to docker-py's exec_run without accepting any additional keyword arguments:

def exec(self, command: Union[str, list[str]]) -> ExecResult:
    if not self._container:
        raise ContainerStartException("Container should be started before executing a command")
    return self._container.exec_run(command)

We'd like exec() to support a timeout so that commands that hang or take too long don't block test runs indefinitely.

Why should it be done this way?

Without a timeout, any exec() call that hangs (e.g. a command waiting on a resource that never becomes available) will block the test process forever with no way to recover other than killing the process externally.

Unfortunately this isn't a simple pass through - the underlying docker-py Container.exec_run() also does not expose a timeout parameter. So this would need a client-side implementation, for example wrapping the call with concurrent.futures.ThreadPoolExecutor and a deadline, or using exec_run(socket=True) with a socket-level timeout.

Just like the timeout for a subprocess.run run doesn't guarantee closing the process after the timeout, so will this.

I'm happy to open a PR for this, but wanted to gain agreement on the issue first before submitting code.

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 DockerContainer.exec e docs/features/executing_commands.md, quindi confronta l’esempio di timeout documentato con docker-py Container.exec_run e le sue limitazioni. Il lavoro è completato quando exec() accetta un timeout, solleva TimeoutError al superamento della scadenza e non lascia il test run bloccato indefinitamente.

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

Valutazione

Stack tecnologico
docker, python
Ambito
devops, testing
Tipo di issue
Funzionalità
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
42/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.