[Bug]: withStartupTimeout() changes the startup timeout of all containers that use the default wait strategy
维护者通常 1 天内回复
评估
调研方向
共享状态是 DEFAULT_WAIT_STRATEGY,位于 core/src/main/java/org/testcontainers/containers/GenericContainer.java(大约第 177-183 行的字段,也被 ContainerDef.java 第 61 行引用);getWaitStrategy()(约第 885 行)返回它,withStartupTimeout(Duration)(约第 1221 行)就地修改它。首先在 core/src/test/java/org/testcontainers/containers/ 下添加报告中的复现测试,并运行 ./gradlew :testcontainers:test --tests 'org.testcontainers.containers.DefaultWaitStrategyTimeoutTest' 观察它失败,然后确认在应用超时之前默认策略会按容器复制。完成意味着新测试通过,且没有修改其他任何容器的策略实例。注意报告者已经在这里提供了一个补丁,所以开始前先协调。
由索引模型根据 Issue 内容生成。
描述
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.
- 主要语言
- Java
- 星标
- 8.8k
- 派生
- 2k
- 平均合并
- 5 天 4 小时
- 30 天内合并 PR
- 17
环境准备
在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。
- 提供 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
testcontainers/testcontainers-java 的其他 Issue
-
[Enhancement]: Document why singleton containers are required under Spring's test context caching可能已有人在做 @zakaullah075 于 93 天前认领。 未关闭type/enhancement
难度 1/5 1 小时以内 新手友好度 85/100
testcontainers/testcontainers-java#11967 · 1 条评论 ·
维护者通常 1 天内回复
-
[Bug]: Cannot reuse Selenium BrowserWebDriverContainers under Linux可能已有人在做 @kdelay 于 115 天前认领。 未关闭type/bug
难度 2/5 1-3 小时 新手友好度 78/100
testcontainers/testcontainers-java#11941 ·
维护者通常 1 天内回复
-
[Bug]: DockerDesktopClientProviderStrategy is always applicable可能已有人在做 @seonwooj0810 于 164 天前认领。 未关闭type/bug
难度 2/5 1-3 小时 新手友好度 74/100
testcontainers/testcontainers-java#11829 · 1 条评论 ·
维护者通常 1 天内回复
-
[Enhancement]: cleanup document after removing junit4 support可能已有人在做 @SJvaca30 于 171 天前认领。 未关闭type/enhancement
难度 2/5 1-3 小时 新手友好度 65/100
testcontainers/testcontainers-java#11578 ·
维护者通常 1 天内回复
-
[Feature]: Presto Module uses the outdated Docker Image of `ghcr.io/trinodb/presto` and needs to be updated to `prestodb/presto`可能已有人在做 @linghengqian 于 854 天前认领。 未关闭help wanted type/feature
难度 2/5 1-3 小时 新手友好度 68/100
testcontainers/testcontainers-java#8657 · 4 条评论 ·
维护者通常 1 天内回复
查看 testcontainers/testcontainers-java 的全部 Issue
相似的 Issue
-
triage
难度 2/5 1-3 小时 新手友好度 72/100
xerial/sqlite-jdbc#1515 ·
维护者通常 1 天内回复
-
Area/Configuration Type/Improvement
难度 2/5 1-3 小时 新手友好度 68/100
ballerina-platform/static-code-analysis-tool#217 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 76/100
eclipse-dirigible/dirigible#8636 · 1 条评论 ·
维护者通常 1 天内回复
-
bug severity-medium XS
难度 2/5 1-3 小时 新手友好度 80/100
nikolas-charalambidis/facepalm-maven-plugin#291 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 62/100
OakLoaf/PluginUpdater#264 ·
维护者通常 1 天内回复