puppetlabs/puppet — pull request #854 by joshcooper (2012-06-12 22:07:42+00:00)
Title: (#13489) Synchronously start and stop services

Previously, we were using the `win32-service` gem to start and stop
services. It uses Win32 APIs to programmatically send start and stop
requests. The actual service starts/stops asynchronously with respect to
the request. As a result, when refreshing a service, puppet would issue a
stop request, immediately followed by a start request, and that would race
as the service would often still be running and in the process of stopping
when the start request occurred.

This commit changes the windows service provider to use `sc.exe` to start
and stop services. The main benefit is that it performs these operations
synchronously, eliminating the race.

This is much cleaner than calling `StartService` and `StopService` and
polling with `QueryServiceStatus`.

As far as I can tell, sc.exe is available in 2003 (non-R2) and all future
versions. Previous to 2003, it was part of the NT Resource Kit.

The utility could also be used to configure the service, though that is
outside the scope of this ticket.