Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Windows/SetThreadExecutionState based modes on same thread are not isolated from each other #167

Closed
fohrloop opened this issue Jan 19, 2024 · 1 comment · Fixed by #342
Labels
Type: Bug Something isn't working

Comments

@fohrloop
Copy link
Owner

fohrloop commented Jan 19, 2024

As mentioned on #60, there is a bug in the old implementation (wakepy/_deprecated/_windows.py). Since the SetThreadExecutionState changes "execution state" of current thread to either

  1. Prevent display power management
  2. Prevent suspend
  3. No prevented

if someone uses multiple wakelocks / activates multiple modes in a single thread, it will not work. For example this:

def foo():
   baz()
   with keep.running():
        quux()

with keep.presenting():
    foo()
    bar()
  • Start with entering keep.presenting mode.
  • keep.presenting mode is active during baz()
  • This is overridden completely when entering keep.running mode, which is active during quux(). Only sleep is prevented, but display might turn off.
  • When the keep.running mode exits, the whole execution state is cleared with ES_CONTINUOUS
  • The bar() is being run without any mode active at all. System could go to sleep and display might turn off.

This bug has been there since the first version of wakepy.

Task

  • Make it possible to use two modes in same thread at same time.
@fohrloop
Copy link
Owner Author

fohrloop commented Jun 2, 2024

Minimal example to reproduce the issue is:

from wakepy import keep
import time 

def foo():
   with keep.running():
       ...

with keep.presenting():
    foo()
    time.sleep(1000)

run powercfg /requests and you'll see that there is any flags set:

PS C:\Users\niko> powercfg /requests
DISPLAY:
None.

SYSTEM:
None.

AWAYMODE:
None.

EXECUTION:
None.

PERFBOOST:
None.

ACTIVELOCKSCREEN:
None.

fohrloop added a commit that referenced this issue Jun 2, 2024
…ther (#342)

Fixes: #167

Previously: 
Using SetThreadExecutionState Method twice on same thread (having both
active at same time) would make the methods interfere with each other,
as the flags set are global to the thread. 

Now:
The SetThreadExecutionState Method may have multiple modes (same or
different) activated within the same python thread without them
interfering with each other on activation or deactivation, as wakepy
creates a separate worker thread for the single purpose of setting and
keeping the thread execution flag each time you activate a mode with the
SetThreadExecutionState wakepy.Method.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Something isn't working
Projects
None yet
1 participant