[Bug]: withStartupTimeout() changes the startup timeout of all containers that use the default wait strategy

Aperta Adatta ai principianti
#12,171 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@aymenjamaaoui ci sta già lavorando.

Dal 15/11/2026.

  • #12172 di @aymenjamaaoui — aperta

Valutazione

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

Direzione di ricerca

Lo stato condiviso è DEFAULT_WAIT_STRATEGY in core/src/main/java/org/testcontainers/containers/GenericContainer.java (campi intorno alle righe 177-183, referenziati anche da ContainerDef.java riga 61); getWaitStrategy() (~riga 885) lo restituisce e withStartupTimeout(Duration) (~riga 1221) lo muta sul posto. Inizia aggiungendo il test di riproduzione dalla segnalazione sotto core/src/test/java/org/testcontainers/containers/ ed eseguendo ./gradlew :testcontainers:test --tests 'org.testcontainers.containers.DefaultWaitStrategyTimeoutTest' per vederlo fallire, poi conferma che la strategia predefinita viene copiata per container prima che venga applicato il timeout. Fatto significa che il nuovo test passa e nessuna istanza di strategia di un altro container viene mutata. Nota che chi ha segnalato ha già offerto una patch qui, quindi coordinati prima di iniziare.

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

Descrizione

type/bug
Module

Core

Testcontainers version

2.0.5

Using the latest Testcontainers version?

Yes

Host OS

macOS 27.2.1

Host Arch

ARM

Docker version
Client:
 Version:           29.8.2
 API version:       1.56
 Go version:        go1.26.8
 Git commit:        7fc2dff
 Built:             Wed Sep 30 19:32:03 2026
 OS/Arch:           darwin/arm64
 Context:           desktop-linux

Server: Docker Desktop 4.94.0 (241994)
 Engine:
  Version:          29.8.2
  API version:      1.56 (minimum version 1.40)
  Go version:       go1.26.8
  Git commit:       8af9fe3
  Built:            Wed Sep 30 19:31:50 2026
  OS/Arch:          linux/arm64
  Experimental:     false
 containerd:
  Version:          v2.3.6
  GitCommit:        ee2735368117d2eb259779949d5e75cdafec9761
 runc:
  Version:          1.5.1
  GitCommit:        v1.5.1-0-g8f2685a4
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0
What happened?

withStartupTimeout(Duration) on a container without its own wait strategy changes the startup timeout of all containers in the JVM that use the default wait strategy.

Expected: the timeout applies only to the container where it is set.
Actual: other containers get this timeout too. A short timeout on one container makes another container fail. The result depends on the order of tests in the JVM, so it looks like a flaky test in another class.

Reproduction. Put this test in core/src/test/java/org/testcontainers/containers/ and run
./gradlew :testcontainers:test --tests 'org.testcontainers.containers.DefaultWaitStrategyTimeoutTest':

package org.testcontainers.containers;

import org.junit.jupiter.api.Test;

import java.time.Duration;

class DefaultWaitStrategyTimeoutTest {

    @Test
    void startupTimeoutOfOneContainerDoesNotChangeOtherContainers() {
        // Never started. Only sets a short timeout for itself.
        new GenericContainer<>("alpine:3.17").withStartupTimeout(Duration.ofSeconds(2));

        // Default wait strategy (60 seconds). The port opens after 5 seconds.
        try (
            GenericContainer<?> slow = new GenericContainer<>("alpine:3.17")
                .withExposedPorts(8080)
                .withCommand("sh", "-c", "sleep 5; while true; do nc -l -p 8080; done")
        ) {
            slow.start();
        }
    }
}
  • On main (e16dca9ea): the test fails after about 3 seconds with ContainerLaunchException (log below).
  • Without the first line (new GenericContainer<>(...).withStartupTimeout(...)): the test passes after about 6 seconds.

Cause (links to main at e16dca9ea):

  1. DEFAULT_WAIT_STRATEGY is one static instance. Every container without waitingFor(...) points to it: the waitStrategy field and ContainerDef.
  2. getWaitStrategy() returns this shared instance.
  3. withStartupTimeout(Duration) calls getWaitStrategy().withStartupTimeout(...), so it changes the shared instance itself.
Relevant log output
org.testcontainers.containers.ContainerLaunchException: Container startup failed for image alpine:3.17
Caused by: org.testcontainers.containers.ContainerLaunchException: Could not create/start container
Caused by: org.testcontainers.containers.ContainerLaunchException: Timed out waiting for container port to open (localhost ports: [55831] should be listening)
Additional Information

A possible fix: give the container its own default strategy before changing the timeout. With this change, the test above passes:

public SELF withStartupTimeout(Duration startupTimeout) {
    if (getWaitStrategy() == DEFAULT_WAIT_STRATEGY) {
        waitingFor(Wait.defaultWaitStrategy());
    }
    getWaitStrategy().withStartupTimeout(startupTimeout);
    return self();
}

I can open a PR with this fix and the test if this direction is OK for you.

I found this while working on #12103: JdbcDatabaseContainer.withStartupTimeoutSeconds() must not change the shared instance either. #12103 is in draft until I fix it.

Lingua principale
Java
Stelle
8.8k
Fork
2k
Merge medio
5g 4h
PR unite (30g)
17

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

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.

Altre issue di testcontainers/testcontainers-java

Tutte le issue di testcontainers/testcontainers-java

Issue simili

Altre issue su Java

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.