From d34f7f3df5837babe78d4f8e2ab51d3b4f4c36a7 Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Thu, 14 Nov 2019 11:20:43 +0100 Subject: [PATCH] fix unobserved task exception problem for non-exhausted response streams --- src/csharp/Grpc.Core/Internal/AsyncCall.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/csharp/Grpc.Core/Internal/AsyncCall.cs b/src/csharp/Grpc.Core/Internal/AsyncCall.cs index 830a1f4edc6..899af94fb6c 100644 --- a/src/csharp/Grpc.Core/Internal/AsyncCall.cs +++ b/src/csharp/Grpc.Core/Internal/AsyncCall.cs @@ -626,6 +626,14 @@ namespace Grpc.Core.Internal if (status.StatusCode != StatusCode.OK) { streamingResponseCallFinishedTcs.SetException(new RpcException(status, receivedStatus.Trailers)); + if (status.StatusCode == StatusCode.Cancelled) + { + // Make sure the exception set to the Task is observed, + // otherwise this can trigger "Unobserved exception" when the response stream + // is not read until its end and the task created by the TCS is garbage collected. + // See https://github.com/grpc/grpc/issues/17458 + var _ = streamingResponseCallFinishedTcs.Task.Exception; + } return; }