Windows Power Requests for Long Running Dev Processes

Keep long-running builds awake on Windows with the modern power request API.

Cover illustration for “Windows Power Requests for Long Running Dev Processes”

Windows power requests let long-running development processes tell the kernel that work must go on even when the user is inactive. For builds, CI agents, training loops, and AI coding sessions, the correct pattern is a scoped, auditable request that ends with the work. That pattern prevents avoidable interruptions, but Modern Standby on battery also requires checkpointing and resume-aware design.

Why Windows decides to sleep during your build — and what actually stops it

A build left running overnight can stop without an application exception or crash, leaving hours of work undone. The absence of an error makes this failure harder to diagnose than an ordinary build failure. Windows treats keyboard and mouse inactivity as a signal that the machine is idle, even while a CPU-intensive process is working. CPU load does not inherently tell the operating system that the process is doing user-expected work.

The missing signal belongs at the kernel boundary. Before initiating automatic sleep, Windows checks power requests that express a temporary need to keep working. If no request applies, the machine can go to sleep as usual when idle, whatever the CPU load. A timer setting cannot substitute for a programmatic request tied to the work’s lifetime. Conversely, a request that outlives its work creates the opposite failure, keeping a machine awake when it should sleep.

The three request types and what each one actually prevents

SYSTEM, DISPLAY, and EXECUTION express different requirements rather than interchangeable levels of sleep protection. SYSTEM, represented by PowerRequestSystemRequired, keeps the machine from going to sleep because the user has been inactive. The display can still turn off, and the session can still lock. That makes SYSTEM the appropriate request for headless background work such as builds and CI agents.

DISPLAY, represented by PowerRequestDisplayRequired, prevents the display from turning off. It is appropriate when a person needs to watch live output. Keeping a screen lit for unattended work wastes battery without expressing the process’s actual requirement.

EXECUTION, represented by PowerRequestExecutionRequired, protects the calling process from suspension or termination by process lifetime management. On Windows 8+, keeping the system awake and keeping the process running are distinct concerns. Request entries also identify the caller’s role through tags, including [PROCESS] for an application, [SERVICE] for a background service, and [DRIVER] for a device driver. Those tags later make it possible to connect a live request to its issuer during diagnosis.

The two APIs for issuing power requests and why the modern one is the right default

The legacy API expresses sleep prevention through thread state. Calling SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED) suppresses automatic idle sleep while that thread holds the state. Another call on the same thread, including one from a library, can overwrite it. Suppression disappears if the process dies, but the shared use of that thread’s state makes ownership harder to reason about. The machine can still sleep when the user presses the power button, closes the lid, or chooses sleep from the Start menu.

The modern PowerCreateRequest and PowerSetRequest family gives the caller an owned handle and an explicit reason string. PowerSetRequest increments the kernel reference count for a request type, and PowerClearRequest decrements it when the work finishes. The reason appears in powercfg /requests, making the request attributable to both a caller and a purpose. This ownership and visibility make the modern API the right default, rather than a stronger claim to override every sleep transition.

A minimal implementation sets a REASON_CONTEXT to POWER_REQUEST_CONTEXT_VERSION with POWER_REQUEST_CONTEXT_SIMPLE_STRING and assigns its Reason.SimpleReasonString. It passes that context to PowerCreateRequest, checks the returned handle, and calls PowerSetRequest(handle, PowerRequestSystemRequired) followed by PowerSetRequest(handle, PowerRequestExecutionRequired), checking each result. The work to be protected runs only after all the requests it needs have succeeded. A finally or cleanup block calls PowerClearRequest once for each set that succeeded, then calls CloseHandle. Cleanup must track which calls succeeded so it can release each reference acquired, even if setup fails partway through.

Real cases where the wrong API caused silent failures — and how the right one fixed them

KiroCrew’s September 2026 gateway logs showed that SetThreadExecutionState had been called to keep the machine from sleeping. Nevertheless, the system entered Modern Standby approximately six and a half minutes later because of an idle timeout. It exited only after keyboard input nearly ten hours later, leaving the long task aborted. The proposed fix was to replace or supplement the legacy call with PowerCreateRequest and both SYSTEM and EXECUTION requests, released through PowerClearRequest. The record leaves successful deployment of the proposed correction unverified.

Microsoft PowerToys “Awake” illustrates a tool whose implementation still used the legacy path in the cited open issue from 2026. Its keep-awake implementation invoked SetThreadExecutionState through src/modules/awake/Awake/Core/Native/Bridge.cs, with no PowerCreateRequest or PowerSetRequest in the module. The issue proposed moving to the modern API for SYSTEM and, where process-running intent applied, EXECUTION. That change would also expose the requests under the corresponding categories in powercfg /requests.

In contrast, cc-caffeine’s native sleep backend for Claude Code uses PowerCreateRequest and PowerSetRequest to create a SYSTEM request. It supplies the explicit reason string cc-caffeine: Claude Code session active, so an operator can identify why the session is holding sleep. Corsinvest’s pull request for the cv4vs-agents Visual Studio extension also chose the modern API so its requests could be seen. It also noted that even with better ownership, system power requests on Modern Standby battery power end five minutes after the sleep timeout expires.

Where Modern Standby breaks the rules that traditional sleep follows

Modern Standby, or S0 Low Power Idle, is a different power model from traditional S3 sleep. Its name can conceal the important distinction between the system occupying S0 and a desktop process continuing useful execution. The Desktop Activity Moderator suspends desktop-process threads during Modern Standby, while services in Session 0 are throttled instead. On the hardware described in the notes, powercfg /a reports Standby (S0 Low Power Idle) Network Connected, with S1, S2, and S3 unavailable in firmware. The two sleep models are mutually exclusive, so S3 assumptions cannot simply carry over.

Power requests still do their job on Modern Standby systems running on AC when they are scoped to the work. On DC power, however, Windows terminates system and execution required requests five minutes after the system sleep timeout expires. User-initiated sleep also terminates requests, apart from the PowerRequestAwayModeRequired exception on traditional S3 systems. Moving to the modern API therefore does not grant permission to defeat a deliberate sleep action.

Windows 11 version 24H2 adds another battery-related boundary. When Modern Standby detects excessive battery drain, a power-saving measure disables most wake sources, leaving the power button or opening the lid as the ways to wake the device. Application power requests cannot override that Windows-level decision. The DC five-minute cap and the 24H2 battery-drain override are therefore specific limits on otherwise effective power requests.

Transition detection and timekeeping also require care under this model. Traditional WM_POWERBROADCAST suspend and resume handling and SetSuspendState() do not work reliably for Modern Standby. Applications must instead use PowerRegisterSuspendResumeNotification() or RegisterSuspendResumeNotification() for transition notifications. Since Windows 8, relative waits such as SleepEx and the relative form of SetWaitableTimer exclude time asleep and retain their remaining duration after resume. By contrast, QueryPerformanceCounter, the basis for Stopwatch, counts sleep time, while QueryUnbiasedInterruptTime does not.

What to do when power requests are not enough: checkpointing and resume-resilient design

These limits make restart capability a requirement for long-running processes that may run on battery-powered hardware after 24H2. Requests prevent avoidable idle interruptions, while persisted progress limits the cost of interruptions the application cannot prevent.

For builds, object files and cached artifacts already provide natural checkpoints, but the coordinator must persist enough state to resume. For self-hosted CI agents, the corresponding pattern is re-registration and job resumption from a persisted step log. For training loops, it is epoch-level or step-level checkpoint saves with explicit resume-from-checkpoint loading. Each pattern preserves the workload’s progress at a boundary it already understands.

Recovery also needs a reliable indication that suspension occurred. Modern Standby notification registration lets the application respond after resume, reconnect dropped network state, and re-issue power requests. A TCP connection left idle during sleep can be silently dropped by a NAT, firewall, or load balancer. Azure Load Balancer’s default idle timeout is 4 minutes, so an apparently intact process cannot assume its connections remain usable. The code should reconnect as part of the resume path, before the next unexplained failure.

How to audit what is actually holding or breaking sleep with powercfg

Begin with powercfg /a to establish which sleep states the target hardware supports. If S3 is unavailable and S0 Low Power Idle is present, the diagnosis must account for Modern Standby. This first step separates a hardware-model limit from an application request problem. Without it, a correct implementation can be judged against guarantees the machine does not provide.

Next, run powercfg /requests as administrator to inspect the requests active now. Its categories include Display, System, Awaymode, Execution, and Perfboost, with caller names and tags identifying the issuers. The reason string supplied through PowerCreateRequest is the direct payoff of the modern API. A process name without a reason string can signal the legacy path. This output provides only a live snapshot of sleep requests.

For a specific insomnia suspect, powercfg /requestsoverride PROCESS app.exe SYSTEM suppresses that process’s SYSTEM request at the operating-system level. This allows investigation without modifying the application, but it changes behavior. An override can block a request that is needed and let the machine sleep during work that should stay protected. It therefore requires care when distinguishing an unnecessary blocker from a necessary one.

Finally, powercfg /sleepstudy shows how energy is used on Modern Standby systems. It reports power consumption and DRIPS, or Deepest Runtime Idle Platform State, metrics for each session. Those measures help teams assess whether build agents or AI coding sessions are holding machines out of their lowest power state across a fleet.

Putting it together: a decision pattern for scoping power requests correctly

The decision starts with what the work requires, then moves to the hardware power model and whether AC or DC operation is in scope. For headless builds, CI agents, and training loops on AC, use SYSTEM and EXECUTION through PowerCreateRequest and PowerSetRequest, with a descriptive reason string. Add DISPLAY only when a person must watch the output. For the same work on Modern Standby battery power, retain those requests and add checkpointing and resume handling because uninterrupted execution cannot be guaranteed.

Scope the request to the work rather than treating it as a permanent machine policy. Microsoft’s guidance is explicit that PowerClearRequest should be called as soon as the scenario finishes. For Claude Code sessions, cc-caffeine offers a way to tie the modern SYSTEM request and a named reason to the session.

The final test is the audit loop. Run powercfg /requests before and after deploying the process. Verify that its reason string appears during protected work and disappears when that work finishes. Check powercfg /a on the target hardware so the result is interpreted under the correct sleep model. A request that is necessary, attributable, and promptly released is the mechanism working as intended.

Sources

  1. Sleep, Hibernation, Modern Standby, and Long-Running Apps

    Provided the Azure Load Balancer idle timeout figure cited in the resume-resilient design section.

Tomás Guerrero-Ibáñez