Skip to content

Commit 2d82842

Browse files
committed
services/tasks, linux: ignore shutdown tasks
Because tasks may be deleted while listing containers, we need to ignore errors from state requests that are due to a closed error. All of these get mapped to ErrNotFound, which can be used to filter the entries. There may be a better fix that does a better job of keeping track of the intended state of a backend task. The current condition of assuming that a closed client is a shutdown task may be too naive. Signed-off-by: Stephen J Day <stephen.day@docker.com> (cherry picked from commit c80ca4f) Signed-off-by: Stephen J Day <stephen.day@docker.com>
1 parent fbc6de1 commit 2d82842

File tree

4 files changed

+20
-10
lines changed

4 files changed

+20
-10
lines changed

linux/process.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"github.com/containerd/containerd/errdefs"
1111
shim "github.com/containerd/containerd/linux/shim/v1"
1212
"github.com/containerd/containerd/runtime"
13+
"github.com/pkg/errors"
14+
"github.com/stevvooe/ttrpc"
1315
)
1416

1517
// Process implements a linux process
@@ -44,7 +46,14 @@ func (p *Process) State(ctx context.Context) (runtime.State, error) {
4446
ID: p.id,
4547
})
4648
if err != nil {
47-
return runtime.State{}, errdefs.FromGRPC(err)
49+
if errors.Cause(err) != ttrpc.ErrClosed {
50+
return runtime.State{}, errdefs.FromGRPC(err)
51+
}
52+
53+
// We treat ttrpc.ErrClosed as the shim being closed, but really this
54+
// likely means that the process no longer exists. We'll have to plumb
55+
// the connection differently if this causes problems.
56+
return runtime.State{}, errdefs.ErrNotFound
4857
}
4958
var status runtime.Status
5059
switch response.Status {

linux/shim/service.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func (s *Service) Start(ctx context.Context, r *shimapi.StartRequest) (*shimapi.
129129
defer s.mu.Unlock()
130130
p := s.processes[r.ID]
131131
if p == nil {
132-
return nil, errdefs.ToGRPCf(errdefs.ErrNotFound, "process %s not found", r.ID)
132+
return nil, errdefs.ToGRPCf(errdefs.ErrNotFound, "process %s", r.ID)
133133
}
134134
if err := p.Start(ctx); err != nil {
135135
return nil, err
@@ -238,7 +238,7 @@ func (s *Service) State(ctx context.Context, r *shimapi.StateRequest) (*shimapi.
238238
defer s.mu.Unlock()
239239
p := s.processes[r.ID]
240240
if p == nil {
241-
return nil, errdefs.ToGRPCf(errdefs.ErrNotFound, "process id %s not found", r.ID)
241+
return nil, errdefs.ToGRPCf(errdefs.ErrNotFound, "process id %s", r.ID)
242242
}
243243
st, err := p.Status(ctx)
244244
if err != nil {

linux/task.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ import (
66
"context"
77
"sync"
88

9-
"github.com/pkg/errors"
10-
"google.golang.org/grpc"
11-
129
"github.com/containerd/cgroups"
1310
eventstypes "github.com/containerd/containerd/api/events"
1411
"github.com/containerd/containerd/api/types/task"
@@ -20,6 +17,8 @@ import (
2017
"github.com/containerd/containerd/runtime"
2118
runc "github.com/containerd/go-runc"
2219
"github.com/gogo/protobuf/types"
20+
"github.com/pkg/errors"
21+
"github.com/stevvooe/ttrpc"
2322
)
2423

2524
// Task on a linux based system
@@ -109,7 +108,7 @@ func (t *Task) State(ctx context.Context) (runtime.State, error) {
109108
ID: t.id,
110109
})
111110
if err != nil {
112-
if err != grpc.ErrServerStopped {
111+
if errors.Cause(err) != ttrpc.ErrClosed {
113112
return runtime.State{}, errdefs.FromGRPC(err)
114113
}
115114
return runtime.State{}, errdefs.ErrNotFound

services/tasks/service.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ func (s *service) DeleteProcess(ctx context.Context, r *api.DeleteProcessRequest
224224
func processFromContainerd(ctx context.Context, p runtime.Process) (*task.Process, error) {
225225
state, err := p.State(ctx)
226226
if err != nil {
227-
return nil, errdefs.ToGRPC(err)
227+
return nil, err
228228
}
229229
var status task.Status
230230
switch state.Status {
@@ -267,7 +267,7 @@ func (s *service) Get(ctx context.Context, r *api.GetRequest) (*api.GetResponse,
267267
}
268268
t, err := processFromContainerd(ctx, p)
269269
if err != nil {
270-
return nil, err
270+
return nil, errdefs.ToGRPC(err)
271271
}
272272
return &api.GetResponse{
273273
Process: t,
@@ -290,7 +290,9 @@ func addTasks(ctx context.Context, r *api.ListTasksResponse, tasks []runtime.Tas
290290
for _, t := range tasks {
291291
tt, err := processFromContainerd(ctx, t)
292292
if err != nil {
293-
log.G(ctx).WithError(err).WithField("id", t.ID()).Error("converting task to protobuf")
293+
if !errdefs.IsNotFound(err) { // handle race with deletion
294+
log.G(ctx).WithError(err).WithField("id", t.ID()).Error("converting task to protobuf")
295+
}
294296
continue
295297
}
296298
r.Tasks = append(r.Tasks, tt)

0 commit comments

Comments
 (0)