Is it advisable to use `catch_unwind` or threads to catch all panics? (In my situation)

Hi,

So I'm working on a small server for local web development (live-reloading, etc.), and I'd like to prevent, that the server crashes.

Currently, I have a "supervisor" that executes the components (HTTP server, WebSocket server, etc), and restarts them when they fail.

This supervisor made it a bit more difficult to implement my web dev server (for example, because I have mutable data that now has to be behind a Mutex). Also, as far as I see, my (pretty naive) implementation of the supervisor could lead to problems (currently I just re-start the component, for example. And I'm using the Mutex type from parking_lot, but I'm not 100% sure what could happen if a component with mutable data crashes while altering the data).

Everything is working pretty well. However, I'm not 100% sure anymore if a supervisor makes much sense in this context.

If I remember correctly, there was only one instance when a component panic!ked so far (and that was during the implementation, not even when everything was working).

So I'm wondering: If I do proper error handling (no unwrap, etc.) – does it even make sense here to implement a supervisor?

I'd hate it, if, let's say, the HTTP server panics, which would result in nothing working anymore.

Maybe the right way would be, to still have a "supervisor", but instead of blindly restarting a failed component, to just stop all other components, to fail gracefully.

What do you think?

As an alternative, what about letting a panicking thread crash your process and use something like an infinite loop in bash to make sure the server is restarted?

It's primitive, but writing 3 lines of bash is a lot less effort than creating a supervisor system.

1 Like

Thanks, for the tip, @Michael-F-Bryan!

I think this would be a great option if I'd only want to use the web dev server myself.

Because I'd like to publish this tool later, I'd like to avoid this for the end-user (for some, even this simple shell scripting is probably too much).

But this led me to a good solution, I believe.

Basically, a combination of your approach and what I wrote at the end of my message: On errors, I'll gracefully shutdown all components, and main will have a loop that restarts the server again (with a "maximum restarts in N seconds" check).

This way the components can own their data (instead of a component start closure), which would make things much easier.

Thanks, again!

1 Like