The Core Technologies Blog

Professional Software for Windows Services / 24×7 Operation


Q&A: Why Doesn’t AlwaysUp Catch my Endless Loop?

Q & A - Detect Infinite Loops
  Following your AlwaysUp documentation, I am using the “Monitor the application and stop it whenever it hangs” option. When I set its value to 1 minute, this feature does not work as I expect.

I wrote a simple application that goes into an endless loop after one minute of execution time. But the application is not restarted by AlwaysUp. Why not?

— Beatrix

Hi Beatrix. Not all infinite loops can be detected by AlwaysUp. Let me explain…

AlwaysUp catches non-responsive GUI applications

AlwaysUp’s hang detection feature is designed to identify a specific situation — where a Windows GUI application is not responding to the operating system.

We have all encountered this type of hang before. It happens when Firefox suddenly locks up, or when Excel goes gray and the cursor turns into a spinning wheel. The application is frozen and all input is ignored.

If you are lucky, the disruption only lasts a few seconds; if not, you may have to forcibly terminate the application from the Task Manager. Very annoying!

Beyond the gray window and spinning cursor, there are a couple of telltale signs when an application is hung/non-responsive:

  1. You will see “Not responding” in the application’s title bar.

    Outlook Not Responding
  2. The application’s process will be marked as “Not responding” in the Task Manager:

    Task Manager Process Not Responding

Furthermore, Windows may throw up a warning outlining your very limited options:

Excel Process Not Responding

The good news is that AlwaysUp has you covered in all of those situations!

How AlwaysUp checks if your application is hung

When you enable hang-checking, the AlwaysUp Windows Service component executes the following procedure every few seconds:

  1. Scans the desktop to identify all top-level windows belonging to the application being monitored.

  2. Sends each of those top-level windows a WM_NULL (no-op) message.

  3. Waits a few seconds for each of the windows to acknowledge the message.

  4. Declares the application a hang if no windows respond.

An application that remains hung over the user-supplied duration (1 minute, in your case) will be terminated and restarted.

Not all endless loops result in a frozen/non-responsive application

There are a couple of consequences stemming from the way that AlwaysUp checks for a hang.

First, an application without a top-level window can never be classified as a hang.

For example, this simple, non-GUI C# program that loops forever will escape detection:

using System;
namespace InfiniteLoop {
   class Program {
      static void Main(string[] args) {
         while (true) {
            Thread.Sleep(1000);
         }
      }
   }
}

Second, if an application creates multiple windows, all windows must be non-responsive for AlwaysUp to declare a hang. If even a single window processes the WM_NULL message, the application will be classified as responsive.

My guess is that your endless loop test fits into one of the scenarios above. Unfortunately AlwaysUp’s hang detection feature will not be able to help there. Sorry about that!

But all is not lost…

Use the Sanity Check feature to detect endless loops and other failures

There can be many indications that a program is stuck. For example, an application might:

  • stop responding to requests over the network;

  • stop writing entries to a status file;

  • report an “I have failed” message to a log file;

  • or leave files collecting dust in a specific “inbox” folder

In situations like these — where the failure is detectable by a script or simple utility — you should look to AlwaysUp’s powerful Sanity Check feature. It allows you to extend failure detection to encompass anything you like, including the issues listed above.

The Sanity Check page digs into the technical details, which we will not repeat here. Please review the documentation and get in touch if you need help to use that flexible functionality.

Posted in AlwaysUp | Tagged , , , , | Comments Off on Q&A: Why Doesn’t AlwaysUp Catch my Endless Loop?

Comments are closed.