Prism icon indicating copy to clipboard operation
Prism copied to clipboard

[BUG] Task Extensions .Await completed callback can trigger error callback

Open Binderbound opened this issue 2 years ago • 3 comments

Description

When Await() is used with a callback and errorCallback, an exception in the callback can trigger an errorCallback. I think this is kind of counterintuitive - the errorCallback should be about the task you're awaiting on only, and you should either A) get some kind of guarantee that only the completed callback or the error callback is going to be triggered or B) completed callback will always be run, and error will only be run if there's an error in the Task

Steps to Reproduce

Console.WriteLine("Hello, World!");
Task.Delay(1000).Await(()=> {
    Console.WriteLine("completed");
    throw new Exception("Unrelated exception");
}, ex=>Console.WriteLine(ex.Message), true);
Console.ReadKey();

Platform with bug

Prism Core

Affected platforms

Windows

Did you find any workaround?

No workaround, but it should be easily resolved by moving completedCallback?Invoke() line outsideof the try/catch and adding a return after errorCallback invoke OR moving the completed callback to a finally block, depending on what behaviour is desired.

Relevant log output

Hello, World!
completed
Unrelated exception

Binderbound avatar Feb 01 '23 06:02 Binderbound

In case of an exception the completed callback should not fire at all, only the error call back.

Would you like to submit a PR?

brianlagunas avatar Feb 01 '23 19:02 brianlagunas

Can do I think this could be done as a shorthand wrapper around Task.ContinueWith as well. What approach would you prefer?

Binderbound avatar Feb 01 '23 23:02 Binderbound

Please avoid using ContinueWith.

brianlagunas avatar Feb 02 '23 02:02 brianlagunas

Closing this as there we no longer any interest in moving this forward.

brianlagunas avatar Jul 04 '24 03:07 brianlagunas