Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

Commit

Permalink
Add container not found check in stop & remove
Browse files Browse the repository at this point in the history
Add error checks in StopContainer() and RemoveContainer() to return nil
when the container is not found. This helps avoid failure when VM stop
or remove is executed and the containers don't exist anymore, resulting
in no-op and a successful stop and remove execution.
  • Loading branch information
darkowlzz committed Mar 13, 2021
1 parent 40891c5 commit 6de3da1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
10 changes: 10 additions & 0 deletions pkg/runtime/containerd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,11 @@ func withDevices(devices []*runtime.Bind) oci.SpecOpts {
func (cc *ctdClient) StopContainer(container string, timeout *time.Duration) (err error) {
cont, err := cc.client.LoadContainer(cc.ctx, container)
if err != nil {
// If the container is not found, return nil, no-op.
if errdefs.IsNotFound(err) {
log.Warn(err)
err = nil
}
return
}

Expand All @@ -596,6 +601,11 @@ func (cc *ctdClient) StopContainer(container string, timeout *time.Duration) (er

task, err := cont.Task(cc.ctx, cio.Load)
if err != nil {
// If the task is not found, return nil, no-op.
if errdefs.IsNotFound(err) {
log.Warn(err)
err = nil
}
return
}

Expand Down
10 changes: 10 additions & 0 deletions pkg/runtime/docker/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ func (dc *dockerClient) StopContainer(container string, timeout *time.Duration)
<-readyC // wait until removal detection has started

if err := dc.client.ContainerStop(context.Background(), container, timeout); err != nil {
// If the container is not found, return nil, no-op.
if errdefs.IsNotFound(err) {
log.Warn(err)
return nil
}
return err
}

Expand Down Expand Up @@ -225,6 +230,11 @@ func (dc *dockerClient) RemoveContainer(container string) error {

<-readyC // The ready channel is used to wait until removal detection has started
if err := dc.client.ContainerRemove(context.Background(), container, types.ContainerRemoveOptions{}); err != nil {
// If the container is not found, return nil, no-op.
if errdefs.IsNotFound(err) {
log.Warn(err)
return nil
}
return err
}

Expand Down

0 comments on commit 6de3da1

Please sign in to comment.