The Core Technologies Blog

Our Software // Windows Services // 24×7 Operation


Q&A: Why does AlwaysUp Restart my Application During Windows Shutdown?

Q&A: Why does AlwaysUp Restart my Application During Windows Shutdown?
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’s an example from the activity log:

AlwaysUp System Shutdown Log

Why does that happen? How do I prevent it from happening?

— Kevan

Hi Kevan. We have seen this before…

You’re encountering a Race Condition as Windows shuts down

Unfortunately you are experiencing the effects of the chaotic and poorly documented Windows shutdown process.

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.

That system-initiated logoff terminates Session 1. Windows stops all programs running in Session 1 — forcibly if necessary. And since your application is running in Session 1, it’s terminated too. That happens at about 10:26:08.

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:

Session 1 closed

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’s no longer possible. The only reliable place to restart your application is in the session that is always available to Windows Services — the isolated Session 0.

So AlwaysUp starts your application in Session 0. The log line at 10:26:09 confirms that action:

Application started in Session 0

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:

Windows shutdown initiated

And finally, a mere two seconds later, both the application and service stop.

So the only hiccup — as you mentioned — is the unnecessary, “phantom” restart that happens at 10:26:08. This is because AlwaysUp is not aware of the system shutdown at the time when it detects the application outage. AlwaysUp is just doing it’s job, trying to make sure that your application is always running, 24×7.

This is a classic race condition, where two events (“session closed” and “system shutting down” notifications) occur in an unpredictable order. When AlwaysUp is notified of the shutdown first, it won’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’re going to get. 🙂

How to avoid application restarts during Windows Shutdown

To overcome the problem, configure AlwaysUp to wait for a few seconds before restarting your application. Make that change on the Restart tab.

For example, here we have instructed AlwaysUp to delay for 15 seconds:

AlwaysUp: Delay restart

That short pause should provide enough time for AlwaysUp to receive the Windows shutdown notification and exit gracefully — without a phantom restart.

Of course, this adjustment means that AlwaysUp will not restart your program immediately if it crashes. If that’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.

Posted in AlwaysUp | Tagged , , , , | Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *