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 <pholzing@redhat.com>
This commit is contained in:
Paul Holzinger
2023-05-04 13:35:34 +02:00
parent b5387da839
commit 95557a532e
2 changed files with 11 additions and 0 deletions

View File

@ -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.

View File

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