Skip to content

The agent that grows with you

fix(docker): drop -f from orphan reaper rm so running containers fail safe

beardthelione885d9cdc2fa
diff --git a/tests/tools/test_docker_environment.py b/tests/tools/test_docker_environment.py
index 2aa88533244..ce78514946c 100644
--- a/tests/tools/test_docker_environment.py
+++ b/tests/tools/test_docker_environment.py
@@ -1514,6 +1514,51 @@ def test_reap_orphan_continues_after_individual_rm_failure(monkeypatch):
)
+def test_reap_orphan_uses_plain_rm_so_running_containers_fail_safe(monkeypatch):
+ """``docker rm -f`` defeats the exited-only filter: a sibling can restart an
+ exited container between the ``docker ps`` snapshot and the rm (the reuse
+ path legitimately ``docker start``s exited containers), and ``FinishedAt``
+ still reports the previous exit. Plain ``docker rm`` lets the daemon refuse
+ removal of a running container atomically; every intended target is
+ already exited, so ``-f`` buys nothing."""
+ old = _now_iso(offset_seconds=900)
+ rm_calls = []
+
+ def _run(cmd, **kwargs):
+ if not isinstance(cmd, list) or len(cmd) < 2:
+ return subprocess.CompletedProcess(cmd, 0, stdout="", stderr="")
+ sub = cmd[1]
+ if sub == "ps":
+ return subprocess.CompletedProcess(
+ cmd, 0, stdout="cid-a\ncid-b\n", stderr="",
+ )
+ if sub == "inspect":
+ return subprocess.CompletedProcess(cmd, 0, stdout=old + "\n", stderr="")
+ if sub == "rm":
+ rm_calls.append(list(cmd))
+ if cmd[-1] == "cid-b":
+ # cid-b was restarted by a sibling after the ps snapshot: the
+ # daemon refuses a plain rm on a running container.
+ return subprocess.CompletedProcess(
+ cmd, 1, stdout="",
+ stderr='cannot remove container "cid-b": container is running',
+ )
+ return subprocess.CompletedProcess(cmd, 0, stdout="", stderr="")
+ return subprocess.CompletedProcess(cmd, 0, stdout="", stderr="")
+
+ monkeypatch.setattr(docker_env.subprocess, "run", _run)
+
+ removed = docker_env.reap_orphan_containers(
+ max_age_seconds=600, profile_filter="default", docker_exe="/usr/bin/docker",
+ )
+
+ assert removed == 1
+ assert [c[-1] for c in rm_calls] == ["cid-a", "cid-b"]
+ assert all("-f" not in c for c in rm_calls), (
+ f"reaper must not force-remove containers: {rm_calls}"
+ )
+
+
def test_container_finished_at_parses_nanosecond_timestamp(monkeypatch):
"""Docker emits FinishedAt with nanosecond precision (RFC3339 with up to
9 fractional digits), but Python's fromisoformat caps at microseconds.
diff --git a/tools/environments/docker.py b/tools/environments/docker.py
index 1238a219322..a9a79bfa546 100644
--- a/tools/environments/docker.py
+++ b/tools/environments/docker.py
@@ -152,15 +152,18 @@ def reap_orphan_containers(
age = (now - finished_at).total_seconds()
if age < max_age_seconds:
continue
+ # No -f: a sibling may have restarted the container between the ps snapshot
+ # and now (FinishedAt still reports the previous exit), and the daemon refuses
+ # a plain rm on a running container, which is the atomic recheck this sweep needs.
result = _docker_query(
- [docker, "rm", "-f", cid], timeout=30, fail="orphan reaper docker rm %s failed: %s", fail_args=(cid[:12],))
+ [docker, "rm", cid], timeout=30, fail="orphan reaper docker rm %s failed: %s", fail_args=(cid[:12],))
if result is None:
continue
if result.returncode == 0:
removed += 1
logger.info("Reaped orphan container %s (exited %d seconds ago)", cid[:12], int(age))
else:
- logger.debug("docker rm -f %s failed: %s", cid[:12], result.stderr.strip())
+ logger.debug("docker rm %s failed: %s", cid[:12], result.stderr.strip())
return removed