diff --git a/container_test.go b/container_test.go index f621cdae9..8a9ff7ebd 100644 --- a/container_test.go +++ b/container_test.go @@ -1132,3 +1132,133 @@ func TestWaitStoppedProcess(t *testing.T) { } <-finished } + +func TestTaskForceDelete(t *testing.T) { + client, err := newClient(t, address) + if err != nil { + t.Fatal(err) + } + defer client.Close() + + var ( + image Image + ctx, cancel = testContext() + id = t.Name() + ) + defer cancel() + + if runtime.GOOS != "windows" { + image, err = client.GetImage(ctx, testImage) + if err != nil { + t.Error(err) + return + } + } + spec, err := generateSpec(withImageConfig(ctx, image), withProcessArgs("sleep", "30")) + if err != nil { + t.Error(err) + return + } + container, err := client.NewContainer(ctx, id, WithSpec(spec), withNewSnapshot(id, image)) + if err != nil { + t.Error(err) + return + } + defer container.Delete(ctx, WithSnapshotCleanup) + + task, err := container.NewTask(ctx, Stdio) + if err != nil { + t.Error(err) + return + } + if err := task.Start(ctx); err != nil { + t.Error(err) + return + } + if _, err := task.Delete(ctx); err == nil { + t.Error("task.Delete of a running task should create an error") + } + if _, err := task.Delete(ctx, WithProcessKill); err != nil { + t.Error(err) + return + } +} + +func TestProcessForceDelete(t *testing.T) { + client, err := newClient(t, address) + if err != nil { + t.Fatal(err) + } + defer client.Close() + + var ( + image Image + ctx, cancel = testContext() + id = t.Name() + ) + defer cancel() + + if runtime.GOOS != "windows" { + image, err = client.GetImage(ctx, testImage) + if err != nil { + t.Error(err) + return + } + } + spec, err := generateSpec(withImageConfig(ctx, image), withProcessArgs("sleep", "30")) + if err != nil { + t.Error(err) + return + } + container, err := client.NewContainer(ctx, id, WithSpec(spec), withNewSnapshot(id, image)) + if err != nil { + t.Error(err) + return + } + defer container.Delete(ctx, WithSnapshotCleanup) + + task, err := container.NewTask(ctx, Stdio) + if err != nil { + t.Error(err) + return + } + defer task.Delete(ctx) + + statusC := make(chan uint32, 1) + go func() { + status, err := task.Wait(ctx) + if err != nil { + t.Error(err) + } + statusC <- status + }() + + // task must be started on windows + if err := task.Start(ctx); err != nil { + t.Error(err) + return + } + processSpec := spec.Process + withExecArgs(processSpec, "sleep", "20") + execID := t.Name() + "_exec" + process, err := task.Exec(ctx, execID, processSpec, empty()) + if err != nil { + t.Error(err) + return + } + if err := process.Start(ctx); err != nil { + t.Error(err) + return + } + if _, err := process.Delete(ctx); err == nil { + t.Error("process.Delete should return an error when process is running") + } + if _, err := process.Delete(ctx, WithProcessKill); err != nil { + t.Error(err) + } + if err := task.Kill(ctx, syscall.SIGKILL); err != nil { + t.Error(err) + return + } + <-statusC +} diff --git a/process.go b/process.go index 1eb31080f..c2d2119c0 100644 --- a/process.go +++ b/process.go @@ -11,6 +11,7 @@ import ( "github.com/containerd/containerd/runtime" "github.com/containerd/containerd/typeurl" specs "github.com/opencontainers/runtime-spec/specs-go" + "github.com/pkg/errors" ) // Process represents a system process @@ -20,7 +21,7 @@ type Process interface { // Start starts the process executing the user's defined binary Start(context.Context) error // Delete removes the process and any resources allocated returning the exit status - Delete(context.Context) (uint32, error) + Delete(context.Context, ...ProcessDeleteOpts) (uint32, error) // Kill sends the provided signal to the process Kill(context.Context, syscall.Signal) error // Wait blocks until the process has exited returning the exit status @@ -141,7 +142,19 @@ func (p *process) Resize(ctx context.Context, w, h uint32) error { return err } -func (p *process) Delete(ctx context.Context) (uint32, error) { +func (p *process) Delete(ctx context.Context, opts ...ProcessDeleteOpts) (uint32, error) { + for _, o := range opts { + if err := o(ctx, p); err != nil { + return UnknownExitStatus, err + } + } + status, err := p.Status(ctx) + if err != nil { + return UnknownExitStatus, err + } + if status.Status != Stopped { + return UnknownExitStatus, errors.Wrapf(errdefs.ErrFailedPrecondition, "process must be stopped before deletion") + } if p.io != nil { p.io.Wait() p.io.Close() diff --git a/task.go b/task.go index 130d79513..2700b7373 100644 --- a/task.go +++ b/task.go @@ -54,6 +54,8 @@ const ( // Pausing indicates that the process is currently switching from a // running state into a paused state Pausing ProcessStatus = "pausing" + // Unknown indicates that we could not determine the status from the runtime + Unknown ProcessStatus = "unknown" ) // IOCloseInfo allows specific io pipes to be closed on a process @@ -236,7 +238,21 @@ func (t *task) Wait(ctx context.Context) (uint32, error) { // Delete deletes the task and its runtime state // it returns the exit status of the task and any errors that were encountered // during cleanup -func (t *task) Delete(ctx context.Context) (uint32, error) { +func (t *task) Delete(ctx context.Context, opts ...ProcessDeleteOpts) (uint32, error) { + for _, o := range opts { + if err := o(ctx, t); err != nil { + return UnknownExitStatus, err + } + } + status, err := t.Status(ctx) + if err != nil && errdefs.IsNotFound(err) { + return UnknownExitStatus, err + } + switch status.Status { + case Stopped, Unknown, "": + default: + return UnknownExitStatus, errors.Wrapf(errdefs.ErrFailedPrecondition, "task must be stopped before deletion: %s", status.Status) + } if t.io != nil { t.io.Cancel() t.io.Wait() diff --git a/task_opts.go b/task_opts.go index 40b178c3c..271c4ee9c 100644 --- a/task_opts.go +++ b/task_opts.go @@ -2,6 +2,7 @@ package containerd import ( "context" + "syscall" "github.com/containerd/containerd/linux/runcopts" "github.com/containerd/containerd/mount" @@ -25,3 +26,21 @@ func WithExit(r *CheckpointTaskInfo) error { } return nil } + +// ProcessDeleteOpts allows the caller to set options for the deletion of a task +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) + // 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) + // wait for the process to fully stop before letting the rest of the deletion complete + <-s + return nil +}