From 61e8cb5d50044cd14cacbf51d60789f5d1ce792c Mon Sep 17 00:00:00 2001 From: doomchild Date: Fri, 22 May 2026 14:51:49 -0500 Subject: [PATCH] Issue-130: Remove Task.Result access in HandleCancellation closes #130 --- src/TaskExtensions.cs | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/src/TaskExtensions.cs b/src/TaskExtensions.cs index dd19601..e31bf2f 100644 --- a/src/TaskExtensions.cs +++ b/src/TaskExtensions.cs @@ -13,20 +13,11 @@ public static partial class TaskExtensions private static Exception PotentiallyUnwindException(Exception exception) => exception is AggregateException aggregateException ? aggregateException.InnerException ?? exception : exception; - - private static Exception HandleCancellation(this Task task) - { - try - { - T result = task.Result; - - return new Exception("Expected canceled task"); - } - catch (Exception exception) - { - return PotentiallyUnwindException(exception); - } - } + + private static Exception HandleCancellation(Task task) => + task.IsCanceled + ? new TaskCanceledException() + : PotentiallyUnwindException(task.Exception ?? new Exception("Expected canceled task")); /// /// Monadic 'alt'.