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:
Kenfe-Mickael Laventure 2017-08-14 10:34:53 -07:00
parent b02e9a844e
commit 2abaf6e964
No known key found for this signature in database
GPG Key ID: 40CF16616B361216
2 changed files with 10 additions and 3 deletions

View File

@ -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:

View File

@ -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