Skip to content

Commit 669bcc2

Browse files
committed
Retry containerd task api request when task is in unknown state
In some cases, it appears that containerd task api can return a zero pid when the task is not ready yet. In these cases, the process task state is reported to be in unknown state. In this case, we should retry the containerd task api request as the task can take some time to move away from unknown state and be initialized. Signed-off-by: David Porter <[email protected]>
1 parent 3648766 commit 669bcc2

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

container/containerd/client.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package containerd
1616

1717
import (
1818
"context"
19+
"errors"
1920
"fmt"
2021
"net"
2122
"sync"
@@ -24,6 +25,7 @@ import (
2425
containersapi "github.com/containerd/containerd/api/services/containers/v1"
2526
tasksapi "github.com/containerd/containerd/api/services/tasks/v1"
2627
versionapi "github.com/containerd/containerd/api/services/version/v1"
28+
tasktypes "github.com/containerd/containerd/api/types/task"
2729
ptypes "github.com/gogo/protobuf/types"
2830
"github.com/google/cadvisor/container/containerd/containers"
2931
"github.com/google/cadvisor/container/containerd/errdefs"
@@ -44,6 +46,10 @@ type ContainerdClient interface {
4446
Version(ctx context.Context) (string, error)
4547
}
4648

49+
var (
50+
ErrTaskIsInUnknownState = errors.New("containerd task is in unknown state") // used when process reported in containerd task is in Unknown State
51+
)
52+
4753
var once sync.Once
4854
var ctrdClient ContainerdClient = nil
4955

@@ -114,6 +120,9 @@ func (c *client) TaskPid(ctx context.Context, id string) (uint32, error) {
114120
if err != nil {
115121
return 0, errdefs.FromGRPC(err)
116122
}
123+
if response.Process.Status == tasktypes.StatusUnknown {
124+
return 0, ErrTaskIsInUnknownState
125+
}
117126
return response.Process.Pid, nil
118127
}
119128

container/containerd/handler.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package containerd
1717

1818
import (
1919
"encoding/json"
20+
"errors"
2021
"fmt"
2122
"strings"
2223
"time"
@@ -101,10 +102,14 @@ func newContainerdContainerHandler(
101102
if err == nil {
102103
break
103104
}
104-
retry--
105-
if !errdefs.IsNotFound(err) || retry == 0 {
105+
106+
// Retry when task is not created yet or task is in unknown state (likely in process of initializing)
107+
isRetriableError := errdefs.IsNotFound(err) || errors.Is(err, ErrTaskIsInUnknownState)
108+
if !isRetriableError || retry == 0 {
106109
return nil, err
107110
}
111+
112+
retry--
108113
time.Sleep(backoff)
109114
backoff *= 2
110115
}

0 commit comments

Comments
 (0)