Skip to content

Commit

Permalink
Merge pull request google#1894 from Random-Liu/avoid-containerd-race
Browse files Browse the repository at this point in the history
Use backoff to tolerant race condition with containerd.
  • Loading branch information
dashpole authored Feb 23, 2018
2 parents 8ec51bb + d5ee05f commit b817801
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions container/containerd/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ import (
"fmt"
"path"
"strings"
"time"

"github.com/containerd/containerd/errdefs"
cgroupfs "github.com/opencontainers/runc/libcontainer/cgroups/fs"
libcontainerconfigs "github.com/opencontainers/runc/libcontainer/configs"
"golang.org/x/net/context"
Expand Down Expand Up @@ -91,10 +93,28 @@ func newContainerdContainerHandler(
return nil, err
}

taskPid, err := client.TaskPid(ctx, id)
if err != nil {
return nil, err
// Cgroup is created during task creation. When cadvisor sees the cgroup,
// task may not be fully created yet. Use a retry+backoff to tolerant the
// race condition.
// TODO(random-liu): Use cri-containerd client to talk with cri-containerd
// instead. cri-containerd has some internal synchronization to make sure
// `ContainerStatus` only returns result after `StartContainer` finishes.
var taskPid uint32
backoff := 100 * time.Millisecond
retry := 5
for {
taskPid, err = client.TaskPid(ctx, id)
if err == nil {
break
}
retry--
if !errdefs.IsNotFound(err) || retry == 0 {
return nil, err
}
time.Sleep(backoff)
backoff *= 2
}

rootfs := "/"
if !inHostNamespace {
rootfs = "/rootfs"
Expand Down

0 comments on commit b817801

Please sign in to comment.