wakepy icon indicating copy to clipboard operation
wakepy copied to clipboard

Documentation - How will killing while lock set affect it?

Open JonLevin25 opened this issue 3 years ago • 5 comments

Hi, thanks for this :) While super simple, it's always good to have a cross-platform package, especially when it's source code is easy to read and understand.

One thing that could be a factor in decisions on whether to use this is how force killing a process (Task manager in windows, kill in Linux etc) will affect it?

I've tested on Windows, and it seems that killing the python process successfully removes the lock (makes sense that SetThreadExecutionState would die with the process).

But is this the same for linux / mac? Regardless of result it could be good to document in the readme.

JonLevin25 avatar Nov 22 '21 10:11 JonLevin25

Hi @JonLevin25, glad that you like it!

On the linux & mac versions, I have say honestly that I don't know, and would refer to the documentation of the underlying apps that keep the process awake (_linux.py and _darwin.py).

On Windows, I would actually very much guess that the behaviour is the opposite of what you've suggested. As you have read, in _win.py the SetThreadExecutionState is called, which in my understanding kind of sets a flag for the operating system to not start sleeping:

ES_CONTINUOUS
0x80000000
Informs the system that the state being set should remain in effect until the next call that uses ES_CONTINUOUS and one of the other state flags is cleared.

This also means that in practice one could, with current implementation, accidentally remove this flag with some other process/thread (even outside of python).

I fully agree that some sort of table in the docs would be handy! :)

fohrloop avatar Nov 22 '21 10:11 fohrloop

About windows - I thought the same thing at first. But the name suggests SetThreadExecutionState is local to the current Thread. (which should die with the process)

I've tested this and it is indeed reset:

import wakepy
wakepy.set_keepawake()
while True:
    pass

(In admin terminal)

PS C:\Users\Jon> powercfg -requests
DISPLAY:
None.

SYSTEM:
[PROCESS] ...\AppData\Local\Programs\Python\Python310\python.exe

AWAYMODE:
None.

EXECUTION:
None.

PERFBOOST:
None.

ACTIVELOCKSCREEN:
None.

After killing the process (from PyCharm or Task Manager)

PS C:\Users\Jon> powercfg -requests
DISPLAY:
None.

SYSTEM:
None.

AWAYMODE:
None.

EXECUTION:
None.

PERFBOOST:
None.

ACTIVELOCKSCREEN:
None.

JonLevin25 avatar Nov 22 '21 15:11 JonLevin25

Hi @JonLevin25,

Oh wow, thanks for the info! You're right! Nice catch 😎

The execution state flag is really tied to the process. I tested by using threading.Threads, and whenever a thread is done executing and dies, the flag disappears from powercfg -requests listing. Also, it is possible to have multiple threads (& processes) having flags of their own; all of the processes must either remove their flags or die before windows would go into sleep. See, for example here, there are two processes calling wakepy.set_keepawake, with the second one also having keep_screen_awake=True:

PS C:\WINDOWS\system32>  powercfg -requests
DISPLAY:
[PROCESS] \Device\HarddiskVolume3\Python\Python385-64\python.exe

SYSTEM:
[PROCESS] \Device\HarddiskVolume3\Python\Python385-64\python.exe
[PROCESS] \Device\HarddiskVolume3\Python\Python385-64\python.exe

AWAYMODE:
None.

EXECUTION:
None.

PERFBOOST:
None.

ACTIVELOCKSCREEN:
None.

fohrloop avatar Nov 22 '21 18:11 fohrloop

Np :) If I ever test on linux I'll be sure to update here as well.

JonLevin25 avatar Nov 22 '21 19:11 JonLevin25

Nice! I also added a Summary table to the README.md.

fohrloop avatar Nov 22 '21 19:11 fohrloop

This is now documented for Windows and Linux. Created new ticket (#25) for macOS so this one can be closed. Thanks @JonLevin25 for your contribution !

fohrloop avatar Feb 27 '23 22:02 fohrloop