From 8ef9f599fdc514502ae7ab447abd7eb1f96e2fee Mon Sep 17 00:00:00 2001 From: dannyward630 Date: Fri, 19 Jun 2026 01:02:08 +0200 Subject: [PATCH] fix(env): stop or kill docker containers on cleanup --- src/minisweagent/environments/docker.py | 4 ++-- tests/environments/test_docker.py | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/minisweagent/environments/docker.py b/src/minisweagent/environments/docker.py index 42e65e35a..430693923 100644 --- a/src/minisweagent/environments/docker.py +++ b/src/minisweagent/environments/docker.py @@ -151,9 +151,9 @@ def _check_finished(self, output: dict): ) def cleanup(self): - """Stop and remove the Docker container.""" + """Stop the Docker container.""" if getattr(self, "container_id", None) is not None: # if init fails early, container_id might not be set - cmd = f"(timeout 60 {self.config.executable} stop {self.container_id} || {self.config.executable} rm -f {self.container_id}) >/dev/null 2>&1 &" + cmd = f"(timeout 60 {self.config.executable} stop {self.container_id} || {self.config.executable} kill {self.container_id}) >/dev/null 2>&1 &" subprocess.Popen(cmd, shell=True) def __del__(self): diff --git a/tests/environments/test_docker.py b/tests/environments/test_docker.py index cfead57ae..6fb9047bf 100644 --- a/tests/environments/test_docker.py +++ b/tests/environments/test_docker.py @@ -53,6 +53,21 @@ def test_docker_environment_config_defaults(executable): assert config.executable == executable +def test_docker_cleanup_stops_or_kills_container(): + """Test that cleanup leaves removal policy to the configured run args.""" + env = DockerEnvironment.__new__(DockerEnvironment) + env.container_id = "minisweagent-test" + env.config = DockerEnvironmentConfig(image="python:3.11", executable="docker") + + with patch("subprocess.Popen") as popen: + env.cleanup() + + popen.assert_called_once_with( + "(timeout 60 docker stop minisweagent-test || docker kill minisweagent-test) >/dev/null 2>&1 &", + shell=True, + ) + + @pytest.mark.slow @pytest.mark.parametrize("executable", environment_params) def test_docker_environment_basic_execution(executable):