From 95557a532e86fd9315a583b8407f55231f33c7b5 Mon Sep 17 00:00:00 2001 From: Paul Holzinger Date: Thu, 4 May 2023 13:35:34 +0200 Subject: [PATCH] libpod: do not Cleanup() more than once If the container was already cleaned up we should not try to do it again. Podman stop will always try to call Cleanup() if you look at the podman event log and just keep calling podman stop --all you see a cleanup event every time. This is not wanted. Also in case of the host pidns we report a error every single time, see the linked issue. Fixes #18460 Signed-off-by: Paul Holzinger --- libpod/container_api.go | 5 +++++ test/e2e/stop_test.go | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/libpod/container_api.go b/libpod/container_api.go index f928e51667..8627a3ea7a 100644 --- a/libpod/container_api.go +++ b/libpod/container_api.go @@ -765,6 +765,11 @@ func (c *Container) Cleanup(ctx context.Context) error { return fmt.Errorf("container %s is running or paused, refusing to clean up: %w", c.ID(), define.ErrCtrStateInvalid) } + // if the container was not created in the oci runtime or was already cleaned up, then do nothing + if c.ensureState(define.ContainerStateConfigured, define.ContainerStateExited) { + return nil + } + // Handle restart policy. // Returns a bool indicating whether we actually restarted. // If we did, don't proceed to cleanup - just exit. diff --git a/test/e2e/stop_test.go b/test/e2e/stop_test.go index d77682413f..0e15263c7f 100644 --- a/test/e2e/stop_test.go +++ b/test/e2e/stop_test.go @@ -126,6 +126,12 @@ var _ = Describe("Podman stop", func() { finalCtrs.WaitWithDefaultTimeout() Expect(finalCtrs).Should(Exit(0)) Expect(strings.TrimSpace(finalCtrs.OutputToString())).To(Equal("")) + + // make sure we only have one cleanup event for this container + events := podmanTest.Podman([]string{"events", "--since=30s", "--stream=false"}) + events.WaitWithDefaultTimeout() + Expect(events).Should(Exit(0)) + Expect(strings.Count(events.OutputToString(), "container cleanup")).To(Equal(1), "cleanup event should show up exactly once") }) It("podman stop all containers -t", func() {