windows-restart provisioner intermittently loses WinRM session after successful reboot on Azure Windows images
- Dominant language
- Go
- Stars
- 15.8k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
#### Overview of the Issue
The `windows-restart` provisioner can lose the WinRM session after a reboot when provisioning Azure Windows images.
In our environment, Packer successfully triggers the reboot and the VM comes back online. However, immediately after WinRM reconnects, the build intermittently fails because the provisioner appears to perform additional communication or cleanup against the previous remote PowerShell session, which no longer exists after the reboot.
The failure occurs before the next provisioner starts. The VM itself is healthy and WinRM is available again.
As a workaround, I replaced the `windows-restart` provisioner with a custom PowerShell-based reboot implementation that schedules the reboot from inside the guest and lets the next PowerShell provisioner handle the reconnect. With this approach, the issue no longer occurs, even after many consecutive reboots.
This suggests the problem is related to the internal implementation of the `windows-restart` provisioner rather than the reboot itself.
#### Reproduction Steps
1. Build an Azure Windows image using the Azure ARM builder.
2. Use the `windows-restart` provisioner.
3. Repeat multiple reboot cycles during provisioning.
4. Eventually, one of the reboot cycles fails after the VM has already restarted successfully.
5. WinRM becomes available again, but the build terminates before the next provisioner begins.
The issue is intermittent but reproducible in long-running image builds containing many reboots.
### Packer version
```
Packer v1.12.0
```
### Simplified Packer Template
```hcl
provisioner "windows-restart" {
restart_command = "shutdown /r /f /t 0 /c \"Packer restart\""
restart_timeout = "30m"
check_registry = true
restart_check_command = "powershell -command \"Write-Output 'WinRM Ready'\""
}
```
The issue also occurs when using longer reboot delays and extended timeouts.
### Operating system and Environment details
- Packer v1.12.0
- Azure ARM builder
- Windows 11 Enterprise multi-session (Azure Virtual Desktop image)
- WinRM communicator
- Provisioning executed from a Linux container
- Long-running image build with approximately 18 planned reboots
### Log Fragments and crash.log files
The Windows Event Log confirms that the reboot is initiated successfully and the operating system starts normally afterwards.
```
Event ID 1074
User32
Restart initiated by user "packer"
Event ID 6006
Event Log service stopped
Event ID 109
Kernel-Power
Power Action Reboot
Event ID 6005
Event Log service started
```
The VM is fully operational after the reboot and WinRM is reachable again.
During the investigation, no scheduled task, MSI installation, Windows Update, or pending reboot was found that could explain the failure.
Additionally, replacing the `windows-restart` provisioner with a custom PowerShell reboot implementation completely eliminates the issue while using the same VM, communicator, and provisioning sequence.
This strongly suggests that the problem is related to the internal handling of the reboot or remote PowerShell session by the `windows-restart` provisioner itself.
Contributor guide
Research direction
Start at the windows-restart provisioner entry point and reproduce the issue with the supplied Azure ARM template, WinRM communicator, and repeated reboot cycles. Trace what happens after WinRM reconnects and before the next provisioner starts. Done means long-running Azure Windows image builds complete reliably without communicating with the pre-reboot session.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- azure, go, powershell
- Domain
- cloud, devops
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100