diff --git a/virtcontainers/cc_shim_test.go b/virtcontainers/cc_shim_test.go index 960af3c3ec..3d1c01ed04 100644 --- a/virtcontainers/cc_shim_test.go +++ b/virtcontainers/cc_shim_test.go @@ -242,8 +242,9 @@ func TestCCShimStartDetachSuccessful(t *testing.T) { testCCShimStart(t, sandbox, params, false) - readCh := make(chan error) + readCh := make(chan error, 1) go func() { + defer close(readCh) bufStdout := make([]byte, 1024) n, err := rStdout.Read(bufStdout) if err != nil && err != io.EOF { diff --git a/virtcontainers/hook.go b/virtcontainers/hook.go index 5da2bb6dbe..66c8674064 100644 --- a/virtcontainers/hook.go +++ b/virtcontainers/hook.go @@ -22,6 +22,7 @@ import ( "fmt" "os" "os/exec" + "syscall" "time" specs "github.com/opencontainers/runtime-spec/specs-go" @@ -82,9 +83,11 @@ func (h *Hook) runHook() error { return fmt.Errorf("%s: stdout: %s, stderr: %s", err, stdout.String(), stderr.String()) } } else { - done := make(chan error) - - go func() { done <- cmd.Wait() }() + done := make(chan error, 1) + go func() { + done <- cmd.Wait() + close(done) + }() select { case err := <-done: @@ -92,6 +95,10 @@ func (h *Hook) runHook() error { return fmt.Errorf("%s: stdout: %s, stderr: %s", err, stdout.String(), stderr.String()) } case <-time.After(time.Duration(h.Timeout) * time.Second): + if err := syscall.Kill(cmd.Process.Pid, syscall.SIGKILL); err != nil { + return err + } + return fmt.Errorf("Hook timeout") } } diff --git a/virtcontainers/kata_shim_test.go b/virtcontainers/kata_shim_test.go index 4d43780b0d..4ae1057f3e 100644 --- a/virtcontainers/kata_shim_test.go +++ b/virtcontainers/kata_shim_test.go @@ -236,8 +236,9 @@ func TestKataShimStartDetachSuccessful(t *testing.T) { testKataShimStart(t, sandbox, params, false) - readCh := make(chan error) + readCh := make(chan error, 1) go func() { + defer close(readCh) bufStdout := make([]byte, 1024) n, err := rStdout.Read(bufStdout) if err != nil && err != io.EOF {