Skip to content

Giving the service more time to stop when Windows restarts  #489

Closed
@nddipiazza

Description

Feature Request

Problem statement.
For services that have a slow shutdown, Windows gives up on the graceful shutdown of the services and terminates them.

Describe the solution you'd like.

We would like to be able to utilize preShutdown handler.

See this link: https://www.coretechnologies.com/blog/windows-services/increase-shutdown-time/

How to extend the preshutdown timeout beyond 3 minutes

With a simple code change, a preshutdown service can instruct Windows to wait more than the default 3 minutes for the service to end in the preshutdown phase.

You must add code to call the ChangeServiceConfig2 function with the SERVICE_CONFIG_PRESHUTDOWN_INFO level, and the timeout value in the SERVICE_PRESHUTDOWN_INFO structure set to an appropriate value.

Here is what the addition looks like in C++ (error checking omitted for brevity):

SC_HANDLE hService = OpenService(...);
...
SERVICE_PRESHUTDOWN_INFO info;
info.dwPreshutdownTimeout = 5 * 60 * 1000; // 5 minutes
ChangeServiceConfig2(hService, SERVICE_CONFIG_PRESHUTDOWN_INFO, &info);

The new code is best called once when you install/setup your service, but it can be invoked during normal operation as well. Whichever works better in your situation.

Can this somehow be used to make Windows wait longer to restart the service?

Describe alternatives you've considered.
We can also increase a registry timeout parameter to give the service more time to restart, but this does not seem to be a very popular suggestion with the system admins who refuse to touch registry settings for our app.

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions