{"id":9848,"date":"2020-11-16T06:46:15","date_gmt":"2020-11-16T14:46:15","guid":{"rendered":"https:\/\/www.coretechnologies.com\/blog\/?p=9848"},"modified":"2020-11-16T06:46:15","modified_gmt":"2020-11-16T14:46:15","slug":"avoid-shutdown-restarts","status":"publish","type":"post","link":"https:\/\/www.coretechnologies.com\/blog\/alwaysup\/avoid-shutdown-restarts\/","title":{"rendered":"Q&#038;A: Why does AlwaysUp Restart my Application During Windows Shutdown?"},"content":{"rendered":"<div align=\"center\"><img decoding=\"async\" src=\"\/blog\/images\/qa-shutdown-race-condition.png\" title=\"Q&#038;A: Why does AlwaysUp Restart my Application During Windows Shutdown?\" alt=\"Q&#038;A: Why does AlwaysUp Restart my Application During Windows Shutdown?\" border=\"0\" \/><\/div>\n<div class=\"blog-qa-question-box\"><img decoding=\"async\" src=\"\/images\/quotes-transparent-21x21.png\" \/> This seems like strange behavior, but when you are using autologon to launch an application in session 1 and  reboot the server, during the reboot AlwaysUp briefly relaunches the application in session 0. Here&#8217;s an example from the activity log:<\/p>\n<div align=\"center\"><a href=\"\/blog\/images\/alwaysup-system-shutdown-log.png\" class=\"zoomPopup\" title=\"AlwaysUp System Shutdown Log\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"image-padding\" src=\"\/blog\/images\/alwaysup-system-shutdown-log.png\" title=\"AlwaysUp System Shutdown Log (click to enlarge)\" alt=\"AlwaysUp System Shutdown Log\" border=\"0\" width=\"520\" \/><\/a><\/div>\n<p>Why does that happen? How do I prevent it from happening?<\/p>\n<p align=\"right\">&mdash; Kevan<\/p>\n<\/div>\n<p>Hi Kevan. We have seen this before&#8230;<\/p>\n<h2 class=\"blog-caption\">You&#8217;re encountering a Race Condition as Windows shuts down<\/h2>\n<p>Unfortunately you are experiencing the effects of the chaotic and poorly documented <a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/win32\/shutdown\/about-system-shutdown\" target=\"_blank\" rel=\"noopener noreferrer\">Windows shutdown process<\/a>.<\/p>\n<p>From the timeline you have provided, you initiated the reboot at about 10:26 AM, right? Within a few seconds of that, Windows logs off the user signed in to Session 1.<\/p>\n<p>That system-initiated logoff terminates Session 1. Windows stops all programs running in Session 1 &mdash; forcibly if necessary. And since your application is running in Session 1, it&#8217;s terminated too. That happens at about 10:26:08.<\/p>\n<p>AlwaysUp, which is constantly monitoring your application in the background, immediately notices that the Session is closed and that your application has stopped. AlwaysUp writes an informative log entry at 10:26:08:<\/p>\n<div align=\"center\"><a href=\"\/blog\/images\/session-closed.png\" class=\"zoomPopup\" title=\"Session 1 closed\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"image-padding\" src=\"\/blog\/images\/session-closed.png\" title=\"Session 1 closed (click to enlarge)\" alt=\"Session 1 closed\" border=\"0\" width=\"520\" \/><\/a><\/div>\n<p>Next, AlwaysUp springs into action to ensure that your application runs 24\/7. It knows your preference is to run in Session 1, but that&#8217;s no longer possible. The only reliable place to restart your application is in the session that is always available to Windows Services &mdash; the <a href=\"\/WindowsServices\/FAQ.html#WhatIsSession0Isolation\">isolated Session 0<\/a>.<\/p>\n<p>So AlwaysUp starts your application in Session 0. The log line at 10:26:09 confirms that action:<\/p>\n<div align=\"center\"><a href=\"\/blog\/images\/application-started-in-session-0.png\" class=\"zoomPopup\" title=\"Application started in Session 0\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"image-padding\" src=\"\/blog\/images\/application-started-in-session-0.png\" title=\"Application started in Session 0 (click to enlarge)\" alt=\"Application started in Session 0\" border=\"0\" width=\"520\" \/><\/a><\/div>\n<p>However, during the process of rapidly restarting your program in Session 0, AlwaysUp is notified of another important event. Windows is shutting down. This is reported at 10:26:09 too:<\/p>\n<div align=\"center\"><a href=\"\/blog\/images\/windows-shutdown-initiated.png\" class=\"zoomPopup\" title=\"Windows shutdown initiated\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"image-padding\" src=\"\/blog\/images\/windows-shutdown-initiated.png\" title=\"Windows shutdown initiated (click to enlarge)\" alt=\"Windows shutdown initiated\" border=\"0\" width=\"520\" \/><\/a><\/div>\n<p>And finally, a mere two seconds later, both the application and service stop.<\/p>\n<p>So the only hiccup &mdash; as you mentioned &mdash; is the unnecessary, &#8220;phantom&#8221; restart that happens at 10:26:08. <b>This is because AlwaysUp is not aware of the system shutdown at the time when it detects the application outage.<\/b> AlwaysUp is just doing it&#8217;s job, trying to make sure that your application is always running, 24&#215;7.<\/p>\n<p>This is a classic <a href=\"https:\/\/en.wikipedia.org\/wiki\/Race_condition\" target=\"_blank\" rel=\"noopener noreferrer\">race condition<\/a>, where two events (&#8220;session closed&#8221; and &#8220;system shutting down&#8221; notifications) occur in an unpredictable order. When AlwaysUp is notified of the shutdown first, it won&#8217;t restart your application. However, when AlwaysUp learns of the session close first, it will restart your application. And unfortunately you never know which sequence\/behavior you&#8217;re going to get. \ud83d\ude42<\/p>\n<h2 class=\"blog-caption\">How to avoid application restarts during Windows Shutdown<\/h2>\n<p>To overcome the problem, configure AlwaysUp to wait for a few seconds before restarting your application. Make that change on the <b>Restart<\/b> tab.<\/p>\n<p>For example, here we have instructed AlwaysUp to delay for 15 seconds:<\/p>\n<div align=\"center\"><a href=\"\/blog\/images\/alwaysup-delay-restart.png\" class=\"zoomPopup\" title=\"AlwaysUp: Delay restart\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"image-padding\" src=\"\/blog\/images\/alwaysup-delay-restart.png\" title=\"AlwaysUp: Delay restart (click to enlarge)\" alt=\"AlwaysUp: Delay restart\" border=\"0\" \/><\/a><\/div>\n<p>That short pause should provide enough time for AlwaysUp to receive the Windows shutdown notification and exit gracefully &mdash; without a phantom restart.<\/p>\n<p>Of course, this adjustment means that <b>AlwaysUp will not restart your program immediately if it crashes<\/b>. If that&#8217;s a problem (i.e. your application must achieve close to 100% uptime), you may want to reduce the delay. Even 5 seconds should be sufficient to sidestep the dreaded race condition.<\/p>\n<!-- relpost-thumb-wrapper --><div class=\"relpost-thumb-wrapper\"><!-- filter-class --><div class=\"relpost-thumb-container\"><style>.relpost-block-single-image, .relpost-post-image { margin-bottom: 10px; }<\/style><h3>You may also like...<\/h3><div style=\"clear: both\"><\/div><div style=\"clear: both\"><\/div><!-- relpost-block-container --><div class=\"relpost-block-container relpost-block-column-layout\" style=\"--relposth-columns: 3;--relposth-columns_t: 2; --relposth-columns_m: 2\"><a href=\"https:\/\/www.coretechnologies.com\/blog\/windows\/getting-ready-for-windows-11\/\"class=\"relpost-block-single\" ><div class=\"relpost-custom-block-single\"><img decoding=\"async\" loading=\"lazy\" class=\"relpost-block-single-image\" alt=\"Getting ready for Windows 11\"  src=\"https:\/\/www.coretechnologies.com\/blog\/wp-content\/uploads\/windows-11-preview-logo-150x150-1.png\" style=\"aspect-ratio:1\/1\" style=\"aspect-ratio:1\/1\"><\/img><div class=\"relpost-block-single-text\"  style=\"height: 75px;font-family: Arial;  font-size: 12px;  color: #333333;\"><h2 class=\"relpost_card_title\">Getting ready for Windows 11<\/h2><\/div><\/div><\/a><a href=\"https:\/\/www.coretechnologies.com\/blog\/dropbox-software\/why-three-processes\/\"class=\"relpost-block-single\" ><div class=\"relpost-custom-block-single\"><img decoding=\"async\" loading=\"lazy\" class=\"relpost-block-single-image\" alt=\"Why are there 3 copies of Dropbox Running on my PC?\"  src=\"https:\/\/www.coretechnologies.com\/blog\/wp-content\/uploads\/dropbox-question-150x150.png\" style=\"aspect-ratio:1\/1\" style=\"aspect-ratio:1\/1\"><\/img><div class=\"relpost-block-single-text\"  style=\"height: 75px;font-family: Arial;  font-size: 12px;  color: #333333;\"><h2 class=\"relpost_card_title\">Why are there 3 copies of Dropbox Running on my PC?<\/h2><\/div><\/div><\/a><a href=\"https:\/\/www.coretechnologies.com\/blog\/windows-services\/auto-restart-service\/\"class=\"relpost-block-single\" ><div class=\"relpost-custom-block-single\"><img decoding=\"async\" loading=\"lazy\" class=\"relpost-block-single-image\" alt=\"Q&amp;A: How do I Automatically Restart my Windows Service when it Crashes?\"  src=\"https:\/\/www.coretechnologies.com\/blog\/wp-content\/uploads\/restart-windows-service-150x150-1.png\" style=\"aspect-ratio:1\/1\" style=\"aspect-ratio:1\/1\"><\/img><div class=\"relpost-block-single-text\"  style=\"height: 75px;font-family: Arial;  font-size: 12px;  color: #333333;\"><h2 class=\"relpost_card_title\">Q&amp;A: How do I Automatically Restart my Windows Service when it Crashes?<\/h2><\/div><\/div><\/a><\/div><!-- close relpost-block-container --><div style=\"clear: both\"><\/div><\/div><!-- close filter class --><\/div><!-- close relpost-thumb-wrapper -->","protected":false},"excerpt":{"rendered":"<p>This seems like strange behavior, but when you are using autologon to launch an application in session 1 and reboot the server, during the reboot AlwaysUp briefly relaunches the application in session 0. Here&#8217;s an example from the activity log: &hellip; <a href=\"https:\/\/www.coretechnologies.com\/blog\/alwaysup\/avoid-shutdown-restarts\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":9871,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[26,127,224,149,225],"class_list":["post-9848","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-alwaysup","tag-alwaysup-tag","tag-qa","tag-race-condition","tag-service-shutdown","tag-windows-shutdown"],"_links":{"self":[{"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/9848","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/comments?post=9848"}],"version-history":[{"count":34,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/9848\/revisions"}],"predecessor-version":[{"id":10038,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/9848\/revisions\/10038"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/media\/9871"}],"wp:attachment":[{"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/media?parent=9848"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/categories?post=9848"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/tags?post=9848"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}