Diagnosing bugs preventing sleep on Windows

https://news.ycombinator.com/rss Hits: 13
Summary

TL;DR: Diagnosing programs causing insomnia is quite straightforward on Windows. My employer has a policy set for Windows machines which locks each computer automatically after some inactivity. One of my colleagues noticed that if they have recent builds of our product running in the background, this auto-locking does not take effect. I was asked to investigate. The bug itself got pretty low priority, as at first glance the phenomenon does not seem to cause that much trouble. I did not know anything about how all this works on Windows, but I had a hunch that if the lock screen is being prevented, chances are that the computer going to sleep gets the same treatment. This is a much bigger issue, as it might have a huge impact on the battery life of notebooks. Breakpoints The repro was pretty straightforward (launch the program, then wait X minutes, and note how the lock screen did not appear), and I also had the source code for the software being investigated, so my first intuition was to put a breakpoint on the functions or function that lets you interact with this functionality of Windows. The first search result was a function named SetThreadExecutionState: Enables an application to inform the system that it is in use, thereby preventing the system from entering sleep or turning off the display while the application is running. I placed a breakpoint at this function, but there were no hits at all. Okay, the next search result pointed me to PowerCreateRequest and PowerSetRequest. The basic idea of this API is that you create a power request object, and increment/decrement a reference count for a given type of request, as required by your specific use case. Placing breakpoints at these functions revealed the following sequence of events: PowerCreateRequest was called when the application launched, and the UI is displayed Shortly after, PowerSetRequest was called with PowerRequestDisplayRequired There were no further calls to any of these functions during the regular ...

First seen: 2025-04-06 01:12

Last seen: 2025-04-06 13:14