diff --git a/pkg/domain/infra/abi/pods_stats.go b/pkg/domain/infra/abi/pods_stats.go index 8ece02c2c6..841a8e2972 100644 --- a/pkg/domain/infra/abi/pods_stats.go +++ b/pkg/domain/infra/abi/pods_stats.go @@ -8,6 +8,7 @@ import ( "github.com/containers/common/pkg/cgroups" "github.com/containers/podman/v5/libpod" + "github.com/containers/podman/v5/libpod/define" "github.com/containers/podman/v5/pkg/domain/entities" "github.com/containers/podman/v5/pkg/rootless" "github.com/docker/go-units" @@ -39,6 +40,10 @@ func (ic *ContainerEngine) podsToStatsReport(pods []*libpod.Pod) ([]*entities.Po for i := range pods { // Access by index to prevent potential loop-variable leaks. podStats, err := pods[i].GetPodStats(nil) if err != nil { + // pod was removed, skip it + if errors.Is(err, define.ErrNoSuchPod) { + continue + } return nil, err } podID := pods[i].ID()[:12] diff --git a/pkg/ps/ps.go b/pkg/ps/ps.go index 243d238715..755e04c08f 100644 --- a/pkg/ps/ps.go +++ b/pkg/ps/ps.go @@ -76,7 +76,8 @@ func GetContainerLists(runtime *libpod.Runtime, options entities.ContainerListOp for _, con := range cons { listCon, err := ListContainerBatch(runtime, con, options) switch { - case errors.Is(err, define.ErrNoSuchCtr): + // ignore both no ctr and no such pod errors as it means the ctr is gone now + case errors.Is(err, define.ErrNoSuchCtr), errors.Is(err, define.ErrNoSuchPod): continue case err != nil: return nil, err @@ -148,6 +149,7 @@ func ListContainerBatch(rt *libpod.Runtime, ctr *libpod.Container, opts entities networks []string healthStatus string restartCount uint + podName string ) batchErr := ctr.Batch(func(c *libpod.Container) error { @@ -201,10 +203,6 @@ func ListContainerBatch(rt *libpod.Runtime, ctr *libpod.Container, opts entities return err } - if !opts.Size && !opts.Namespace { - return nil - } - if opts.Namespace { ctrPID := strconv.Itoa(pid) cgroup, _ = getNamespaceInfo(filepath.Join("/proc", ctrPID, "ns", "cgroup")) @@ -231,6 +229,14 @@ func ListContainerBatch(rt *libpod.Runtime, ctr *libpod.Container, opts entities size.RootFsSize = rootFsSize size.RwSize = rwSize } + + if opts.Pod && len(conConfig.Pod) > 0 { + podName, err = rt.GetPodName(conConfig.Pod) + if err != nil { + return fmt.Errorf("could not find container %s pod (id %s) in state: %w", conConfig.ID, conConfig.Pod, err) + } + } + return nil }) if batchErr != nil { @@ -256,6 +262,7 @@ func ListContainerBatch(rt *libpod.Runtime, ctr *libpod.Container, opts entities Networks: networks, Pid: pid, Pod: conConfig.Pod, + PodName: podName, Ports: portMappings, Restarts: restartCount, Size: size, @@ -263,16 +270,6 @@ func ListContainerBatch(rt *libpod.Runtime, ctr *libpod.Container, opts entities State: conState.String(), Status: healthStatus, } - if opts.Pod && len(conConfig.Pod) > 0 { - podName, err := rt.GetPodName(conConfig.Pod) - if err != nil { - if errors.Is(err, define.ErrNoSuchCtr) { - return entities.ListContainer{}, fmt.Errorf("could not find container %s pod (id %s) in state: %w", conConfig.ID, conConfig.Pod, define.ErrNoSuchPod) - } - return entities.ListContainer{}, err - } - ps.PodName = podName - } if opts.Namespace { ps.Namespaces = entities.ListContainerNamespaces{