Skip to content

Shutdown & Teardown

Akram El Assas edited this page Feb 5, 2026 · 9 revisions

OS Shutdown Handling (v6.2+)

Servy v6.2 introduces high-reliability teardown during OS shutdown/reboot, mimicking the behavior of robust wrappers like NSSM.

Key Features

  • Pre-Shutdown Registration: Intercepts SERVICE_CONTROL_PRESHUTDOWN to start cleanup before the standard shutdown phase begins.
  • SCM Heartbeats: Periodically reports progress (Wait Hints and Checkpoints) to the Windows Service Control Manager (SCM) to prevent "forced termination."
  • Synchronized Teardown: Built-in protection against race conditions if Stop and Shutdown signals arrive simultaneously.

Mandatory Reinstallation

IMPORTANT: You must reinstall your service using the Servy v6.2+ executable. Older installations do not have the SERVICE_ACCEPT_PRESHUTDOWN flag registered in the Windows SCM database.

Extending the Shutdown Window

By default, Windows provides a grace period for services to stop during shutdown. If your service and all associated hooks complete within 30 seconds, no action is needed.

If your service requires more time, you must manually increase the global OS limits. Note that the Service Control Manager (SCM) reads these values only at boot, so a reboot is required after applying these changes.

The following PowerShell script can be used to extend the shutdown timeout. Run it as Administrator, set your desired timeout in milliseconds, and then reboot the system.

# Registry path
$registryPath = "HKLM:\SYSTEM\CurrentControlSet\Control"

# Timeout in milliseconds (example: 80 seconds)
$timeoutValue = 80000

# 1. Set PreshutdownTimeout (DWORD, milliseconds)
if (Get-ItemProperty -Path $registryPath -Name "PreshutdownTimeout" -ErrorAction SilentlyContinue) {
    Set-ItemProperty -Path $registryPath -Name "PreshutdownTimeout" -Value $timeoutValue
} else {
    New-ItemProperty -Path $registryPath -Name "PreshutdownTimeout" -Value $timeoutValue -PropertyType DWord -Force
}

# 2. Set WaitToKillServiceTimeout (REG_SZ, milliseconds)
Set-ItemProperty -Path $registryPath -Name "WaitToKillServiceTimeout" -Value "$timeoutValue"

Write-Host "Shutdown timeouts updated to $timeoutValue ms."
Write-Host "A system reboot is required for these changes to take effect."

Notes:

  • PreshutdownTimeout specifies the maximum time the OS waits for services registered for pre-shutdown.
  • WaitToKillServiceTimeout controls how long the OS waits for all services to stop before forcing termination.
  • Adjust $timeoutValue according to the maximum time your service needs to stop cleanly.

Clone this wiki locally