shimv2: handle ctx passed by containerd

Sometimes shim process cannot be shutdown because of container list
is not empty. This container list is written in shim service, while
creating container. We find that if containerd cancel its Create
Container Request due to timeout, but runtime didn't handle it properly
and continue creating action, then this container cannot be deleted at
all. So we should make sure the ctx passed to Create Service rpc call
is effective.

Fixes #1088

Signed-off-by: Yves Chan <shanks.cyp@gmail.com>
Signed-off-by: Chelsea Mafrica <chelsea.e.mafrica@intel.com>
This commit is contained in:
Chelsea Mafrica 2020-12-02 14:28:31 -08:00
parent f96cdc1a67
commit 0155fe1260

View File

@ -343,16 +343,27 @@ func (s *service) Create(ctx context.Context, r *taskAPI.CreateTaskRequest) (_ *
s.mu.Lock()
defer s.mu.Unlock()
var c *container
c, err = create(ctx, s, r)
if err != nil {
return nil, err
type Result struct {
container *container
err error
}
ch := make(chan Result, 1)
go func() {
container, err := create(ctx, s, r)
ch <- Result{container, err}
}()
c.status = task.StatusCreated
select {
case <-ctx.Done():
return nil, errors.Errorf("create container timeout: %v", r.ID)
case res := <-ch:
if res.err != nil {
return nil, res.err
}
container := res.container
container.status = task.StatusCreated
s.containers[r.ID] = c
s.containers[r.ID] = container
s.send(&eventstypes.TaskCreate{
ContainerID: r.ID,
@ -372,6 +383,7 @@ func (s *service) Create(ctx context.Context, r *taskAPI.CreateTaskRequest) (_ *
Pid: s.pid,
}, nil
}
}
// Start a process
func (s *service) Start(ctx context.Context, r *taskAPI.StartRequest) (_ *taskAPI.StartResponse, err error) {