Skip to content

Commit ba0c300

Browse files
authored
fix(sandbox): tolerate partial docker inspect failures (#1282)
1 parent 5eeb5ad commit ba0c300

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

rock/utils/system.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,10 @@ def _get_docker_used_host_ports() -> set[int]:
130130
)
131131
if inspect.returncode != 0:
132132
logger.debug(f"docker inspect failed ({inspect.returncode}): {inspect.stderr.strip()}")
133-
return used
133+
# Containers can disappear after ``docker ps`` and before this
134+
# command runs. Docker still writes the successfully inspected
135+
# containers to stdout, so retain that partial snapshot instead
136+
# of discarding every port because one stale ID failed.
134137
for line in inspect.stdout.splitlines():
135138
line = line.strip()
136139
if not line or line == "null":

tests/unit/utils/test_system.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import json
2+
import subprocess
3+
4+
from rock.utils import system
5+
6+
7+
def test_get_docker_used_host_ports_keeps_partial_inspect_results(monkeypatch):
8+
"""A container removed during the scan must not hide surviving ports."""
9+
responses = iter(
10+
[
11+
subprocess.CompletedProcess(
12+
args=["docker", "ps", "-aq"],
13+
returncode=0,
14+
stdout="live-container\nstale-container\n",
15+
stderr="",
16+
),
17+
subprocess.CompletedProcess(
18+
args=["docker", "inspect"],
19+
returncode=1,
20+
stdout=json.dumps({"22555/tcp": [{"HostIp": "", "HostPort": "42463"}]}),
21+
stderr="error: no such object: stale-container",
22+
),
23+
]
24+
)
25+
26+
monkeypatch.setattr(system.subprocess, "run", lambda *args, **kwargs: next(responses))
27+
system._DOCKER_USED_PORTS.clear()
28+
29+
assert system._get_docker_used_host_ports() == {42463}

0 commit comments

Comments
 (0)