-
Notifications
You must be signed in to change notification settings - Fork 826
Pass cancellation tokens and tasks to OperationCanceledException and TaskCanceledException #2770
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
Merged
KevinRansom
merged 3 commits into
dotnet:master
from
saul:taskcanceledexception-improvements
Apr 3, 2017
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -767,7 +767,7 @@ namespace Microsoft.FSharp.Control | |
|
||
// Call the cancellation continuation | ||
let cancelT (args:AsyncParams<_>) = | ||
args.aux.ccont (new OperationCanceledException()) | ||
args.aux.ccont (new OperationCanceledException(args.aux.token)) | ||
|
||
// Build a primitive without any exception of resync protection | ||
// | ||
|
@@ -1541,8 +1541,8 @@ namespace Microsoft.FSharp.Control | |
let continuation (completedTask : Task<_>) : unit = | ||
args.aux.trampolineHolder.Protect((fun () -> | ||
if completedTask.IsCanceled then | ||
if useCcontForTaskCancellation then args.aux.ccont(new OperationCanceledException()) | ||
else args.aux.econt (ExceptionDispatchInfo.Capture(new TaskCanceledException())) | ||
if useCcontForTaskCancellation then args.aux.ccont(new OperationCanceledException(args.aux.token)) | ||
else args.aux.econt (ExceptionDispatchInfo.Capture(new TaskCanceledException(completedTask))) | ||
elif completedTask.IsFaulted then | ||
args.aux.econt (MayLoseStackTrace(completedTask.Exception)) | ||
else | ||
|
@@ -1555,8 +1555,8 @@ namespace Microsoft.FSharp.Control | |
let continuation (completedTask : Task) : unit = | ||
args.aux.trampolineHolder.Protect((fun () -> | ||
if completedTask.IsCanceled then | ||
if useCcontForTaskCancellation then args.aux.ccont (new OperationCanceledException()) | ||
else args.aux.econt (ExceptionDispatchInfo.Capture(new TaskCanceledException())) | ||
if useCcontForTaskCancellation then args.aux.ccont (new OperationCanceledException(args.aux.token)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
else args.aux.econt (ExceptionDispatchInfo.Capture(new TaskCanceledException(completedTask))) | ||
elif completedTask.IsFaulted then | ||
args.aux.econt (MayLoseStackTrace(completedTask.Exception)) | ||
else | ||
|
@@ -1630,7 +1630,7 @@ namespace Microsoft.FSharp.Control | |
match !timer with | ||
| None -> () | ||
| Some t -> t.Dispose() | ||
aux.trampolineHolder.Protect(fun () -> savedCCont(new OperationCanceledException())) |> unfake | ||
aux.trampolineHolder.Protect(fun () -> savedCCont(new OperationCanceledException(aux.token))) |> unfake | ||
), | ||
null) | ||
let mutable edi = null | ||
|
@@ -1695,7 +1695,7 @@ namespace Microsoft.FSharp.Control | |
Async.Start (async { do (ccont e |> unfake) }) | ||
|
||
// register cancellation handler | ||
let registration = aux.token.Register(fun () -> cancel (OperationCanceledException())) | ||
let registration = aux.token.Register(fun () -> cancel (OperationCanceledException(aux.token))) | ||
|
||
// run actual await routine | ||
// 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 | |
match !rwh with | ||
| None -> () | ||
| Some rwh -> rwh.Unregister(null) |> ignore) | ||
Async.Start (async { do (aux.ccont (OperationCanceledException()) |> unfake) })) | ||
Async.Start (async { do (aux.ccont (OperationCanceledException(aux.token)) |> unfake) })) | ||
|
||
and registration : CancellationTokenRegistration = aux.token.Register(cancelHandler, null) | ||
|
||
|
@@ -1842,7 +1842,7 @@ namespace Microsoft.FSharp.Control | |
// Register the result. This may race with a successful result, but | ||
// ResultCell allows a race and throws away whichever comes last. | ||
once.Do(fun () -> | ||
let canceledResult = Canceled (OperationCanceledException()) | ||
let canceledResult = Canceled (OperationCanceledException(cancellationToken)) | ||
resultCell.RegisterResult(canceledResult,reuseThread=true) |> unfake | ||
) | ||
| Some cancel -> | ||
|
@@ -2007,7 +2007,7 @@ namespace Microsoft.FSharp.Control | |
event.RemoveHandler(del) | ||
// Register the result. This may race with a successful result, but | ||
// ResultCell allows a race and throws away whichever comes last. | ||
once.Do(fun () -> resultCell.RegisterResult(Canceled (OperationCanceledException()),reuseThread=true) |> unfake) | ||
once.Do(fun () -> resultCell.RegisterResult(Canceled (OperationCanceledException(token)),reuseThread=true) |> unfake) | ||
| Some cancel -> | ||
// If we get an exception from a cooperative cancellation function | ||
// we assume the operation has already completed. | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@saul @dsyme this is incorrect. You are passing the async cancellation token which may not be related to the cancellation of the task we're binding on.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@saul @matthid this line should be reverted to the original (unless there is a generic way to obtain a cancellation token from arbitrary tasks which I'm not aware of)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok added in #3255
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@eiriktsarpalis @matthid What is this were
if args.aux.token.IsCancellationRequested then new OperationCanceledException(args.aux.token) else new OperationCanceledException()
?I suspect a lot of corresponding C# code would do the same thing: if a cancellation continuation needs to be converted into a OCE, then you would attach the current cancellation token in scope to the OCE, at least on the condition that that the current cancellation token in scope is in the cancelled state,