Bug: Reaper is force-removed at interpreter exit since 4.15.0, session containers are never reaped

未关闭
#1,140 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@pantafive 已经在做这个了。

开始于 2026年10月22日。

  • #1141 来自 @pantafive —— 未关闭

评估

难度
3/5
预计耗时
1-2 天
新手友好度
72/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
docker, python
领域
backend, testing

调研方向

Read the atexit registration for Reaper.delete_instance and the Reaper._container.stop() path in src/testcontainers/core/container.py, then inspect the regression tests around reaper cleanup. Reproduce with the issue's short Python command and check Docker events or the session label after waiting 15 seconds. Done when normal interpreter exit closes the socket, leaves Ryuk running long enough to reap the session containers, and the regression test passes.

由索引模型根据 Issue 内容生成。

描述

Describe the bug

Since 4.15.0, containers started by a process are left running after that process exits normally. Ryuk never reaps them.

#1017 registered Reaper.delete_instance with atexit to close the Ryuk socket before interpreter shutdown. delete_instance() also calls Reaper._container.stop(), which is remove(force=True), so the Ryuk container gets SIGKILL at the same moment the socket closes:

https://github.com/testcontainers/testcontainers-python/blob/6146aa1/src/testcontainers/core/container.py#L544

Ryuk only reaps a session RYUK_RECONNECTION_TIMEOUT (10s by default) after the last connection drops. Killed at exit, it never gets there. Every container of a session that ends normally without stopping its containers itself stays behind. This is the common case Ryuk exists for, e.g. a session-scoped pytest fixture that relies on Ryuk for cleanup.

docker events for the Ryuk container at interpreter exit:

start   <ryuk id>
kill    <ryuk id> sig=9
destroy <ryuk id>

On 4.14.2 the same script leaves nothing behind. With the process ending through os._exit(0), which skips atexit, 4.15.0 works too, and the Ryuk log shows the normal path:

Client disconnected: 172.17.0.1:49870
Timeout waiting for connection
Removed 1 container(s), 0 network(s), 0 volume(s) 0 image(s)

Expected: the process exits, Ryuk removes the session's containers about 10s later.
Actual: Ryuk is force-removed at exit, the session's containers keep running.

To Reproduce

python -c "
from testcontainers.core.container import DockerContainer
c = DockerContainer('alpine:3.20').with_command('sleep infinity').start()
print(c.get_wrapped_container().short_id)
"
sleep 15
docker ps --filter label=org.testcontainers.lang=python
# 4.14.2: empty. 4.15.0 and main: the alpine container is still Up.

Workarounds: pin testcontainers<4.15, or drop the handler after the reaper is created with atexit.unregister(Reaper.delete_instance).

Closing only the socket at exit keeps the fix from #1017 (no ResourceWarning) and lets Ryuk do its job. #1125 hits a 409 from this same atexit call when Ryuk is already being removed. Fixing this means the atexit path no longer runs into it. I'll open a PR with a fix and a regression test.

Runtime environment

  • testcontainers 4.15.0, reproduced on main at 6146aa1
  • Python 3.12.12
  • macOS with Colima (Ubuntu 24.04.4 VM, Docker Engine 29.2.1)
主要语言
Python
星标
2.3k
派生
395
PR 合并指标
30 天内没有已合并 PR

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

testcontainers/testcontainers-python 的其他 Issue

查看 testcontainers/testcontainers-python 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。