Capture OperationCanceledException (instead of TaskCanceledException)

The former is the base class for the latter, so this will widen the umbrella to catch a few corner cases
that would otherwise been handled by the generic "Exception" catch block. One corner case I know
of is when an op is canceled via "CancellationToken.ThrowIfCancellationRequested". In that case,
an OperationCanceledException is thrown (not a TaskCanceledException).

https://stackoverflow.com/questions/34359129/operationcanceledexception-vs-taskcanceledexception-when-task-is-canceled
This commit is contained in:
John Colvin committed 2024-11-25 15:26:58 +00:00
1 parent 41390da06a
commit 94591eee6d
1 file changed
+1 -1
@@ -39,7 +39,7 @@ public class JsonRpcRequestAsyncEnumerable {
var offsetBuffer = new ArraySegment<byte>(buffer, offset, buffer.Length - offset);
try {
return await webSocket.ReceiveAsync(offsetBuffer, cancellationToken);
} catch (TaskCanceledException) {
} catch (OperationCanceledException) {
logger.LogTrace("JsonRpcRequestAsyncEnumerable 'ReceiveAsync' task was cancelled (exiting gracefully).");
} catch (Exception ex) {
logger.LogError("JsonRpcRequestAsyncEnumerable Exception while awaiting request: {Exception}", ex);