Preserving stacktraces in Asynchronous workflows

Oct 31, 2014 at 2:04 AM
One of the bigger annoyances when using async is the fact that exceptions are returned to the user with erased stacktraces. This is due to nature of async and the fact that the CLR resets the stacktrace of thrown exceptions. Luckily, .NET 4.5 comes with ExceptionDispatchInfo, a type that permits re-raising of exceptions inside different contexts in an approved manner.

Having this capability baked into async would greatly improve its debugging experience. I have prepared a fork with some preliminary work done that you can find here. Here is a screenshot that demonstrates an example.

Since it only works in .NET 4.5 runtimes, this should be wrapped inside compilation conditionals as well as and possibly include runtime checks. Your feedback on the preferred approach would be appreciated.

Some additional thoughts:
  1. There is a way to achieve emulate ExceptionDispatchInfo in older runtimes, although it involves reflection. Here is an example.
  2. An Async.Raise combinator would be useful as an alternative to return raise syntax. This would simply pass its argument to the exception continuation, thus keeping its original stacktrace if so desired.
  3. It would be possible imprint a 'monadic stacktrace' onto exceptions. This can be achieved by making the exception continuation append a symbolic stack entry on each bind operation. This would probably require reflection.
Oct 31, 2014 at 2:22 PM
Hi Eirik,

Did you see this PR?

Could you review it? I think it is ready to go in. It would be great if you could try it out.

Oct 31, 2014 at 4:39 PM
Thanks, looking at it now.