Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gracefully handling shutdown of a Windows service

Assume that you have a multi-threaded Windows service which performs lots of different operations which takes a fair share of time, e.g. extracting data from different data stores, parsing said data, posting it to an external server etc. Operations may be performed in different layers, e.g. application layer, repository layer or service layer.

At some point in the lifespan of this Windows service you may wish to shut it down or restart it by way of services.msc, however if you can't stop all operations and terminate all threads in the Windows service within the timespan that services.msc expects to be done with the stop procedure, it will hang and you will have to kill it from Task Manager.

Because of the issue mentioned above, my question is as follows: How would you implement a fail-safe way of handling shutdown of your Windows service? I have a volatile boolean that acts as a shutdown signal, enabled by OnStop() in my service base class, and should gracefully stop my main loop, but that isn't worth anything if there is an operation in some other layer which is taking it's time doing whatever that operation is up to.

How should this be handled? I'm currently at a loss and need some creative input.

like image 498
Maritim Avatar asked Aug 07 '14 09:08

Maritim


People also ask

What is gracefully shutdown?

A graceful shutdown is when a computer is turned off by software function and the operating system (OS) is allowed to perform its tasks of safely shutting down processes and closing connections. A hard shutdown is when the computer is forcibly shut down by interruption of power.

What is graceful shutdown in Linux?

A graceful shutdown, on the other hand is when you switch off your Linux system using embedded software functions while allowing the OS to finalise and close remaining tasks and save any in-flight data to the disks. This is also called Planned Shutdown.

What is the last process performed by the Windows shut down routine?

Shutdown signal is sent After Windows is done with itself, and safely halted operations, it then sends a signal to the power management hardware of your computer to turn off the power.


1 Answers

I would use a CancellationTokenSource and propagate the cancellation token from the OnStop method down to all layers and all threads and tasks started there. It's in the framework, so it will not break your loose coupling if you care about that (I mean, wherever you use a thread/Task you also have `CancellationToken' available.

This means you need to adjust your async methods to take the cancellation token into consideration.

You should also be aware of ServiceBase.RequestAdditionalTime. In case it is not possible to cancel all tasks in due time, you can request an extension period.

Alternatively, maybe you can explore the IsBackground alternative. All threads in your windows service that have this enabled are stopped by the CLR when the process is about to exit:

A thread is either a background thread or a foreground thread. Background threads are identical to foreground threads, except that background threads do not prevent a process from terminating. Once all foreground threads belonging to a process have terminated, the common language runtime ends the process. Any remaining background threads are stopped and do not complete.

like image 50
Marcel N. Avatar answered Nov 09 '22 03:11

Marcel N.