From d7c3ecd0fb56b06982b59c3a4021775a30a39da9 Mon Sep 17 00:00:00 2001 From: "Justin Terry (VM)" Date: Tue, 21 May 2019 13:49:34 -0700 Subject: [PATCH] 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) --- pkg/server/sandbox_stop.go | 13 +++++-------- pkg/server/sandbox_stop_test.go | 7 ++++++- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/pkg/server/sandbox_stop.go b/pkg/server/sandbox_stop.go index 2ca8c9661..a3d5a2f11 100644 --- a/pkg/server/sandbox_stop.go +++ b/pkg/server/sandbox_stop.go @@ -142,18 +142,15 @@ func (c *criService) stopSandboxContainer(ctx context.Context, sandbox sandboxst 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. -func (c *criService) waitSandboxStop(ctx context.Context, sandbox sandboxstore.Sandbox, timeout time.Duration) error { - timeoutTimer := time.NewTimer(timeout) - defer timeoutTimer.Stop() +// waitSandboxStop waits for sandbox to be stopped until context is cancelled or +// the context deadline is exceeded. +func (c *criService) waitSandboxStop(ctx context.Context, sandbox sandboxstore.Sandbox) error { select { case <-ctx.Done(): - return errors.Wrapf(ctx.Err(), "wait sandbox container %q is cancelled", sandbox.ID) - case <-timeoutTimer.C: - return errors.Errorf("wait sandbox container %q stop timeout", sandbox.ID) + return errors.Wrapf(ctx.Err(), "wait sandbox container %q", sandbox.ID) case <-sandbox.Stopped(): return nil } diff --git a/pkg/server/sandbox_stop_test.go b/pkg/server/sandbox_stop_test.go index 93a4f29a8..a1ba500bf 100644 --- a/pkg/server/sandbox_stop_test.go +++ b/pkg/server/sandbox_stop_test.go @@ -62,7 +62,12 @@ func TestWaitSandboxStop(t *testing.T) { cancel() 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) } }