question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Action methods returning Task and exception handling

See original GitHub issue

Hi!

First of all, big thanks for this amazing library. I have just started using it and I really enjoy working with it, it’s great!

One thing I love about Stylet is the Command="{stylet:Action Execute}" command binding. I also found it surprising (in a good way) that it handles async methods that return Task.

However, there is a little caveat, that is if the Task is not awaited or disposed, any exceptions it generates will be uncaught.

This leads to problems:

  1. Exceptions are not caught so methods may silently fail to execute.
  2. The whole application will crash after an arbitrary time when GC finalizes the task, causing an unhandled exception.

I haven’t had the chance to look at the code yet, but I suppose it’s impossible to await tasks in the code that handles execution of actions in Stylet.

One alternative solution would be trailing tasks with .ContinueWith() that will dispose the task on the Dispatcher thread, thus forcing the exception to be shown to the user and instantly close the application.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
Tyrrrzcommented, Nov 28, 2018

Also wanted to add - great library, I’m using it for a few weeks now and I’m thoroughly impressed!

1reaction
Tyrrrzcommented, Oct 28, 2018

Ok good points!

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Where to handle an exception in a task of async action?
To properly propagate exceptions, the task returned from Task. ... Then You need to handle the exception inside the Action delegate:
Read more >
Async return types (C#)
If a method that returns a Task or Task<TResult> throws an exception, the exception is stored in the returned task. The exception is...
Read more >
Why exceptions in async methods are “dangerous” in C# | ...
No exception was thrown because the MyAsyncMethod routine is not awaited and the exception is literally lost with the Task. In that case,...
Read more >
Exception handling in async methods in .NET4.5 MVC4 with ...
In this post we'll take a look at how to handle exceptions that are thrown by actions that are awaited. My previous post...
Read more >
Asynchronous Programming with Async and Await in ASP. ...
When our method returns Task<TResult>, as in our previous example, it will return a result of type TResult in an asynchronous manner.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found