Do not hold lock when ExponentialBackoff() is called
@@ -695,6 +695,7 @@ func (r *runtimeOCI) UpdateContainerStatus(c *Container) error {
return nil
}
+ stateCmd := func() (*ContainerState, bool, error) {
cmd := exec.Command(r.path, rootFlag, r.root, "state", c.id) // nolint: gosec
if v, found := os.LookupEnv("XDG_RUNTIME_DIR"); found {
cmd.Env = append(cmd.Env, fmt.Sprintf("XDG_RUNTIME_DIR=%s", v))
@@ -712,13 +713,28 @@ func (r *runtimeOCI) UpdateContainerStatus(c *Container) error {
c.state.Finished = time.Now()
c.state.ExitCode = utils.Int32Ptr(255)
}
- return nil
+ return nil, true, nil
+ }
+ state := *c.state
+ if err := json.NewDecoder(bytes.NewBuffer(out)).Decode(&state); err != nil {
+ return &state, false, fmt.Errorf("failed to decode container status for %s: %s", c.id, err)
+ }
+ return &state, false, nil
+ }
+ state, canReturn, err := stateCmd()
+ if err != nil {
+ return err
}
- if err := json.NewDecoder(bytes.NewBuffer(out)).Decode(&c.state); err != nil {
- return fmt.Errorf("failed to decode container status for %s: %s", c.id, err)
+ if canReturn {
+ return nil
}
- if c.state.Status == ContainerStateStopped {
+ if state.Status != ContainerStateStopped {
+ *c.state = *state
+ return nil
+ }
+ // release the lock before waiting
+ c.opLock.Unlock()
exitFilePath := c.exitFilePath()
var fi os.FileInfo
err = kwait.ExponentialBackoff(
@@ -736,6 +752,13 @@ func (r *runtimeOCI) UpdateContainerStatus(c *Container) error {
}
return true, nil
})
+ c.opLock.Lock()
+ // run command again
+ state, _, err2 := stateCmd()
+ if err2 != nil {
+ return err2
+ }
+ *c.state = *state
if err != nil {
logrus.Warnf("failed to find container exit file for %v: %v", c.id, err)
} else {
@@ -758,7 +781,6 @@ func (r *runtimeOCI) UpdateContainerStatus(c *Container) error {
if _, err = os.Stat(oomFilePath); err == nil {
c.state.OOMKilled = true
}
- }
return nil
}
