Merge pull request #17126 from vrothberg/fix-16142

container kill: handle stopped/exited container
This commit is contained in:
OpenShift Merge Robot
2023-01-16 13:24:09 -05:00
committed by GitHub
2 changed files with 14 additions and 8 deletions

View File

@ -429,15 +429,20 @@ func (r *ConmonOCIRuntime) StopContainer(ctr *Container, timeout uint, all bool)
} }
if err := r.KillContainer(ctr, uint(unix.SIGKILL), all); err != nil { if err := r.KillContainer(ctr, uint(unix.SIGKILL), all); err != nil {
// If the PID is 0, then the container is already stopped. // Ignore the error if KillContainer complains about it already
if ctr.state.PID == 0 { // being stopped or exited. There's an inherent race with the
return nil // cleanup process (see #16142).
if !(errors.Is(err, define.ErrCtrStateInvalid) && ctr.ensureState(define.ContainerStateStopped, define.ContainerStateExited)) {
// If the PID is 0, then the container is already stopped.
if ctr.state.PID == 0 {
return nil
}
// Again, check if the container is gone. If it is, exit cleanly.
if aliveErr := unix.Kill(ctr.state.PID, 0); errors.Is(aliveErr, unix.ESRCH) {
return nil
}
return fmt.Errorf("sending SIGKILL to container %s: %w", ctr.ID(), err)
} }
// Again, check if the container is gone. If it is, exit cleanly.
if aliveErr := unix.Kill(ctr.state.PID, 0); errors.Is(aliveErr, unix.ESRCH) {
return nil
}
return fmt.Errorf("sending SIGKILL to container %s: %w", ctr.ID(), err)
} }
// Give runtime a few seconds to make it happen // Give runtime a few seconds to make it happen

View File

@ -608,6 +608,7 @@ func (r *Runtime) setupContainer(ctx context.Context, ctr *Container) (_ *Contai
// be removed also if and only if the container is the sole user // be removed also if and only if the container is the sole user
// Otherwise, RemoveContainer will return an error if the container is running // Otherwise, RemoveContainer will return an error if the container is running
func (r *Runtime) RemoveContainer(ctx context.Context, c *Container, force bool, removeVolume bool, timeout *uint) error { func (r *Runtime) RemoveContainer(ctx context.Context, c *Container, force bool, removeVolume bool, timeout *uint) error {
// NOTE: container will be locked down the road.
return r.removeContainer(ctx, c, force, removeVolume, false, false, timeout) return r.removeContainer(ctx, c, force, removeVolume, false, false, timeout)
} }