From c9fe5d26b3ac26ca448fb6c491a1da46667dc0ce Mon Sep 17 00:00:00 2001 From: Michael Schurter Date: Thu, 21 Feb 2019 11:55:01 -0800 Subject: [PATCH] plugins: squelch context Canceled error logs As far as I can tell this is the most straightforward and resilient way to skip error logging on context cancellation with grpc streams. You cannot compare the error against context.Canceled directly as it is of type `*status.statusError`. The next best solution I found was: ```go resp, err := stream.Recv() if code, ok := err.(interface{ Code() code.Code }); ok { if code.Code == code.Canceled { return } } ``` However I think checking ctx.Err() directly makes the code much easier to read and is resilient against grpc API changes. --- drivers/shared/executor/client.go | 5 +++++ plugins/drivers/client.go | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/drivers/shared/executor/client.go b/drivers/shared/executor/client.go index 1e44788f6..101583605 100644 --- a/drivers/shared/executor/client.go +++ b/drivers/shared/executor/client.go @@ -118,6 +118,11 @@ func (c *grpcExecutorClient) handleStats(ctx context.Context, stream proto.Execu defer close(ch) for { resp, err := stream.Recv() + if ctx.Err() != nil { + // Context canceled; exit gracefully + return + } + if err != nil { if err != io.EOF { c.logger.Error("error receiving stream from Stats executor RPC, closing stream", "error", err) diff --git a/plugins/drivers/client.go b/plugins/drivers/client.go index 8a2e828e7..11babf249 100644 --- a/plugins/drivers/client.go +++ b/plugins/drivers/client.go @@ -282,6 +282,11 @@ func (d *driverPluginClient) handleStats(ctx context.Context, ch chan<- *cstruct defer close(ch) for { resp, err := stream.Recv() + if ctx.Err() != nil { + // Context canceled; exit gracefully + return + } + if err != nil { if err != io.EOF { d.logger.Error("error receiving stream from TaskStats driver RPC, closing stream", "error", err)