I've been looking into using Tokio for a toy project and I'm trying to understand how to handle unintended panics in a Tokio task gracefully.
I understand that my code should never panic if it could instead use normal error handling but I would still like to handle panics gracefully. They could accidentally come from my code or maybe some crate panics in certain cases that I didn't properly account for.
As far as I understand, Tokio will by default let the thread crash, print the error to stderr and carry on. I noticed some work was being done to offer more control (tokio-rs/tokio#495, tokio-rs/tokio#700).
Ideally, I want to be able to see which tasks failed by panicking, log this somewhere, possibly restart the task that failed or maybe start another task that has the same purpose as the task that just failed. Is the right way to do this to use
catch_unwind on the
Future? Is there some solution involving channels that's the right way to do this? Am I trying to do too much and would end up fighting with the Tokio runtime - should I use another solution/crate? Any suggestions, advice or thoughts would be helpful, thanks!