[Bug]: withStartupTimeout() changes the startup timeout of all containers that use the default wait strategy
Maintainers usually reply within 1 day
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 64/100
Research direction
The shared state is DEFAULT_WAIT_STRATEGY in core/src/main/java/org/testcontainers/containers/GenericContainer.java (fields around lines 177-183, also referenced from ContainerDef.java line 61); getWaitStrategy() (~line 885) returns it and withStartupTimeout(Duration) (~line 1221) mutates it in place. Start by adding the reproduction test from the report under core/src/test/java/org/testcontainers/containers/ and running ./gradlew :testcontainers:test --tests 'org.testcontainers.containers.DefaultWaitStrategyTimeoutTest' to watch it fail, then confirm the default strategy is copied per container before the timeout is applied. Done means the new test passes and no other container's strategy instance is mutated. Note the reporter has already offered a patch here, so coordinate before starting.
Written by the indexing model from the issue text.
Description
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 withContainerLaunchException(log below). - Without the first line (
new GenericContainer<>(...).withStartupTimeout(...)): the test passes after about 6 seconds.
Cause (links to main at e16dca9ea):
DEFAULT_WAIT_STRATEGYis onestaticinstance. Every container withoutwaitingFor(...)points to it: thewaitStrategyfield andContainerDef.getWaitStrategy()returns this shared instance.withStartupTimeout(Duration)callsgetWaitStrategy().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.
- Dominant language
- Java
- Stars
- 8.8k
- Forks
- 2k
- Avg merge
- 5d 4h
- Merged PRs (30d)
- 17
Getting set up
Starts the project's dev container in your browser, under your own GitHub account.
- Ships a Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from testcontainers/testcontainers-java
-
[Enhancement]: Document why singleton containers are required under Spring's test context cachingPossibly taken @zakaullah075 claimed this 93 days ago. Opentype/enhancement
Difficulty 1/5 Under an hour Newbie friendliness 85/100
testcontainers/testcontainers-java#11967 · 1 comment ·
Maintainers usually reply within 1 day
-
[Bug]: Cannot reuse Selenium BrowserWebDriverContainers under LinuxPossibly taken @kdelay claimed this 115 days ago. Opentype/bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
testcontainers/testcontainers-java#11941 ·
Maintainers usually reply within 1 day
-
[Bug]: DockerDesktopClientProviderStrategy is always applicablePossibly taken @seonwooj0810 claimed this 164 days ago. Opentype/bug
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
testcontainers/testcontainers-java#11829 · 1 comment ·
Maintainers usually reply within 1 day
-
[Enhancement]: cleanup document after removing junit4 supportPossibly taken @SJvaca30 claimed this 171 days ago. Opentype/enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
testcontainers/testcontainers-java#11578 ·
Maintainers usually reply within 1 day
-
[Feature]: Presto Module uses the outdated Docker Image of `ghcr.io/trinodb/presto` and needs to be updated to `prestodb/presto`Possibly taken @linghengqian claimed this 854 days ago. Openhelp wanted type/feature
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
testcontainers/testcontainers-java#8657 · 4 comments ·
Maintainers usually reply within 1 day
All issues in testcontainers/testcontainers-java
Similar issues
-
triage
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
xerial/sqlite-jdbc#1515 ·
Maintainers usually reply within 1 day
-
Area/Configuration Type/Improvement
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
ballerina-platform/static-code-analysis-tool#217 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
eclipse-dirigible/dirigible#8636 · 1 comment ·
Maintainers usually reply within 1 day
-
bug severity-medium XS
Difficulty 2/5 1-3 hours Newbie friendliness 80/100
nikolas-charalambidis/facepalm-maven-plugin#291 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
OakLoaf/PluginUpdater#264 ·
Maintainers usually reply within 1 day