Delete deletes the task and its runtime state it returns the exit status of the task and any errors that were encountered during cleanup
(ctx context.Context, opts ...ProcessDeleteOpts)
| 370 | // it returns the exit status of the task and any errors that were encountered |
| 371 | // during cleanup |
| 372 | func (t *task) Delete(ctx context.Context, opts ...ProcessDeleteOpts) (*ExitStatus, error) { |
| 373 | ctx, span := tracing.StartSpan(ctx, tracing.Name("client.task", "Delete"), |
| 374 | tracing.WithAttribute("task.id", t.ID()), |
| 375 | tracing.WithNamespace(ctx), |
| 376 | ) |
| 377 | defer span.End() |
| 378 | for _, o := range opts { |
| 379 | if err := o(ctx, t); err != nil { |
| 380 | return nil, err |
| 381 | } |
| 382 | } |
| 383 | status, err := t.Status(ctx) |
| 384 | if err != nil && errdefs.IsNotFound(err) { |
| 385 | return nil, err |
| 386 | } |
| 387 | |
| 388 | runtime, err := t.client.defaultRuntime(ctx) |
| 389 | if err != nil { |
| 390 | return nil, fmt.Errorf("failed to get default runtime: %w", err) |
| 391 | } |
| 392 | |
| 393 | switch status.Status { |
| 394 | case Stopped, Unknown, "": |
| 395 | case Created: |
| 396 | if runtime == plugins.RuntimePlugin.String()+".windows" { |
| 397 | // On windows Created is akin to Stopped |
| 398 | break |
| 399 | } |
| 400 | if t.pid == 0 { |
| 401 | // allow for deletion of created tasks with PID 0 |
| 402 | // https://github.com/containerd/containerd/issues/7357 |
| 403 | break |
| 404 | } |
| 405 | fallthrough |
| 406 | default: |
| 407 | return nil, fmt.Errorf("task must be stopped before deletion: %s: %w", status.Status, errdefs.ErrFailedPrecondition) |
| 408 | } |
| 409 | if t.io != nil { |
| 410 | // io.Wait locks for restored tasks on Windows unless we call |
| 411 | // io.Close first (https://github.com/containerd/containerd/issues/5621) |
| 412 | // in other cases, preserve the contract and let IO finish before closing |
| 413 | if runtime == plugins.RuntimePlugin.String()+".windows" { |
| 414 | t.io.Close() |
| 415 | } |
| 416 | // io.Cancel is used to cancel the io goroutine while it is in |
| 417 | // fifo-opening state. It does not stop the pipes since these |
| 418 | // should be closed on the shim's side, otherwise we might lose |
| 419 | // data from the container! |
| 420 | t.io.Cancel() |
| 421 | t.io.Wait() |
| 422 | } |
| 423 | r, err := t.client.TaskService().Delete(ctx, &tasks.DeleteTaskRequest{ |
| 424 | ContainerID: t.id, |
| 425 | }) |
| 426 | if err != nil { |
| 427 | return nil, errgrpc.ToNative(err) |
| 428 | } |
| 429 |
nothing calls this directly
no test coverage detected