-
Notifications
You must be signed in to change notification settings - Fork 781
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
Don't hide exceptions on the async cancellation-path #3257
Changes from all commits
7075338
e742506
b8a325e
304e5dc
a7d97e3
d5881c9
ac0a9c1
49c26bf
bd08e6e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -126,7 +126,12 @@ type AsyncType() = | |
) | ||
|
||
member private this.WaitASec (t:Task) = | ||
let result = t.Wait(TimeSpan(hours=0,minutes=0,seconds=1)) | ||
let result = | ||
try t.Wait(TimeSpan(hours=0,minutes=0,seconds=1)) | ||
with :? AggregateException -> | ||
// This throws the "original" exception | ||
t.GetAwaiter().GetResult() | ||
false | ||
Assert.IsTrue(result, "Task did not finish after waiting for a second.") | ||
|
||
|
||
|
@@ -162,17 +167,118 @@ type AsyncType() = | |
try | ||
let result = t.Wait(300) | ||
Assert.IsFalse (result) | ||
with :? AggregateException -> Assert.Fail "Task should not finish, jet" | ||
with :? AggregateException -> Assert.Fail "Task should not finish, yet" | ||
|
||
tcs.SetCanceled() | ||
|
||
try | ||
this.WaitASec t | ||
with :? AggregateException as a -> | ||
match a.InnerException with | ||
| :? TaskCanceledException as t -> () | ||
with :? TaskCanceledException -> () | ||
Assert.IsTrue (t.IsCompleted, "Task is not completed") | ||
Assert.IsTrue (t.IsCanceled, "Task is not cancelled") | ||
|
||
[<Test>] | ||
member this.StartAsTaskCancellationViaException () = | ||
let cts = new CancellationTokenSource() | ||
let tcs = TaskCompletionSource<unit>() | ||
let a = async { | ||
cts.CancelAfter (100) | ||
do! tcs.Task |> Async.AwaitTask } | ||
#if FSCORE_PORTABLE_NEW || coreclr | ||
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. Does this still exist? 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. cc @KevinRansom 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. I'm taking a look through these various old flags now, will send a PR to flush this |
||
let t : Task<unit> = | ||
#else | ||
use t : Task<unit> = | ||
#endif | ||
Async.StartAsTask(a, cancellationToken = cts.Token) | ||
|
||
// Should not finish | ||
try | ||
let result = t.Wait(300) | ||
Assert.IsFalse (result) | ||
with :? AggregateException -> Assert.Fail "Task should not finish, yet" | ||
|
||
let msg = "Custom non-conforming 3rd-Party-Api throws" | ||
tcs.SetException(Exception msg) | ||
|
||
try | ||
this.WaitASec t | ||
with :? TaskCanceledException as t -> | ||
match t.InnerException with | ||
| :? AggregateException as a -> | ||
Assert.AreEqual(1, a.InnerExceptions.Count) | ||
Assert.AreEqual(msg, a.InnerException.Message) | ||
| _ -> reraise() | ||
Assert.IsTrue (t.IsCompleted, "Task is not completed") | ||
Assert.IsTrue (t.IsCanceled, "Task is not cancelled") | ||
|
||
[<Test>] | ||
member this.RunSynchronouslyCancellation () = | ||
let cts = new CancellationTokenSource() | ||
let tcs = TaskCompletionSource<unit>() | ||
let a = async { | ||
cts.CancelAfter (100) | ||
do! tcs.Task |> Async.AwaitTask } | ||
#if FSCORE_PORTABLE_NEW || coreclr | ||
let t : Task<unit> = | ||
#else | ||
use t : Task<unit> = | ||
#endif | ||
Task.Run(new Func<unit>(fun () -> Async.RunSynchronously(a, cancellationToken = cts.Token))) | ||
|
||
// Should not finish | ||
try | ||
let result = t.Wait(300) | ||
Assert.IsFalse (result) | ||
with :? AggregateException -> Assert.Fail "Task should not finish, yet" | ||
|
||
tcs.SetCanceled() | ||
|
||
try | ||
this.WaitASec t | ||
with :? OperationCanceledException -> () | ||
|
||
Assert.IsTrue (t.IsCompleted, "Task is not completed") | ||
// We used Task.Run for convenience, it will not notice the cancellation | ||
// -> Cancellation is noticed by RunSynchronously throwing 'OperationCanceledException' | ||
// which is tested above | ||
//Assert.IsTrue (t.IsCanceled, "Task is not cancelled") | ||
|
||
[<Test>] | ||
member this.RunSynchronouslyCancellationViaException () = | ||
let cts = new CancellationTokenSource() | ||
let tcs = TaskCompletionSource<unit>() | ||
let a = async { | ||
cts.CancelAfter (100) | ||
do! tcs.Task |> Async.AwaitTask } | ||
#if FSCORE_PORTABLE_NEW || coreclr | ||
let t : Task<unit> = | ||
#else | ||
use t : Task<unit> = | ||
#endif | ||
Task.Run(new Func<unit>(fun () -> Async.RunSynchronously(a, cancellationToken = cts.Token))) | ||
|
||
// Should not finish | ||
try | ||
let result = t.Wait(300) | ||
Assert.IsFalse (result) | ||
with :? AggregateException -> Assert.Fail "Task should not finish, yet" | ||
|
||
let msg = "Custom non-conforming 3rd-Party-Api throws" | ||
tcs.SetException(Exception msg) | ||
|
||
try | ||
this.WaitASec t | ||
with :? OperationCanceledException as t -> | ||
match t.InnerException with | ||
| :? AggregateException as a -> | ||
Assert.AreEqual(1, a.InnerExceptions.Count) | ||
Assert.AreEqual(msg, a.InnerException.Message) | ||
| _ -> reraise() | ||
Assert.IsTrue (t.IsCompleted, "Task is not completed") | ||
// We used Task.Run for convenience, it will not notice the cancellation | ||
// -> Cancellation is noticed by RunSynchronously throwing 'OperationCanceledException' | ||
// which is tested above | ||
//Assert.IsTrue (t.IsCanceled, "Task is not cancelled") | ||
|
||
[<Test>] | ||
member this.StartTask () = | ||
|
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.
Is this an artifact?
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.
I mean: Does this define still exist?
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.
This one doesn't exist any more