From 3b7d815af1d1102fc5d834126a9a530a999d1021 Mon Sep 17 00:00:00 2001 From: YaoZengzeng Date: Thu, 13 Oct 2016 14:07:22 +0800 Subject: [PATCH] add timeout when wait to get container pid from conmon Signed-off-by: Yao Zengzeng --- oci/oci.go | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/oci/oci.go b/oci/oci.go index b3395236..7189f1ac 100644 --- a/oci/oci.go +++ b/oci/oci.go @@ -31,6 +31,8 @@ const ( ContainerStateRunning = "running" // ContainerStateStopped represents the stopped state of a container ContainerStateStopped = "stopped" + // ContainerCreateTimeout represents the value of container creating timeout + ContainerCreateTimeout = 3 * time.Second ) // New creates a new Runtime with options provided @@ -154,12 +156,29 @@ func (r *Runtime) CreateContainer(c *Container, cgroupParent string) error { } // Wait to get container pid from conmon - // TODO(mrunalp): Add a timeout here - var si *syncInfo - if err := json.NewDecoder(parentPipe).Decode(&si); err != nil { - return fmt.Errorf("reading pid from init pipe: %v", err) + type syncStruct struct { + si *syncInfo + err error + } + ch := make(chan syncStruct) + go func() { + var si *syncInfo + if err = json.NewDecoder(parentPipe).Decode(&si); err != nil { + ch <- syncStruct{err: err} + return + } + ch <- syncStruct{si: si} + }() + + select { + case ss := <-ch: + if ss.err != nil { + return err + } + logrus.Infof("Received container pid: %q", ss.si.Pid) + case <-time.After(ContainerCreateTimeout): + return fmt.Errorf("create container timeout") } - logrus.Infof("Received container pid: %v", si.Pid) return nil }