{"id":40545,"date":"2023-09-04T15:24:38","date_gmt":"2023-09-04T19:24:38","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=40545"},"modified":"2023-09-06T11:48:51","modified_gmt":"2023-09-06T15:48:51","slug":"restarting-macos-apps-automatically-on-crash","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2023\/09\/04\/restarting-macos-apps-automatically-on-crash\/","title":{"rendered":"Restarting macOS Apps Automatically on Crash"},"content":{"rendered":"<p><a href=\"https:\/\/notes.alinpanaitiu.com\/Restarting-macOS-apps-automatically-on-crash\">Alin Panaitiu<\/a> (<a href=\"https:\/\/news.ycombinator.com\/item?id=37355429\">Hacker News<\/a>):<\/p>\n<blockquote cite=\"https:\/\/notes.alinpanaitiu.com\/Restarting-macOS-apps-automatically-on-crash\">\n<p>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&rsquo;s control. [&#8230;] 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.<\/p>\n<p>[&#8230;]<\/p>\n<p>So with a few lines of XML, we can have launchd start our app and keep it alive on crashes.<\/p>\n<p>[&#8230;]<\/p>\n<p>[A signal handler] is the method I use in <a href=\"https:\/\/lunar.fyi\">Lunar<\/a> and <a href=\"https:\/\/lowtechguys.com\/clop\/\">Clop<\/a> and it is reliable enough while also being easy to use and compatible with old macOS versions.<\/p>\n<p>[&#8230;]<\/p>\n<p>If the app is mostly a menubar utility sitting in background, it could be useful to detect and recover from app hangs. [&#8230;] We can periodically schedule a ping on the main thread, and if the ping is not received for <code>n<\/code> seconds, an app hang happened.<\/p>\n<\/blockquote>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2022\/10\/27\/venturas-open-at-login-vs-allow-in-the-background\/\">Ventura&rsquo;s &ldquo;Open at Login&rdquo; vs. &ldquo;Allow in the Background&rdquo;<\/a><\/li>\n<\/ul>\n\n<p id=\"restarting-macos-apps-automatically-on-crash-update-2023-09-06\">Update (2023-09-06): <a href=\"https:\/\/toot.community\/@justkwin\/111011117928393317\">Quinn<\/a>:<\/p>\n<blockquote cite=\"https:\/\/toot.community\/@justkwin\/111011117928393317\"><p>If you&rsquo;re in a signal handler there&rsquo;s a <em>very<\/em> limited list of stuff you can safely call. This list doesn&rsquo;t include such useful things as <code>malloc<\/code>, or the Swift or Objective-C runtimes.<\/p><p>[&#8230;]<\/p><p>Think very carefully before installing a signal handler for any purpose. And if your goal is to catch crashes, <a href=\"https:\/\/developer.apple.com\/forums\/thread\/113742\">read this first<\/a>.<\/p><\/blockquote>\n<p>I personally do not see much risk in Panaitiu&rsquo;s signal handler that only uses Objective-C to get the main bundle&rsquo;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 <code>Process<\/code> to respawn.<\/p>","protected":false},"excerpt":{"rendered":"<p>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&rsquo;s control. [&#8230;] After we reach a certain confidence in the app stability, we can explore some ways to have the app restart automatically when the inevitable [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2023-09-04T19:24:40Z","apple_news_api_id":"90404935-cd17-4d99-aa31-c64f6e6aad24","apple_news_api_modified_at":"2023-09-06T15:48:39Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAA==","apple_news_api_share_url":"https:\/\/apple.news\/AkEBJNc0XTZmqMcZPbmqtJA","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[131,2460,500,30,2223,71],"class_list":["post-40545","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-bug","tag-crash-handling","tag-launchd","tag-mac","tag-macos-13-ventura","tag-programming"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/40545","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=40545"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/40545\/revisions"}],"predecessor-version":[{"id":40562,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/40545\/revisions\/40562"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=40545"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=40545"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=40545"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}