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

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

Merged
merged 6 commits into from
Apr 6, 2024

Conversation

giampaolo
Copy link
Owner

Summary

  • OS: Windows
  • Bug fix: yes
  • Type: core
  • Fixes: 2359

Description

On Windows, pid_exists() may return True but psutil.Process() raises NoSuchProcess. Internally, this happens because of:

// Access denied means there's a process to deny access to.
if ((hProcess == NULL) && (GetLastError() == ERROR_ACCESS_DENIED))
return 1;
.

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!).

@giampaolo giampaolo merged commit 5bac142 into master Apr 6, 2024
25 of 27 checks passed
@giampaolo giampaolo deleted the 2359-win-pid-exists-lies branch April 6, 2024 15:29
@giampaolo giampaolo changed the title pid_exists() and Process() disagree on whether a pid exists when ERROR_ACCESS_DENIED [Windows] pid_exists() and Process() disagree on whether a pid exists when ERROR_ACCESS_DENIED Apr 6, 2024
ddelange added a commit to ddelange/psutil that referenced this pull request Apr 9, 2024
…mpaolo-master

* 'master' of https://github.com/giampaolo/psutil:
  Fix workflow visibility badges in README (giampaolo#2399)
  OpenBSD: pid_exists() returns True for thread IDs (TIDs) (giampaolo#2395)
  pid_exists() and Process() disagree on whether a pid exists when ERROR_ACCESS_DENIED (giampaolo#2394)
  fix ruff errors
  Fix typos again (giampaolo#2388)
  fix doc style
  update HISTORY / CREDITS
  Include CoreFoundation/CoreFoundation.h (giampaolo#2364)
  Tests: Compare floats less strictly (giampaolo#2372)
  chore: build macOS arm64 wheels on macos-14 (giampaolo#2375)
  Update to fix OSX older version build failure (giampaolo#2379)
  Makefile: define a PYTHON_ENV_VARS var to use with the $PYTHON var.
  fix win tests
  refact serialization tests
  always use unittest.SkipTest where needed
  Add pickle support to psutil Exceptions (giampaolo#2380)
  enable ruff preview mode
ddelange added a commit to ddelange/psutil that referenced this pull request Apr 9, 2024
* giampaolo-master:
  Fix workflow visibility badges in README (giampaolo#2399)
  OpenBSD: pid_exists() returns True for thread IDs (TIDs) (giampaolo#2395)
  pid_exists() and Process() disagree on whether a pid exists when ERROR_ACCESS_DENIED (giampaolo#2394)
  fix ruff errors
  Fix typos again (giampaolo#2388)
  fix doc style
  update HISTORY / CREDITS
  Include CoreFoundation/CoreFoundation.h (giampaolo#2364)
  Tests: Compare floats less strictly (giampaolo#2372)
  chore: build macOS arm64 wheels on macos-14 (giampaolo#2375)
  Update to fix OSX older version build failure (giampaolo#2379)
  Makefile: define a PYTHON_ENV_VARS var to use with the $PYTHON var.
  fix win tests
  refact serialization tests
  always use unittest.SkipTest where needed
  Add pickle support to psutil Exceptions (giampaolo#2380)
  enable ruff preview mode
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant