{"record":{"id":"7ff3eedbcc3195f7","repo":"juanfont/headscale","slug":"cleaning-stale-test-containers-w","errorCode":null,"errorMessage":"cleaning stale test containers: %w","messagePattern":"cleaning stale test containers: %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/hi/cleanup.go","lineNumber":25,"sourceCode":"\t\"os\"\n\t\"path/filepath\"\n\t\"strings\"\n\t\"time\"\n\n\t\"github.com/cenkalti/backoff/v5\"\n\t\"github.com/docker/docker/api/types/container\"\n\t\"github.com/docker/docker/api/types/filters\"\n\t\"github.com/docker/docker/api/types/image\"\n\t\"github.com/docker/docker/client\"\n\t\"github.com/docker/docker/errdefs\"\n)\n\n// cleanupBeforeTest performs cleanup operations before running tests.\n// Only removes stale (stopped/exited) test containers to avoid interfering with concurrent test runs.\nfunc cleanupBeforeTest(ctx context.Context) error {\n\terr := cleanupStaleTestContainers(ctx)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"cleaning stale test containers: %w\", err)\n\t}\n\n\tif err := pruneDockerNetworks(ctx); err != nil { //nolint:noinlineerr\n\t\treturn fmt.Errorf(\"pruning networks: %w\", err)\n\t}\n\n\treturn nil\n}\n\n// cleanupAfterTest removes the test container and all associated integration test containers for the run.\nfunc cleanupAfterTest(ctx context.Context, cli *client.Client, containerID, runID string) error {\n\t// Remove the main test container\n\terr := cli.ContainerRemove(ctx, containerID, container.RemoveOptions{\n\t\tForce: true,\n\t})\n\tif err != nil {\n\t\treturn fmt.Errorf(\"removing test container: %w\", err)\n\t}","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/juanfont/headscale/blob/565fd254d06c4c7f9a8cad1714a43445c79ba420/cmd/hi/cleanup.go#L7-L43","documentation":"Returned by cleanupBeforeTest in the hi integration-test runner when cleanupStaleTestContainers fails. That helper creates a Docker client and lists containers filtered to status exited/dead, so failures are almost always Docker daemon connectivity, permissions, or a malformed filter. The error wraps the underlying cause with %w, so the text after the colon identifies the real problem.","triggerScenarios":"Running `hi run` (or any command that triggers pre-test cleanup) when the Docker daemon is stopped, DOCKER_HOST points to an unreachable socket, the user lacks permission on /var/run/docker.sock, or the Docker API version on the host rejects the status filter combination.","commonSituations":"Docker Desktop not started; running as a non-root user outside the docker group; DOCKER_HOST/DOCKER_CONTEXT env vars pointing at a stale remote daemon; rootless Docker not running; older Docker engines that do not support the 'dead' status filter.","solutions":["Start Docker and verify with `docker ps` — the same permission context is what the test runner uses","Check DOCKER_HOST and DOCKER_CONTEXT environment variables; unset them or point them at a live daemon","Add your user to the docker group (sudo usermod -aG docker $USER) and re-login, or run with adequate privileges","Read the wrapped error text after 'cleaning stale test containers:' — it names the exact Docker API failure","If the environment has no usable Docker, run `hi doctor` to diagnose the setup"],"exampleFix":"# before: daemon not running\ngo run ./cmd/hi run TestACL\n# error: cleaning stale test containers: ... connect /var/run/docker.sock\n\n# after\nsystemctl start docker   # or start Docker Desktop\ngo run ./cmd/hi run TestACL","handlingStrategy":"retry","validationCode":"// Pre-flight: confirm a Docker daemon is reachable before running hi.\nfunc dockerReachable() bool {\n\tcli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())\n\tif err != nil {\n\t\treturn false\n\t}\n\tdefer cli.Close()\n\t_, err = cli.Info(context.Background())\n\treturn err == nil\n}","typeGuard":null,"tryCatchPattern":"Wrap the hi invocation; on failure inspect errors.Unwrap — daemon connectivity errors are retryable after starting Docker, permission errors are not.","preventionTips":["Run `go run ./cmd/hi doctor` before test sessions","Keep DOCKER_HOST/DOCKER_CONTEXT pointed at a live daemon; unset stale values","Ensure the user is in the docker group before starting work","Note that pre-test cleanup failure is logged as a warning when CleanBefore is set — check verbose output"],"tags":["docker","integration-tests","cleanup","hi"],"backgroundTag":null,"analyzedSha":"565fd254d06c4c7f9a8cad1714a43445c79ba420","analyzedAt":"2026-08-15T13:12:30.133Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}