Skip to content

Cancellable: always catch internal cancellations #18531

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/Compiler/Utilities/Cancellable.fs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ module Cancellable =
if ct.IsCancellationRequested then
ValueOrCancelled.Cancelled(OperationCanceledException ct)
else
oper ct
try
oper ct
with
| :? OperationCanceledException as e when ct.IsCancellationRequested -> ValueOrCancelled.Cancelled e
| :? OperationCanceledException as e -> InvalidOperationException("Wrong cancellation token", e) |> raise

let fold f acc seq =
Cancellable(fun ct ->
Expand Down Expand Up @@ -101,14 +105,10 @@ module Cancellable =
let! ct = Async.CancellationToken

return!
Async.FromContinuations(fun (cont, econt, ccont) ->
try
match run ct c with
| ValueOrCancelled.Value v -> cont v
| ValueOrCancelled.Cancelled ce -> ccont ce
with
| :? OperationCanceledException as ce when ct.IsCancellationRequested -> ccont ce
| :? OperationCanceledException as e -> InvalidOperationException("Wrong cancellation token", e) |> econt)
Async.FromContinuations(fun (cont, _econt, ccont) ->
match run ct c with
| ValueOrCancelled.Value v -> cont v
| ValueOrCancelled.Cancelled ce -> ccont ce)
}

let token () = Cancellable(ValueOrCancelled.Value)
Expand Down
Loading