Fix possible deadlock in WithProcessKill
We were not checking the error value of `Kill` leading to deadlock if the process didn't exist. Signed-off-by: Kenfe-Mickael Laventure <mickael.laventure@gmail.com>
This commit is contained in:
parent
b02e9a844e
commit
2abaf6e964
@ -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:
|
||||
|
10
task_opts.go
10
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
|
||||
|
Loading…
Reference in New Issue
Block a user