add timeout when wait to get container pid from conmon

Signed-off-by: Yao Zengzeng <yaozengzeng@zju.edu.cn>
This commit is contained in:
YaoZengzeng 2016-10-13 14:07:22 +08:00
parent 792f585c44
commit 3b7d815af1

View file

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