dasMulli/dotnet-win32-service

Ability to request additional time

gregolsky opened this issue · 2 comments

Some services may take longer to start/stop than default (seems 60 seconds at max). Tried this by putting Thread.Sleep(Timespan.FromSeconds(120)) in the Stop() method implementation and then trying to sc.exe stop SERVICE_NAME. After 1 minute it failed with the following message: The service did not respond to the start or control request in a timely fashion..

Is there currently a way to request additional time for stopping/starting?

The waitHint that is passed by default is 3000 (https://github.com/dasMulli/dotnet-win32-service/blob/master/src/DasMulli.Win32.ServiceUtils/SimpleServiceStateMachine.cs#L36) so the operation would be considered timed out after 3 seconds.

The same value is used for starting at https://github.com/dasMulli/dotnet-win32-service/blob/master/src/DasMulli.Win32.ServiceUtils/Win32ServiceHost.cs#L119

The idea API-wise is that if the operation takes longer, additional calls to ReportServiceStatus (win32 side: SetServiceStatus) will have to be wait to signal that the pending operation is still in progress.

According to docs, the maximum value is controlled by the registry value HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\WaitToKillServiceTimeout which is documented to default to 20000 (20 seconds) - but is 5000 on my win10.
I don't know if call the SetServiceStatus function repeatedly will allow the service to be pending longer than this time span but it seems possible..

You can try by copying SimpleServiceStateMachine and trying some values (Win32ServiceHost has a constructor for passing in alternative state machines). ServiceBase has a RequestAdditionalTime(int milliseconds) but I don't know how to cram it into the "simple" default interface without making breaking API changes - that's what the state machine interface is for.

Thanks for the hint. I'll try with ReportServiceStatus() call and the state machine. I'll close this for now and reopen, if I have more trouble with this.