giampaolo / psutil

Cross-platform lib for process and system monitoring in Python
BSD 3-Clause "New" or "Revised" License
10.22k stars 1.38k forks source link

[Windows] pid_exists() and Process() disagree on whether a pid exists when ERROR_ACCESS_DENIED #2394

Closed giampaolo closed 5 months ago

giampaolo commented 5 months ago

Summary

Description

On Windows, pid_exists() may return True but psutil.Process() raises NoSuchProcess. Internally, this happens because of: https://github.com/giampaolo/psutil/blob/034a1a6996d4ff5116fc45a9c5ed8477d0d73d37/psutil/arch/windows/proc_utils.c#L176-L178.

Differently from UNIX, the assumption in the code that ERROR_ACCESS_DENIED means there's a process to deny access to (hence it exists) is wrong. We therefore remove this assumption and also write a test case which ensures that pid_exists(), Process() and pids() APIs are all consistent with each other.

As a bonus, I also discovered there are "hidden" PIDs on Windows (oh well!).