Fix PowerManager race condition by raising events synchronously#6226
Draft
guimafelipe wants to merge 1 commit intomainfrom
Draft
Fix PowerManager race condition by raising events synchronously#6226guimafelipe wants to merge 1 commit intomainfrom
guimafelipe wants to merge 1 commit intomainfrom
Conversation
The PowerManager callbacks set cached values then dispatch events asynchronously via co_await resume_background(). This creates a window where a subsequent OS callback can overwrite the cached value before the first event handler reads it. For SystemSuspendStatusChanged, this is reliably reproduced on wake from standby: PBT_APMRESUMEAUTOMATIC and PBT_APMRESUMESUSPEND fire on separate threads in rapid succession, causing both handlers to read ManualResume instead of AutoResume then ManualResume. Fix: Raise all power events synchronously from the OS callback thread instead of posting to the thread pool. This ensures the cached value is stable when the handler reads it. For SystemSuspend specifically, add a mutex to serialize the concurrent OS callbacks. All other callbacks (EnergySaver, Battery, Display, PowerSource, EffectivePowerMode, UserPresence, etc.) have the same theoretical vulnerability and receive the same synchronous dispatch fix. Fixes #5224 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes the race condition in \SystemSuspendStatusChanged\ (issue #5224) using the existing API — no new events or event args types needed.
Root Cause
All PowerManager callbacks follow this pattern:
For \SystemSuspendStatusChanged, this reliably happens on wake from standby: \PBT_APMRESUMEAUTOMATIC\ and \PBT_APMRESUMESUSPEND\ fire on separate threads in rapid succession, causing both handlers to read \ManualResume\ instead of \AutoResume\ then \ManualResume.
Fix
Raise events synchronously from the OS callback thread instead of posting to the thread pool. This ensures the cached value is stable when the handler reads the property.
For \SystemSuspendStatusChanged\ specifically, a \std::mutex\ serializes the concurrent OS callbacks (\PBT_APMRESUMEAUTOMATIC\ and \PBT_APMRESUMESUSPEND\ fire on separate threads).
All other callbacks (\EnergySaver, \Battery, \Display, \PowerSource, \EffectivePowerMode, \UserPresence, etc.) receive the same synchronous dispatch fix since they have the same theoretical vulnerability.
Why not a new API?
PR #6202 proposed adding \SystemSuspendStatusChanged2\ with typed event args. That's unnecessary because the race is in the dispatch mechanism, not the API surface. By raising events synchronously (removing the \co_await resume_background()\ gap), the existing \SystemSuspendStatus\ property always returns the correct value when read from within the event handler.
Changes
Testing
Existing tests verify registration/unregistration and property reads. Full validation requires system suspend/resume events (put computer to standby, wake by pressing power button, verify \AutoResume\ followed by \ManualResume).
Fixes #5224