WithProcessKill will forcefully kill and delete a process
(ctx context.Context, p Process)
| 146 | |
| 147 | // WithProcessKill will forcefully kill and delete a process |
| 148 | func WithProcessKill(ctx context.Context, p Process) error { |
| 149 | // Skip killing tasks with PID 0 |
| 150 | // https://github.com/containerd/containerd/issues/10441 |
| 151 | if p.Pid() == 0 { |
| 152 | return nil |
| 153 | } |
| 154 | |
| 155 | ctx, cancel := context.WithCancel(ctx) |
| 156 | defer cancel() |
| 157 | // ignore errors to wait and kill as we are forcefully killing |
| 158 | // the process and don't care about the exit status |
| 159 | s, err := p.Wait(ctx) |
| 160 | if err != nil { |
| 161 | return err |
| 162 | } |
| 163 | if err := p.Kill(ctx, syscall.SIGKILL, WithKillAll); err != nil { |
| 164 | // Kill might still return an IsNotFound error, even if it actually |
| 165 | // killed the process. |
| 166 | if errdefs.IsNotFound(err) { |
| 167 | select { |
| 168 | case <-ctx.Done(): |
| 169 | return ctx.Err() |
| 170 | case <-s: |
| 171 | return nil |
| 172 | } |
| 173 | } |
| 174 | if errdefs.IsFailedPrecondition(err) { |
| 175 | return nil |
| 176 | } |
| 177 | return err |
| 178 | } |
| 179 | // wait for the process to fully stop before letting the rest of the deletion complete |
| 180 | <-s |
| 181 | return nil |
| 182 | } |
| 183 | |
| 184 | // KillInfo contains information on how to process a Kill action |
| 185 | type KillInfo struct { |