From 55b6e4c3e8bf10994a419cb314eaa296670d1d7d Mon Sep 17 00:00:00 2001 From: Paul Holzinger Date: Mon, 22 Jul 2024 10:16:18 +0200 Subject: [PATCH] podman pod stats: fix race when ctr process exits Like commit 55749af0c7 but for podman *pod* stats not the normal podman stats. We must ignore ErrCtrStopped here as well as this will happen when the container process exited. While at it remove a useless argument from the function as it was always nil and restructure the logic flow to make it easier to read. Fixes #23334 Signed-off-by: Paul Holzinger --- libpod/pod.go | 16 ++++++++-------- pkg/domain/infra/abi/pods_stats.go | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/libpod/pod.go b/libpod/pod.go index 6057492417..1a3ee3cb42 100644 --- a/libpod/pod.go +++ b/libpod/pod.go @@ -459,7 +459,7 @@ type PodContainerStats struct { } // GetPodStats returns the stats for each of its containers -func (p *Pod) GetPodStats(previousContainerStats map[string]*define.ContainerStats) (map[string]*define.ContainerStats, error) { +func (p *Pod) GetPodStats() (map[string]*define.ContainerStats, error) { p.lock.Lock() defer p.lock.Unlock() @@ -472,15 +472,15 @@ func (p *Pod) GetPodStats(previousContainerStats map[string]*define.ContainerSta } newContainerStats := make(map[string]*define.ContainerStats) for _, c := range containers { - newStats, err := c.GetContainerStats(previousContainerStats[c.ID()]) - // If the container wasn't running, don't include it - // but also suppress the error - if err != nil && !errors.Is(err, define.ErrCtrStateInvalid) { + newStats, err := c.GetContainerStats(nil) + if err != nil { + // If the container wasn't running ignore it + if errors.Is(err, define.ErrCtrStateInvalid) || errors.Is(err, define.ErrCtrStopped) { + continue + } return nil, err } - if err == nil { - newContainerStats[c.ID()] = newStats - } + newContainerStats[c.ID()] = newStats } return newContainerStats, nil } diff --git a/pkg/domain/infra/abi/pods_stats.go b/pkg/domain/infra/abi/pods_stats.go index 841a8e2972..26d070344e 100644 --- a/pkg/domain/infra/abi/pods_stats.go +++ b/pkg/domain/infra/abi/pods_stats.go @@ -38,7 +38,7 @@ func (ic *ContainerEngine) PodStats(ctx context.Context, namesOrIds []string, op func (ic *ContainerEngine) podsToStatsReport(pods []*libpod.Pod) ([]*entities.PodStatsReport, error) { reports := []*entities.PodStatsReport{} for i := range pods { // Access by index to prevent potential loop-variable leaks. - podStats, err := pods[i].GetPodStats(nil) + podStats, err := pods[i].GetPodStats() if err != nil { // pod was removed, skip it if errors.Is(err, define.ErrNoSuchPod) {