Windows: fix wrapper around OpenProcess (pid_exists() no longer lies) #1094
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.
So this is kind of a big one and the repercussions are hard to describe because it's subtle. This test case:
psutil/psutil/tests/__init__.py
Lines 387 to 399 in cee414d
...which was disabled on Windows, shows that processes unexpectedly stick around after being
terminate()
d andwait()
ed on. The problem originates fromOpenProcess()
Windows API.OpenProcess()
is unreliable in that it can return a handle for a process PID which no longer exists, so we assume thatGetExitCodeProcess
returningSTILL_ACTIVE
means it's running, else it isn't. It turns out this is not always the case. This had repercussions onpsutil.pid_exists()
API which returned an incorrect value, and also on other psutil APIs relying on it internally (cmdline()
,environ()
,cwd()
,connections()
). This PR fixes this logic and assumes a process handle is actually running if:OpenProcess
doesn't return ERROR_INVALID_PARAMETERGetExitCodeProcess
must returnSTILL_ACTIVE
Full logic here:
psutil/psutil/arch/windows/process_info.c
Lines 193 to 245 in 8184b83
The logic of
pid_exists()
changed in an almost identical manner:psutil/psutil/arch/windows/process_info.c
Lines 369 to 441 in 8184b83