testcontainers / testcontainers/testcontainers-python
Feature: Timeout on command execution
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 2.3k
- 派生
- 386
- 平均合并
- 4 小时 40 分钟
- 30 天内合并 PR
- 1
描述
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.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 DockerContainer.exec 和 docs/features/executing_commands.md 开始,然后将文档中的 timeout 示例与 docker-py Container.exec_run 及其限制进行比较。当 exec() 接受 timeout、在超过期限时引发 TimeoutError,并且不会让 test run 无限期阻塞时,即视为完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- docker, python
- 领域
- devops, testing
- Issue 类型
- 功能
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 42/100