23 Apr The fix for Windows machines going to sleep for no reason
I started hosting photos on my home server to de-Google. It was an interesting experience overall, which included setting up a DNS server and fiddling with Wake on Wireless LAN. I'll explain the process below, and finally touch upon the fix for an annoying problem I encountered where the server would go to sleep for seemingly no reason.
I already had [PIVPN](https://www.pivpn.io/) installed on my Raspberry Pi. From there, I set up a DNS server so I could point photos.local to an IP address within the network. I had a choice between `dnsmasq` and `Bind9` for the server, and I chose `Bind9` because it seemed like the more "correct" choice. Digital Ocean [explains](https://www.digitalocean.com/community/tutorials/how-to-configure-bind-as-a-private-network-dns-server-on-ubuntu-18-04) the setup well.
After the server was running and I could connect to it, I wanted a way to wake it up so I wouldn't have to keep it always on. Since I didn't have an Ethernet port where I wanted to place the server, I had it running on a 5GHz USB Wi-Fi adapter. I found that it's almost impossible to get WoWLAN working, since the adapter needs to be powered in order to receive the packet. Again, I had 2 choices. I could either move the server to a place where I had an Ethernet port, or buy a Wi-Fi extender with an Ethernet port. I decided on the latter since latency is not critical for this server, and Wi-Fi seems stable enough.
After setting that up, WOL worked perfectly. I soon ran into another issue though. The server would go offline after a few minutes of waking it up via WOL. As I had it running headless in a closet, I could only debug via RDP. Originally, I thought RDP logging out triggered the sleep. Indeed, I found [this question](https://superuser.com/questions/1827005/prevent-windows-11-from-going-to-sleep-after-rdp-session-was-closed) on Stack Exchange which had a very similar problem with no solution.
I later found out the server would sleep even if I never connected to RDP. I investigated further and that brought me to the Event Log.
![Windows 11 event log with the reason for entering sleep](/_static/stuff/2024-04-23-event_log.png)
I did some more researching and found [this question](https://superuser.com/questions/1693220/windows-11-goes-to-sleep-mode-after-a-long-time-in-lock-mode-even-though-i-have). One [unaccepted answer](https://superuser.com/a/1795470) showed a very interesting option that I didn't have in my power options: "System unattended sleep timeout".
I found [this tutorial](https://www.tenforums.com/tutorials/72133-add-system-unattended-sleep-timeout-power-options-windows.html) to enable the hidden option in power options. For posterity, the commands are duplicated here:
1. Open an elevated command prompt
1. Add the option with this command
```
REG ADD HKLM\SYSTEM\CurrentControlSet\Control\Power\PowerSettings\238C9FA8-0AAD-41ED-83F4-97BE242C8F20\7bc4a2f9-d8fc-4469-b07b-33eb785aaca0 /v Attributes /t REG_DWORD /d 2 /f
```
1. Remove the option with this command
```
powercfg -attributes SUB_SLEEP 7bc4a2f9-d8fc-4469-b07b-33eb785aaca0 +ATTRIB_HIDE
```
I set the timeout to 0 minutes, and the problem was resolved.
![The hidden power option "System unattended sleep timeout" set to 0 minutes](/_static/stuff/2024-04-23-power_option.png)
Comments