Skip to content

Commit 2923260

Browse files
authored
Populate DebugException on some cancellation scenarios (#1780)
1 parent de472b1 commit 2923260

File tree

4 files changed

+9
-7
lines changed

4 files changed

+9
-7
lines changed

src/Grpc.Net.Client/Internal/GrpcCall.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,9 @@ public void EnsureNotDisposed()
248248
}
249249
}
250250

251-
public Exception CreateCanceledStatusException()
251+
public Exception CreateCanceledStatusException(Exception? ex = null)
252252
{
253-
var status = (CallTask.IsCompletedSuccessfully()) ? CallTask.Result : new Status(StatusCode.Cancelled, string.Empty);
253+
var status = (CallTask.IsCompletedSuccessfully()) ? CallTask.Result : new Status(StatusCode.Cancelled, string.Empty, ex);
254254
return CreateRpcException(status);
255255
}
256256

@@ -672,7 +672,7 @@ internal bool ResolveException(string summary, Exception ex, [NotNull] out Statu
672672
{
673673
if (ex is OperationCanceledException)
674674
{
675-
status = (CallTask.IsCompletedSuccessfully()) ? CallTask.Result : new Status(StatusCode.Cancelled, string.Empty);
675+
status = (CallTask.IsCompletedSuccessfully()) ? CallTask.Result : new Status(StatusCode.Cancelled, string.Empty, ex);
676676
if (!Channel.ThrowOperationCanceledOnCancellation)
677677
{
678678
resolvedException = CreateRpcException(status.Value);

src/Grpc.Net.Client/Internal/HttpContentClientStreamReader.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ public Task<bool> MoveNext(CancellationToken cancellationToken)
6565
{
6666
if (!_call.Channel.ThrowOperationCanceledOnCancellation)
6767
{
68-
return Task.FromException<bool>(_call.CreateCanceledStatusException());
68+
var ex = _call.CreateCanceledStatusException(new OperationCanceledException(_call.CancellationToken));
69+
return Task.FromException<bool>(ex);
6970
}
7071
else
7172
{
@@ -190,7 +191,7 @@ private async Task<bool> MoveNextCore(CancellationToken cancellationToken)
190191

191192
if (!_call.Channel.ThrowOperationCanceledOnCancellation)
192193
{
193-
throw _call.CreateCanceledStatusException();
194+
throw _call.CreateCanceledStatusException(ex);
194195
}
195196
else
196197
{

src/Grpc.Net.Client/Internal/HttpContentClientStreamWriter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,9 @@ public async Task WriteAsyncCore<TState>(Func<GrpcCall<TRequest, TResponse>, Str
176176

177177
GrpcEventSource.Log.MessageSent();
178178
}
179-
catch (OperationCanceledException) when (!_call.Channel.ThrowOperationCanceledOnCancellation)
179+
catch (OperationCanceledException ex) when (!_call.Channel.ThrowOperationCanceledOnCancellation)
180180
{
181-
throw _call.CreateCanceledStatusException();
181+
throw _call.CreateCanceledStatusException(ex);
182182
}
183183
}
184184
}

test/FunctionalTests/Client/StreamingTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,7 @@ async Task<DataComplete> ClientStreamedData(IAsyncStreamReader<DataMessage> requ
424424
// Assert
425425
Assert.AreEqual(StatusCode.Cancelled, ex.StatusCode);
426426
Assert.AreEqual(StatusCode.Cancelled, call.GetStatus().StatusCode);
427+
Assert.IsInstanceOf<OperationCanceledException>(call.GetStatus().DebugException);
427428

428429
AssertHasLog(LogLevel.Information, "GrpcStatusError", "Call failed with gRPC error status. Status code: 'Cancelled', Message: ''.");
429430

0 commit comments

Comments
 (0)