Repository navigation
Add logging for exceptions and label for Docker container - #351
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces session-based tracking and cleanup for Docker containers created during SWE-bench evaluation and inference tasks. It adds session IDs, wraps the Docker client to automatically label containers, and updates the cleanup logic to target only containers associated with the current session. The review feedback suggests improving error handling and debuggability by checking the return codes of 'docker ps' and 'docker rm' commands and logging failures. Additionally, it recommends handling cases where 'run_args' is configured as a string rather than a list, and updating the unit tests to mock the command execution results accordingly.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| if r.returncode == 0: | ||
| container_ids.update( | ||
| x.strip() for x in r.stdout.strip().splitlines() if x.strip() | ||
| ) |
There was a problem hiding this comment.
When docker ps fails (e.g., due to permission issues or the Docker daemon not running), r.returncode will be non-zero. Since capture_output=True is used, the error output is completely hidden, making troubleshooting difficult. Consider logging a warning with r.stderr when r.returncode != 0.
| if r.returncode == 0: | |
| container_ids.update( | |
| x.strip() for x in r.stdout.strip().splitlines() if x.strip() | |
| ) | |
| if r.returncode == 0: | |
| container_ids.update( | |
| x.strip() for x in r.stdout.strip().splitlines() if x.strip() | |
| ) | |
| else: | |
| _logger.warning( | |
| "Failed to list containers for session %s (exit code %d): %s", | |
| session_id, | |
| r.returncode, | |
| r.stderr.strip() if r.stderr else "", | |
| ) |
| subprocess.run( | ||
| ["docker", "rm", "-f"] + targets, | ||
| capture_output=True, | ||
| timeout=30, | ||
| ) |
There was a problem hiding this comment.
Similarly, if docker rm -f fails, the error is silently ignored because capture_output=True is used without checking r.returncode. Logging a warning with r.stderr when the command fails will greatly improve debuggability.
| subprocess.run( | |
| ["docker", "rm", "-f"] + targets, | |
| capture_output=True, | |
| timeout=30, | |
| ) | |
| r = subprocess.run( | |
| ["docker", "rm", "-f"] + targets, | |
| capture_output=True, | |
| timeout=30, | |
| ) | |
| if r.returncode != 0: | |
| _logger.warning( | |
| "Failed to remove containers %s (exit code %d): %s", | |
| targets, | |
| r.returncode, | |
| r.stderr.decode().strip() if r.stderr else "", | |
| ) |
| run_args = list(environment.get("run_args", ["--rm"])) | ||
| label_flag = f"{SWEBENCH_SESSION_LABEL}={session_id}" | ||
| if label_flag not in run_args: | ||
| run_args.extend(["--label", label_flag]) | ||
| environment["run_args"] = run_args |
There was a problem hiding this comment.
If run_args is configured as a string (e.g., "--rm --network host") rather than a list, calling list(run_args) will split the string into individual characters (e.g., ['-', '-', 'r', 'm', ...]), which will corrupt the arguments. We should handle string values gracefully by splitting them.
run_args = environment.get("run_args", ["--rm"])
if isinstance(run_args, str):
run_args = run_args.split()
else:
run_args = list(run_args)
label_flag = f"{SWEBENCH_SESSION_LABEL}={session_id}"
if label_flag not in run_args:
run_args.extend(["--label", label_flag])
environment["run_args"] = run_args| def test_cleanup_swebench_containers_removes_session_ids(self): | ||
| with patch.object( | ||
| utils, "list_swebench_container_ids", return_value={"session-a", "session-b"} | ||
| ), patch.object(utils.subprocess, "run") as mock_run: | ||
| utils.cleanup_swebench_containers(session_id="session-1") |
There was a problem hiding this comment.
Since we are now checking r.returncode on the result of subprocess.run in cleanup_swebench_containers, we should mock the return value of subprocess.run to return a CompletedProcess with returncode=0 to avoid triggering warnings during unit tests.
| def test_cleanup_swebench_containers_removes_session_ids(self): | |
| with patch.object( | |
| utils, "list_swebench_container_ids", return_value={"session-a", "session-b"} | |
| ), patch.object(utils.subprocess, "run") as mock_run: | |
| utils.cleanup_swebench_containers(session_id="session-1") | |
| def test_cleanup_swebench_containers_removes_session_ids(self): | |
| result = subprocess.CompletedProcess(args=[], returncode=0) | |
| with patch.object( | |
| utils, "list_swebench_container_ids", return_value={"session-a", "session-b"} | |
| ), patch.object(utils.subprocess, "run", return_value=result) as mock_run: | |
| utils.cleanup_swebench_containers(session_id="session-1") |
There was a problem hiding this comment.
Pull request overview
This PR adds session-scoped Docker container labeling and cleanup for SWE-bench inference/evaluation runs, improving safety when multiple tasks run concurrently or are interrupted.
Changes:
- Introduces a per-task SWE-bench
session_idand a standard Docker label key (ais_bench.swebench.session) to tag containers created during a run. - Updates inference/evaluation flows to propagate the session label (via mini-swe-agent run args and a Docker client wrapper) and to clean up only containers from the current session on interruption.
- Adds unit tests covering session container listing/cleanup and label propagation helpers.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| tests/UT/tasks/test_swebench_utils.py | Adds unit tests for session-aware container listing/cleanup and label propagation helpers. |
| ais_bench/benchmark/tasks/swebench/utils.py | Implements session ID generation, Docker client/run-args label injection, and session-filtered cleanup utilities with logging. |
| ais_bench/benchmark/tasks/swebench/swebench_infer.py | Generates a session ID per infer run, injects label into mini-swe-agent Docker args, and performs session-scoped cleanup on interrupt/finally. |
| ais_bench/benchmark/tasks/swebench/swebench_eval.py | Generates a session ID per eval run, wraps Docker client to auto-label created containers, and performs session-scoped cleanup on interrupt. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This pull request introduces a robust session-based mechanism for managing Docker containers used by SWE-bench tasks, ensuring that containers are properly labeled, tracked, and cleaned up for each task session. The changes improve reliability and safety when running multiple concurrent or interrupted tasks, and add comprehensive unit tests for the new utilities.
Session-based Docker container management:
make_swebench_session_id) and added logic to label all Docker containers created during a session with this ID. This enables precise tracking and cleanup of containers per task session. [1] [2] [3]_DockerClientWithSessionLabel,_DockerContainersWithSessionLabel) and helper functions (add_swebench_session_label_to_docker_client,add_swebench_session_label_to_run_args) to ensure that the session label is automatically attached when containers are created, both directly and via mini-swe-agent. [1] [2] [3] [4]Container cleanup improvements:
cleanup_swebench_containersto only stop and remove containers associated with the current session, using the session label for filtering. Added a helper (list_swebench_container_ids) to list containers by session label. [1] [2] [3]Testing and reliability:
tests/UT/tasks/test_swebench_utils.py) covering session ID generation, container listing, cleanup logic, Docker client wrappers, and label propagation to run arguments.General code improvements:
These changes make SWE-bench tasks safer to run in parallel and more robust to interruptions, while also making cleanup operations more precise and less error-prone.
issue #342