cleanup: add new --stopped-only option

The podman container cleanup process runs asynchronous and by the time
it gets the lock it is possible another podman process already did the
cleanup and then did a new init() to start it again. If the cleanup
process gets the lock there it will cause very weird things.

This can be observed in the remote start API as CI flakes.

Fixes #23754

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
This commit is contained in:
Paul Holzinger
2024-08-27 14:54:31 +02:00
parent bf74797c69
commit a89fef6e2a
6 changed files with 20 additions and 7 deletions

View File

@@ -371,6 +371,7 @@ type ContainerCleanupOptions struct {
Latest bool
Remove bool
RemoveImage bool
StoppedOnly bool // Only cleanup if the container is stopped, i.e. was running before
}
// ContainerCleanupReport describes the response from a

View File

@@ -313,7 +313,7 @@ func (ic *ContainerEngine) ContainerStop(ctx context.Context, namesOrIds []strin
}
}
} else {
if err = c.Cleanup(ctx); err != nil {
if err = c.Cleanup(ctx, false); err != nil {
// The container could still have been removed, as we unlocked
// after we stopped it.
if errors.Is(err, define.ErrNoSuchCtr) || errors.Is(err, define.ErrCtrRemoved) {
@@ -1320,7 +1320,7 @@ func (ic *ContainerEngine) ContainerCleanup(ctx context.Context, namesOrIds []st
report.RmErr = fmt.Errorf("failed to clean up and remove container %v: %w", ctr.ID(), err)
}
} else {
err := ctr.Cleanup(ctx)
err := ctr.Cleanup(ctx, options.StoppedOnly)
// ignore error if ctr is removed or cannot be cleaned up, likely the ctr was already restarted by another process
if err != nil && !errors.Is(err, define.ErrNoSuchCtr) && !errors.Is(err, define.ErrCtrStateInvalid) {
report.CleanErr = fmt.Errorf("failed to clean up container %v: %w", ctr.ID(), err)