01 Feb Troubleshooting dwm.exe hangs on Windows 10
I built my PC [a few months ago](https://www.michaelcheng.us/stuff/44). I was really satisfied -- but _obviously_ it started having problems not long after. The first one was random freezes where the mouse couldn't even move. The freezes would be accompanied by 100% CPU usage and my WD Gold drive spinning up. I managed to quickly narrow this down to the Intel Rapid Storage Technology driver. I uninstalled that driver and the issue disappeared. Last September, I upgraded to 32GB of RAM. With that change, I also stopped hibernating (S4 sleep) my machine every night because I believed in the myth (?) that hibernating with 32GB of RAM would be harmful to my SSD. So I started using [S3 sleep](https://en.wikipedia.org/wiki/Advanced_Configuration_and_Power_Interface#Power_states) instead. I noticed out the corner of my eye that the machine would take a long time -- O(minutes) -- to sleep. I ignored it though, because I hadn't used S3 sleep in a while and I forgot if that was normal or not. Partial spoiler alert: not really, but [hybrid sleep](https://www.howtogeek.com/102897/whats-the-difference-between-sleep-and-hibernate-in-windows/) will do this. The other thing I noticed was that sometimes upon resume, everything that uses the GPU would crash. Firefox would be unresponsive with a white screen displayed. Chrome would be completely black and unresponsive. My video player would completely disappear (but immediately appear if I clicked on the transparent window). My desktop wallpaper would also disappear. I'm not too well-versed with debugging Windows problems like this, but I knew about the Event Viewer. So I went in there and saw that upon resume from sleep, there would be messages like `The program dwm.exe version 10.0.18362.387 stopped interacting with Windows and was closed.` There would also be a message saying `The Desktop Window Manager process has exited. (Process exit code: 0xcfffffff, Restart count: 1, Primary display device ID: NVIDIA GeForce RTX 2060)`. Knowing that `dwm.exe` was the Desktop Windows Manager, the first thing I did was check for updated GPU drivers. I went deeper down this rabbit hole than I'm prepared to admit. I used [DDU](https://www.guru3d.com/files-details/display-driver-uninstaller-download.html) multiple times. I installed [DCH drivers](https://nvidia.custhelp.com/app/answers/detail/a_id/4777/~/nvidia-dch%2Fstandard-display-drivers-for-windows-10-faq). I installed standard drivers. I installed Studio drivers. I installed Game Ready drivers. I installed legacy drivers. I tried multiple combinations of these while dancing counter-clockwise under a full moon. Nothing changed the hanging `dwm.exe`. I upgraded my BIOS. I _downgraded_ my BIOS. I replaced the CMOS battery. I learned what each setting in my BIOS did, with some help from my dad, my sister Tiffany, and Wendy—an engineer at American Megatrends. I ran the Windows Memory Diagnostic tool. I disabled LPSM in Windows. I posted on Reddit. I got nothing. Defeated, I decided to just go back to S4 hibernating my machine every night. But then I realized that hibernating had the same issue! I wondered if the long sleep time was somehow related to the hanging resumes. I found out that hybrid sleep would write to the SSD but suspend to RAM. But disabling hybrid sleep had no effect. At this point, I was sick of rebooting almost every day after `dwm.exe` app hang, because what is the point of S3 then? So I decided to try and solve the issue myself. I closed all my programs one by one while S3 sleeping my machine in between. It hung every time. I tried to stop Avast, but it was protected by the OS because of the nature of the application. I thought [f.lux](https://justgetflux.com/) might have been the culprit because it uses the GPU. But closing it did not help. I deduced it wasn't an application-level issue. I unplugged all my USB devices. Nothing changed. I unplugged my network cable. Suddenly, S3 sleep took 3 seconds instead of 3 minutes. And `dwm.exe` no longer hung. Having found a lead now, I installed all kinds of Intel Ethernet drivers. I disabled WOL and disallowed the adapter to wake the machine. I found it did not help. I ordered a USB 3.0 to Ethernet adapter, convinced that I had a bad motherboard. Surprisingly, that did not work. I was totally expecting it to! I noticed that if I unplugged the cable before S3 sleep, everything would be fine. I rebooted my router. It seemed to work, but then it stopped working. Finally, I decided to do some more educated debugging. I installed the Windows ADK to use the [Performance Recorder](https://docs.microsoft.com/en-us/windows-hardware/test/wpt/windows-performance-recorder). I set the scenario to Sleep, and recorded disk, file, and network activity. ![Windows Performance Recorder](/_static/stuff/2020-02-01-performance_recorder.png) I managed to record a S3 sleep event that had a DWM failure. Inspecting the output, I saw the events that happened from right before S3 was initiated, and when the system was actually suspended. The time series begins when sleep is initiated. ![Windows Performance Recorder default view](/_static/stuff/2020-02-01-wp_main.png) I saw that `svchost.exe` kept pinging something over the network. ![svchost.exe pinging the network before sleep](/_static/stuff/2020-02-01-wp_svchost_network.png) Then I looked at file IO and saw Avast doing some weird things. ![AVAST odd disk IO before sleep](/_static/stuff/2020-02-01-wp_avast_disk.png) Notice the thin bar that says "Tracing Off" -- this means it took 110 seconds for the system to actually suspend itself. ![System enters sleep after 110 seconds](/_static/stuff/2020-02-01-tracing_off.png) So, **I decided to uninstall Avast**. Suddenly, all the sleep issues disappeared. And along with it, the `dwm.exe` hangs! Honestly, I'm not even sure why this fixed the issue. I'm certainly not the only one in the world using Avast and S3 / S4 sleep. I'm definitely not the only one who uses HWA in browsers either. Oh, and I'm not even sure if the issue is completely resolved. I just know that I've been able to S3 sleep in 3 seconds every day for the past 7 days already. Hopefully, the computer issues are gone for now.