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() {