Skip to content

Fix error during test cleanup when stopping non-running container (b/496885581) - #1674

Closed
hitnik wants to merge 5 commits into
devfrom
fix-ai/container-stop-cleanup-496885581
Closed

hitnik wants to merge 5 commits into
devfrom
fix-ai/container-stop-cleanup-496885581

Conversation

@hitnik

@hitnik hitnik commented Oct 28, 2026

Copy link
Copy Markdown
Collaborator

Summary / Problem

During test cleanup, stopping or killing containers (such as tr-ct-dhcp-2 or tr-ui) when they have already exited or stopped caused Docker to raise HTTP 409 Conflict (Container ... is not running). This resulted in erroneous error logging ("Container stop error") during standard cleanup.
Linked Buganizer issue: b/496885581 ("[TR] Error during test cleanup: Attempting to kill non-running container tr-ct-dhcp-2").

Root Cause

  1. docker_module.py and network_validator.py did not check container.status == 'running' before calling container.stop() or container.kill(), or did not handle docker.errors.APIError gracefully when status code 409/304 was returned.
  2. In testrun.py, _stop_ui unconditionally called container.kill() without verifying whether the container was running, and caught only docker.errors.NotFound instead of also handling docker.errors.APIError.

Changes Made

  1. framework/python/src/core/docker/docker_module.py:

    • Added import docker.errors.
    • In stop(self, kill=False):
      • Check if container.status == 'running': before stopping/killing.
      • If not running, log at DEBUG: Container <container_name> is not running (status: <status>).
      • Caught docker.errors.NotFound and log at DEBUG.
      • Caught docker.errors.APIError as error: if error.status_code in (304, 409) or 'not running' in str(error).lower(), log at DEBUG: Container <container_name> is already stopped: <error>, otherwise log at ERROR.
  2. framework/python/src/net_orc/network_validator.py:

    • Added import docker.errors.
    • In _stop_network_device(self, net_device, kill=False):
      • Check if container.status == 'running': before stopping/killing.
      • If not running, log at DEBUG: Container <container_name> is not running (status: <status>).
      • Caught docker.errors.NotFound and log at DEBUG.
      • Caught docker.errors.APIError as e: if e.status_code in (304, 409) or 'not running' in str(e).lower(), log at DEBUG: Container <container_name> is already stopped: <e>, otherwise log at ERROR.
  3. framework/python/src/core/testrun.py:

    • In _stop_ui(self):
      • Check if container.status == 'running': before calling container.kill().
      • In the outer try/except, catch (docker.errors.NotFound, docker.errors.APIError): pass.

Verification Plan

  1. Run Testrun module execution and cleanup with containers that exit normally before cleanup (such as DHCP validator/testing containers).
  2. Verify that stopping an already stopped/exited container logs a debug message rather than an ERROR.
  3. Verify that _stop_ui exits cleanly whether tr-ui is running, exited, or not found.
  4. Run automated test suite: pytest testing/unit/.

Branch comparison against dev: dev...fix-ai/container-stop-cleanup-496885581

@hitnik hitnik closed this Oct 28, 2026
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.

1 participant