diff --git a/libpod/stats_linux.go b/libpod/stats_linux.go index 36f861aa3e..c2f53fc16f 100644 --- a/libpod/stats_linux.go +++ b/libpod/stats_linux.go @@ -3,6 +3,7 @@ package libpod import ( + "errors" "fmt" "strings" "syscall" @@ -36,6 +37,11 @@ func (c *Container) getPlatformContainerStats(stats *define.ContainerStats, prev // Ubuntu does not have swap memory in cgroups because swap is often not enabled. cgroupStats, err := cgroup.Stat() if err != nil { + // cgroup.Stat() is not an atomic operation, so it is possible that the cgroup is removed + // while Stat() is running. Try to catch this case and return a more specific error. + if (errors.Is(err, unix.ENOENT) || errors.Is(err, unix.ENODEV)) && !cgroupExist(cgroupPath) { + return fmt.Errorf("cgroup %s does not exist: %w", cgroupPath, define.ErrCtrStopped) + } return fmt.Errorf("unable to obtain cgroup stats: %w", err) } conState := c.state.State