virtcontainers: Properly end up go routines using channels

Those different files were all calling into a go routine that was
eventually reporting some result through a go channel. The problem
was the way those routine were implemented, as they were hanging
around forever. Indeed, nothing was actually listening to the channel
in some cases, and those routines never ended.

This was one of the problem detected by the fact that our unit tests
needed more time to pass because when they were all run in parallel,
the resources consumed by those routines were increasing the time
for other tests to complete.

Fixes #208

Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
This commit is contained in:
Sebastien Boeuf 2018-04-13 15:58:20 -07:00
parent 2c3cfed608
commit 92577c635f
3 changed files with 14 additions and 5 deletions

View File

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

View File

@ -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")
}
}

View File

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