Merge pull request #129 from YaoZengzeng/add-create-container-timeout

add timeout when wait to get container pid from conmon
This commit is contained in:
Antonio Murdaca 2017-03-20 18:45:47 +01:00 committed by GitHub
commit 526f104f21

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
}