Search code examples
asynchronousredisbooksleeve

Cache async error from BookSleeve Redis client


BookSleeve is very good in performance because it use async IO as much as could.

But the problem is, it may throw fatal exception from asynchronous operation and crash my application.

I know that keyword await can help, but I can't use await for every call.

Is there a way to catch the unhandled exception globally?


Solution

  • What version of the library are you using? BookSleeve internally observes its own exceptions specifically so that they should not cause any crashes, however, you can also:

    • handle the .Error event
    • handle the TaskScheduler.UnobservedTaskException event

    strictly, neither of these should be required. It is the latter than would prevent app closures, but again: this should not matter - unless there is a bug in some of the code and it is missing one or more tasks

    But on a more general level, it is good practice to check what happens to your tasks. If you don't want to await them, it would probably still be worth subscribing a ContinueWith which checks for exceptions. Also, you don't necessarily need to await; in many cases, connection.Wait(result) is fine too - although this does tie up a primary thread for a little longer (but note: it doesn't tie up the multiplexer).