Skip to content

Add logging for exceptions and label for Docker container - #351

Merged
SJTUyh merged 3 commits into
AISBench:masterfrom
zhongzhouTan-coder:fix/container-remove
Jun 23, 2026
Merged

SJTUyh merged 3 commits into
AISBench:masterfrom
zhongzhouTan-coder:fix/container-remove

Conversation

@zhongzhouTan-coder

@zhongzhouTan-coder zhongzhouTan-coder commented Jun 23, 2026 •

Copy link
Copy Markdown
Contributor

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:

  • Introduced a unique session ID for each SWE-bench task (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]
  • Added wrapper classes (_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:

  • Refactored cleanup_swebench_containers to 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]
  • Updated interrupt and shutdown handlers in both evaluation and inference tasks to use session-aware cleanup, preventing accidental removal of unrelated containers. [1] [2]

Testing and reliability:

  • Added a comprehensive unit test suite (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:

  • Improved error handling and logging in Docker interaction utilities, and made minor code quality improvements (formatting, error messages, and type annotations). [1] [2] [3]

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

Copilot AI review requested due to automatic review settings June 23, 2026 02:49

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +99 to 102
if r.returncode == 0:
container_ids.update(
x.strip() for x in r.stdout.strip().splitlines() if x.strip()
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
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 "",
)

Comment on lines +124 to +128
subprocess.run(
["docker", "rm", "-f"] + targets,
capture_output=True,
timeout=30,
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
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 "",
)

Comment on lines +140 to +144
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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

Comment on lines +41 to +45
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")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
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")

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_id and 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.

@SJTUyh
SJTUyh merged commit f8e3e76 into AISBench:master Jun 23, 2026
6 checks passed

This branch was previously deployed

1 inactive deployment
smoke-test-approval — ab7a2a24 Deployed Jun 23, 2026 by zhongzhouTan-coder via run_smoke_test_cases #378
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants