From 0eac57ed3140656d3b23a425c2df671adcd56756 Mon Sep 17 00:00:00 2001 From: Paul Holzinger Date: Thu, 24 Apr 2025 15:32:50 +0200 Subject: [PATCH] podman start: remove container if needed Like podman run --rm, start --attach must also ensure the contianer is removed before it exist. Otherwise there is a race where the container still exist after the command exits, because removal would only happen by the cleanup process in the background. Signed-off-by: Paul Holzinger --- pkg/domain/infra/abi/containers.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/pkg/domain/infra/abi/containers.go b/pkg/domain/infra/abi/containers.go index 47fa65ef00..e7111dc5ba 100644 --- a/pkg/domain/infra/abi/containers.go +++ b/pkg/domain/infra/abi/containers.go @@ -958,6 +958,12 @@ func (ic *ContainerEngine) ContainerStart(ctx context.Context, namesOrIds []stri for i := range containers { ctr := containers[i] + removeContainer := func() { + if _, _, err := ic.removeContainer(ctx, ctr.Container, entities.RmOptions{}); err != nil { + logrus.Errorf("Removing container %s: %v", ctr.ID(), err) + } + } + if options.Attach { err = terminal.StartAttachCtr(ctx, ctr.Container, options.Stdout, options.Stderr, options.Stdin, options.DetachKeys, options.SigProxy, true) if errors.Is(err, define.ErrDetach) { @@ -991,9 +997,7 @@ func (ic *ContainerEngine) ContainerStart(ctx context.Context, namesOrIds []stri ExitCode: exitCode, }) if ctr.AutoRemove() { - if _, _, err := ic.removeContainer(ctx, ctr.Container, entities.RmOptions{}); err != nil { - logrus.Errorf("Removing container %s: %v", ctr.ID(), err) - } + removeContainer() } return reports, fmt.Errorf("unable to start container %s: %w", ctr.ID(), err) } @@ -1002,6 +1006,9 @@ func (ic *ContainerEngine) ContainerStart(ctx context.Context, namesOrIds []stri if err2 != nil { logrus.Errorf("Waiting for container %s: %v", ctr.ID(), err2) } + if ctr.AutoRemove() && !ctr.ShouldRestart(ctx) { + removeContainer() + } reports = append(reports, &entities.ContainerStartReport{ Id: ctr.ID(), RawInput: ctr.rawInput, @@ -1038,9 +1045,7 @@ func (ic *ContainerEngine) ContainerStart(ctx context.Context, namesOrIds []stri } report.Err = fmt.Errorf("unable to start container %q: %w", ctr.ID(), err) if ctr.AutoRemove() { - if _, _, err := ic.removeContainer(ctx, ctr.Container, entities.RmOptions{}); err != nil { - logrus.Errorf("Removing container %s: %v", ctr.ID(), err) - } + removeContainer() } reports = append(reports, report) continue