From 1afcbb8995aa9aebed05c2ec3e0cb59d54ca0793 Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 2 Dec 2026 00:41:33 -0400 Subject: [PATCH] fix(docker): stop waiting for a result file when the container is not running read_from_file only reported container-running=false when the container did not exist at all. get_container_by_name lists all containers, so an exited one kept counting as alive and the loop waited out the whole timeout. Check the status like write_to_file already does. --- lean/components/docker/docker_manager.py | 4 ++++ tests/components/docker/test_docker_manager.py | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/lean/components/docker/docker_manager.py b/lean/components/docker/docker_manager.py index 0f2b32ea..c8346ec5 100644 --- a/lean/components/docker/docker_manager.py +++ b/lean/components/docker/docker_manager.py @@ -539,6 +539,10 @@ def read_from_file(self, docker_container_name: str, docker_file: Path, interval error_message = f"Container {docker_container_name} does not exist" container_running = False break + if docker_container.status != "running": + error_message = f"Container {docker_container_name} is not running" + container_running = False + break p = Popen(command, stdin=PIPE, stdout=PIPE, stderr=PIPE) output = p.stdout.read().decode('utf-8') if output is not None and output != "": diff --git a/tests/components/docker/test_docker_manager.py b/tests/components/docker/test_docker_manager.py index 13408827..44cbe845 100644 --- a/tests/components/docker/test_docker_manager.py +++ b/tests/components/docker/test_docker_manager.py @@ -163,6 +163,24 @@ def test_read_from_file_returns_without_sleeping_when_the_result_is_available() sleep_mock.assert_not_called() +def test_read_from_file_reports_a_stopped_container_instead_of_waiting_for_the_timeout() -> None: + docker_manager = _create_docker_manager() + + container = mock.Mock() + container.status = "exited" + + with mock.patch.object(docker_manager, "get_container_by_name", return_value=container), \ + mock.patch("subprocess.Popen") as popen_mock, \ + mock.patch("time.sleep") as sleep_mock: + result = docker_manager.read_from_file("my-container", Path("command-result.json"), interval=1, timeout=60) + + assert result["success"] is False + assert result["container-running"] is False + assert "is not running" in result["error"] + popen_mock.assert_not_called() + sleep_mock.assert_not_called() + + def test_get_image_label_returns_the_label_of_the_image() -> None: docker_manager = _create_docker_manager()