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 is a
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:
- There is a way to achieve emulate ExceptionDispatchInfo in older runtimes, although it involves reflection. Here is 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.
- 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.