From 89c051f81fca78ab9a45f3d297daf6cf91658d04 Mon Sep 17 00:00:00 2001 From: Mrunal Patel Date: Tue, 27 Sep 2016 11:35:40 -0700 Subject: [PATCH] Make sure that the container stops when StopContainer API is called Signed-off-by: Mrunal Patel --- oci/oci.go | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/oci/oci.go b/oci/oci.go index 2b65ca30..802148b6 100644 --- a/oci/oci.go +++ b/oci/oci.go @@ -17,6 +17,7 @@ import ( "github.com/Sirupsen/logrus" "github.com/kubernetes-incubator/cri-o/utils" "github.com/opencontainers/runtime-spec/specs-go" + "golang.org/x/sys/unix" ) // New creates a new Runtime with options provided @@ -132,8 +133,27 @@ func (r *Runtime) StartContainer(c *Container) error { // StopContainer stops a container. func (r *Runtime) StopContainer(c *Container) error { - // TODO: Check if it is still running after some time and send SIGKILL - return utils.ExecCmdWithStdStreams(os.Stdin, os.Stdout, os.Stderr, r.path, "kill", c.name) + if err := utils.ExecCmdWithStdStreams(os.Stdin, os.Stdout, os.Stderr, r.path, "kill", c.name); err != nil { + return err + } + i := 0 + for { + if i == 1000 { + err := unix.Kill(c.state.Pid, syscall.SIGKILL) + if err != nil && err != syscall.ESRCH { + return fmt.Errorf("failed to kill process: %v", err) + } + } + // Check if the process is still around + err := unix.Kill(c.state.Pid, 0) + if err == syscall.ESRCH { + break + } + time.Sleep(10 * time.Millisecond) + i++ + } + + return nil } // DeleteContainer deletes a container.