Skip to content

Commit dd53a3d

Browse files
saulKevinRansom
authored andcommitted
Pass cancellation tokens and tasks to OperationCanceledException and TaskCanceledException (#2770)
* Initial attempt at passing in cancellation tokens/tasks to OperationCanceledException and TaskCanceledException Fixes #2100 * Updated remaining references to OperationCanceledException * Fix Reactor compilation
1 parent cb0b407 commit dd53a3d

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

src/absil/illib.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ module Cancellable =
762762
let token () = Cancellable (fun ct -> ValueOrCancelled.Value ct)
763763

764764
/// Represents a canceled computation
765-
let canceled() = Cancellable (fun _ -> ValueOrCancelled.Cancelled (new OperationCanceledException()))
765+
let canceled() = Cancellable (fun ct -> ValueOrCancelled.Cancelled (new OperationCanceledException(ct)))
766766

767767
/// Catch exceptions in a computation
768768
let private catch (Cancellable e) =

src/fsharp/FSharp.Core/control.fs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,7 @@ namespace Microsoft.FSharp.Control
767767

768768
// Call the cancellation continuation
769769
let cancelT (args:AsyncParams<_>) =
770-
args.aux.ccont (new OperationCanceledException())
770+
args.aux.ccont (new OperationCanceledException(args.aux.token))
771771

772772
// Build a primitive without any exception of resync protection
773773
//
@@ -1541,8 +1541,8 @@ namespace Microsoft.FSharp.Control
15411541
let continuation (completedTask : Task<_>) : unit =
15421542
args.aux.trampolineHolder.Protect((fun () ->
15431543
if completedTask.IsCanceled then
1544-
if useCcontForTaskCancellation then args.aux.ccont(new OperationCanceledException())
1545-
else args.aux.econt (ExceptionDispatchInfo.Capture(new TaskCanceledException()))
1544+
if useCcontForTaskCancellation then args.aux.ccont(new OperationCanceledException(args.aux.token))
1545+
else args.aux.econt (ExceptionDispatchInfo.Capture(new TaskCanceledException(completedTask)))
15461546
elif completedTask.IsFaulted then
15471547
args.aux.econt (MayLoseStackTrace(completedTask.Exception))
15481548
else
@@ -1555,8 +1555,8 @@ namespace Microsoft.FSharp.Control
15551555
let continuation (completedTask : Task) : unit =
15561556
args.aux.trampolineHolder.Protect((fun () ->
15571557
if completedTask.IsCanceled then
1558-
if useCcontForTaskCancellation then args.aux.ccont (new OperationCanceledException())
1559-
else args.aux.econt (ExceptionDispatchInfo.Capture(new TaskCanceledException()))
1558+
if useCcontForTaskCancellation then args.aux.ccont (new OperationCanceledException(args.aux.token))
1559+
else args.aux.econt (ExceptionDispatchInfo.Capture(new TaskCanceledException(completedTask)))
15601560
elif completedTask.IsFaulted then
15611561
args.aux.econt (MayLoseStackTrace(completedTask.Exception))
15621562
else
@@ -1630,7 +1630,7 @@ namespace Microsoft.FSharp.Control
16301630
match !timer with
16311631
| None -> ()
16321632
| Some t -> t.Dispose()
1633-
aux.trampolineHolder.Protect(fun () -> savedCCont(new OperationCanceledException())) |> unfake
1633+
aux.trampolineHolder.Protect(fun () -> savedCCont(new OperationCanceledException(aux.token))) |> unfake
16341634
),
16351635
null)
16361636
let mutable edi = null
@@ -1695,7 +1695,7 @@ namespace Microsoft.FSharp.Control
16951695
Async.Start (async { do (ccont e |> unfake) })
16961696

16971697
// register cancellation handler
1698-
let registration = aux.token.Register(fun () -> cancel (OperationCanceledException()))
1698+
let registration = aux.token.Register(fun () -> cancel (OperationCanceledException(aux.token)))
16991699

17001700
// run actual await routine
17011701
// callback will be executed on the thread pool so we need to use TrampolineHolder.Protect to install trampoline
@@ -1746,7 +1746,7 @@ namespace Microsoft.FSharp.Control
17461746
match !rwh with
17471747
| None -> ()
17481748
| Some rwh -> rwh.Unregister(null) |> ignore)
1749-
Async.Start (async { do (aux.ccont (OperationCanceledException()) |> unfake) }))
1749+
Async.Start (async { do (aux.ccont (OperationCanceledException(aux.token)) |> unfake) }))
17501750

17511751
and registration : CancellationTokenRegistration = aux.token.Register(cancelHandler, null)
17521752

@@ -1842,7 +1842,7 @@ namespace Microsoft.FSharp.Control
18421842
// Register the result. This may race with a successful result, but
18431843
// ResultCell allows a race and throws away whichever comes last.
18441844
once.Do(fun () ->
1845-
let canceledResult = Canceled (OperationCanceledException())
1845+
let canceledResult = Canceled (OperationCanceledException(cancellationToken))
18461846
resultCell.RegisterResult(canceledResult,reuseThread=true) |> unfake
18471847
)
18481848
| Some cancel ->
@@ -2007,7 +2007,7 @@ namespace Microsoft.FSharp.Control
20072007
event.RemoveHandler(del)
20082008
// Register the result. This may race with a successful result, but
20092009
// ResultCell allows a race and throws away whichever comes last.
2010-
once.Do(fun () -> resultCell.RegisterResult(Canceled (OperationCanceledException()),reuseThread=true) |> unfake)
2010+
once.Do(fun () -> resultCell.RegisterResult(Canceled (OperationCanceledException(token)),reuseThread=true) |> unfake)
20112011
| Some cancel ->
20122012
// If we get an exception from a cooperative cancellation function
20132013
// we assume the operation has already completed.

src/fsharp/vs/Reactor.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ type Reactor() =
152152
with e -> e |> AsyncUtil.AsyncException
153153

154154
resultCell.RegisterResult(result)),
155-
ccont=(fun () -> resultCell.RegisterResult (AsyncUtil.AsyncCanceled(OperationCanceledException())) )
155+
ccont=(fun () -> resultCell.RegisterResult (AsyncUtil.AsyncCanceled(OperationCanceledException(ct))) )
156156

157157
)
158158
return! resultCell.AsyncResult

0 commit comments

Comments
 (0)