Skip to content

Commit

Permalink
recognize faulty process
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Dec 18, 2023
1 parent 3c4ae51 commit c19fa04
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
21 changes: 21 additions & 0 deletions psutil/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
'unittest', 'skip_on_access_denied', 'skip_on_not_implemented',
'retry_on_failure', 'TestMemoryLeak', 'PsutilTestCase',
'process_namespace', 'system_namespace', 'print_sysinfo',
'is_win_secure_system_proc',
# fs utils
'chdir', 'safe_rmpath', 'create_exe', 'get_testfn',
# os
Expand Down Expand Up @@ -1297,6 +1298,26 @@ def print_sysinfo():

if WINDOWS:
os.system("tasklist")
elif which("ps"):
os.system("ps aux")


def is_win_secure_system_proc(pid):
# see: https://github.com/giampaolo/psutil/issues/2338
@memoize
def get_procs():
ret = {}
out = sh("tasklist.exe /NH /FO csv")
for line in out.splitlines()[1:]:
bits = [x.replace('"', "") for x in line.split(",")]
name, pid = bits[0], int(bits[1])
ret[pid] = name
return ret

try:
return get_procs()[pid] == "Secure System"
except KeyError:
return False


def _get_eligible_cpu():
Expand Down
8 changes: 5 additions & 3 deletions psutil/tests/test_contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
from psutil._compat import long
from psutil._compat import range
from psutil._compat import unicode
from psutil.tests import APPVEYOR
from psutil.tests import CI_TESTING
from psutil.tests import GITHUB_ACTIONS
from psutil.tests import HAS_CPU_FREQ
Expand All @@ -51,6 +50,7 @@
from psutil.tests import create_sockets
from psutil.tests import enum
from psutil.tests import is_namedtuple
from psutil.tests import is_win_secure_system_proc
from psutil.tests import kernel_version
from psutil.tests import process_namespace
from psutil.tests import serialrun
Expand Down Expand Up @@ -489,7 +489,8 @@ def ppid(self, ret, info):

def name(self, ret, info):
self.assertIsInstance(ret, (str, unicode))
if APPVEYOR and not ret and info['status'] == 'stopped':
if WINDOWS and not ret and is_win_secure_system_proc(info['pid']):
# https://github.com/giampaolo/psutil/issues/2338
return
# on AIX, "<exiting>" processes don't have names
if not AIX:
Expand Down Expand Up @@ -562,7 +563,8 @@ def ionice(self, ret, info):

def num_threads(self, ret, info):
self.assertIsInstance(ret, int)
if APPVEYOR and not ret and info['status'] == 'stopped':
if WINDOWS and ret == 0 and is_win_secure_system_proc(info['pid']):
# https://github.com/giampaolo/psutil/issues/2338
return
self.assertGreaterEqual(ret, 1)

Expand Down

0 comments on commit c19fa04

Please sign in to comment.