RunPodSandbox should block unless client context is canceled

A call to RunPodSandbox should only return timeout if the operation has timed
out because the clients context deadline was exceeded. On client cancelation
it should return gRPC Canceled otherwise it should block until the sandbox has
exited.

Signed-off-by: Justin Terry (VM) <juterry@microsoft.com>
This commit is contained in:
Justin Terry (VM) 2019-05-21 13:49:34 -07:00
parent 98c266fb86
commit d7c3ecd0fb
2 changed files with 11 additions and 9 deletions

View File

@ -142,18 +142,15 @@ func (c *criService) stopSandboxContainer(ctx context.Context, sandbox sandboxst
return errors.Wrap(err, "failed to kill sandbox container") return errors.Wrap(err, "failed to kill sandbox container")
} }
return c.waitSandboxStop(ctx, sandbox, killContainerTimeout) return c.waitSandboxStop(ctx, sandbox)
} }
// waitSandboxStop waits for sandbox to be stopped until timeout exceeds or context is cancelled. // waitSandboxStop waits for sandbox to be stopped until context is cancelled or
func (c *criService) waitSandboxStop(ctx context.Context, sandbox sandboxstore.Sandbox, timeout time.Duration) error { // the context deadline is exceeded.
timeoutTimer := time.NewTimer(timeout) func (c *criService) waitSandboxStop(ctx context.Context, sandbox sandboxstore.Sandbox) error {
defer timeoutTimer.Stop()
select { select {
case <-ctx.Done(): case <-ctx.Done():
return errors.Wrapf(ctx.Err(), "wait sandbox container %q is cancelled", sandbox.ID) return errors.Wrapf(ctx.Err(), "wait sandbox container %q", sandbox.ID)
case <-timeoutTimer.C:
return errors.Errorf("wait sandbox container %q stop timeout", sandbox.ID)
case <-sandbox.Stopped(): case <-sandbox.Stopped():
return nil return nil
} }

View File

@ -62,7 +62,12 @@ func TestWaitSandboxStop(t *testing.T) {
cancel() cancel()
ctx = cancelledCtx ctx = cancelledCtx
} }
err := c.waitSandboxStop(ctx, sandbox, test.timeout) if test.timeout > 0 {
timeoutCtx, cancel := context.WithTimeout(ctx, test.timeout)
defer cancel()
ctx = timeoutCtx
}
err := c.waitSandboxStop(ctx, sandbox)
assert.Equal(t, test.expectErr, err != nil, desc) assert.Equal(t, test.expectErr, err != nil, desc)
} }
} }