diff --git a/container_test.go b/container_test.go index ab4ecbba4..aa13e8d04 100644 --- a/container_test.go +++ b/container_test.go @@ -823,9 +823,8 @@ func TestContainerNoBinaryExists(t *testing.T) { if err != nil { t.Fatalf("failed to create task %v", err) } - defer task.Delete(ctx) + defer task.Delete(ctx, WithProcessKill) if err := task.Start(ctx); err == nil { - task.Kill(ctx, syscall.SIGKILL) t.Error("task.Start() should return an error when binary does not exist") } default: diff --git a/task_opts.go b/task_opts.go index 271c4ee9c..0e4b75e85 100644 --- a/task_opts.go +++ b/task_opts.go @@ -4,6 +4,7 @@ import ( "context" "syscall" + "github.com/containerd/containerd/errdefs" "github.com/containerd/containerd/linux/runcopts" "github.com/containerd/containerd/mount" ) @@ -33,13 +34,20 @@ type ProcessDeleteOpts func(context.Context, Process) error // WithProcessKill will forcefully kill and delete a process func WithProcessKill(ctx context.Context, p Process) error { s := make(chan struct{}, 1) + ctx, cancel := context.WithCancel(ctx) + defer cancel() // ignore errors to wait and kill as we are forcefully killing // the process and don't care about the exit status go func() { p.Wait(ctx) close(s) }() - p.Kill(ctx, syscall.SIGKILL) + if err := p.Kill(ctx, syscall.SIGKILL); err != nil { + if errdefs.IsFailedPrecondition(err) || errdefs.IsNotFound(err) { + return nil + } + return err + } // wait for the process to fully stop before letting the rest of the deletion complete <-s return nil