Monday, September 4, 2023

Restarting macOS Apps Automatically on Crash

Alin Panaitiu (Hacker News):

Normally apps crash because of bugs introduced by the developer, but in my experience, there are cases where crashes can be out of the developer’s control. […] After we reach a certain confidence in the app stability, we can explore some ways to have the app restart automatically when the inevitable crash happens.


So with a few lines of XML, we can have launchd start our app and keep it alive on crashes.


[A signal handler] is the method I use in Lunar and Clop and it is reliable enough while also being easy to use and compatible with old macOS versions.


If the app is mostly a menubar utility sitting in background, it could be useful to detect and recover from app hangs. […] We can periodically schedule a ping on the main thread, and if the ping is not received for n seconds, an app hang happened.


Update (2023-09-06): Quinn:

If you’re in a signal handler there’s a very limited list of stuff you can safely call. This list doesn’t include such useful things as malloc, or the Swift or Objective-C runtimes.


Think very carefully before installing a signal handler for any purpose. And if your goal is to catch crashes, read this first.

I personally do not see much risk in Panaitiu’s signal handler that only uses Objective-C to get the main bundle’s path and then start a helper task to relaunch it. That said, it could perhaps stash the path at launch and use a C function instead of Process to respawn.

1 Comment RSS · Twitter · Mastodon

Pierre Lebeaupin

In terms of trying to recover from signals, I'd be remiss if I didn't also mention Fish's epic regression investigation as a cautionary tale:

Leave a Comment