diff --git a/psutil/tests/__init__.py b/psutil/tests/__init__.py index 852a3fc03..f28953b37 100644 --- a/psutil/tests/__init__.py +++ b/psutil/tests/__init__.py @@ -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 @@ -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(): diff --git a/psutil/tests/test_contracts.py b/psutil/tests/test_contracts.py index dc519005f..0bf03b53d 100755 --- a/psutil/tests/test_contracts.py +++ b/psutil/tests/test_contracts.py @@ -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 @@ -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 @@ -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, "" processes don't have names if not AIX: @@ -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)