{"record":{"id":"74c111ac20be10d7","repo":"vxcontrol/pentagi","slug":"failed-to-stop-container-w","errorCode":null,"errorMessage":"failed to stop container: %w","messagePattern":"failed to stop container: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"backend/pkg/docker/client.go","lineNumber":756,"sourceCode":"\t_, err := dc.db.UpdateContainerStatus(ctx, database.UpdateContainerStatusParams{\n\t\tStatus: database.ContainerStatusStopped,\n\t\tID:     dbID,\n\t})\n\tif err != nil {\n\t\treturn fmt.Errorf(\"database status update failed during container stop: %w\", err)\n\t}\n\n\tlogger.Info(\"container shutdown completed successfully\")\n\n\treturn nil\n}\n\nfunc (dc *dockerClient) RemoveContainer(ctx context.Context, containerID string, dbID int64) error {\n\tlogger := dc.logger.WithContext(ctx).WithField(\"local_id\", containerID)\n\tlogger.Info(\"removing container and associated resources\")\n\n\tif err := dc.StopContainer(ctx, containerID, dbID); err != nil {\n\t\treturn fmt.Errorf(\"failed to stop container: %w\", err)\n\t}\n\n\toptions := client.ContainerRemoveOptions{\n\t\tRemoveVolumes: true,\n\t\tForce:         true,\n\t}\n\tif _, err := dc.client.ContainerRemove(ctx, containerID, options); err != nil {\n\t\tif !cerrdefs.IsNotFound(err) {\n\t\t\treturn fmt.Errorf(\"failed to remove container: %w\", err)\n\t\t}\n\t\t// already gone (removed manually, or a prior call already succeeded);\n\t\t// still mark it deleted below so the database row does not go stale.\n\t\tlogger.WithError(err).Warn(\"container not found\")\n\t}\n\n\t_, err := dc.db.UpdateContainerStatus(ctx, database.UpdateContainerStatusParams{\n\t\tStatus: database.ContainerStatusDeleted,\n\t\tID:     dbID,","sourceCodeStart":738,"sourceCodeEnd":774,"githubUrl":"https://github.com/vxcontrol/pentagi/blob/ea665308baaff015b226f308438a68d929d0f29b/backend/pkg/docker/client.go#L738-L774","documentation":"Wraps a failure from StopContainer during RemoveContainer (backend/pkg/docker/client.go). A container cannot be removed until it is stopped, so any stop failure (Docker daemon error, context cancellation, already-waiting container) aborts removal with this error. The database status is left unchanged.","triggerScenarios":"dc.StopContainer returns an error — either the Docker stop call failed (error 270) or the subsequent DB status update failed (error 271); RemoveContainer propagates it wrapped.","commonSituations":"Daemon unreachable during flow cleanup; paused/dead container that cannot be stopped; Postgres outage while marking the container stopped; race between two teardown paths on the same container.","solutions":["Read the wrapped cause (%w chain) to determine whether it was the Docker stop or the DB update that failed","If the container is already gone (NotFound inside StopContainer), it is safe to proceed — RemoveContainer tolerates missing containers during remove","Force-remove directly with ContainerRemoveOptions{Force:true} if stop keeps failing","Check daemon and database health before retrying the removal"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// verify container exists and is stoppable before removal\ninspect, err := dockerCli.ContainerInspect(ctx, id)\nif err != nil && cerrdefs.IsNotFound(err) {\n    return nil // nothing to remove\n}","typeGuard":"func isTeardownBlocked(err error) bool {\n    return strings.Contains(err.Error(), \"failed to stop container\")\n}","tryCatchPattern":"if err := dc.RemoveContainer(ctx, id, dbID); err != nil {\n    log.WithError(err).Error(\"container teardown failed\")\n    // schedule re-clean: mark row stale and retry later\n}","preventionTips":["Read the full %w error chain to find the real failing stage","Ensure only one teardown path runs per container (idempotency guard)","Check daemon + DB health before initiating removal","Prefer Force removal for containers that repeatedly fail to stop"],"tags":["docker","container-lifecycle","error-wrapping"],"backgroundTag":"docker-container-stop-failed","analyzedSha":"ea665308baaff015b226f308438a68d929d0f29b","analyzedAt":"2026-09-01T14:16:31.421Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}