diff --git a/HISTORY.rst b/HISTORY.rst index 8bc3f7847..7b8f7f41f 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -22,6 +22,7 @@ misbehaving processes which overwrite /proc/pid/cmdline and use spaces instead of null bytes as args separator. - 1181_: [OSX] Process.memory_maps() may raise ENOENT. +- 1187_: [OSX] pids() does not return PID 0 on recent OSX versions. 5.4.1 ===== diff --git a/psutil/_psosx.py b/psutil/_psosx.py index 9fa7716df..4c97af71e 100644 --- a/psutil/_psosx.py +++ b/psutil/_psosx.py @@ -301,7 +301,23 @@ def users(): # ===================================================================== -pids = cext.pids +def pids(): + ls = cext.pids() + if 0 not in ls: + # On certain OSX versions pids() C doesn't return PID 0 but + # "ps" does and the process is querable via sysctl(): + # https://travis-ci.org/giampaolo/psutil/jobs/309619941 + try: + Process(0).create_time() + except NoSuchProcess: + return False + except AccessDenied: + ls.append(0) + else: + ls.append(0) + return ls + + pid_exists = _psposix.pid_exists diff --git a/psutil/tests/test_process.py b/psutil/tests/test_process.py index 1e01aea55..3987943a5 100755 --- a/psutil/tests/test_process.py +++ b/psutil/tests/test_process.py @@ -1314,10 +1314,14 @@ def succeed_or_zombie_p_exc(fun, *args, **kwargs): # self.assertEqual(zpid.ppid(), os.getpid()) # ...and all other APIs should be able to deal with it self.assertTrue(psutil.pid_exists(zpid)) - self.assertIn(zpid, psutil.pids()) - self.assertIn(zpid, [x.pid for x in psutil.process_iter()]) - psutil._pmap = {} - self.assertIn(zpid, [x.pid for x in psutil.process_iter()]) + if not TRAVIS and OSX: + # For some reason this started failing all of the sudden. + # Maybe they upgraded OSX version? + # https://travis-ci.org/giampaolo/psutil/jobs/310896404 + self.assertIn(zpid, psutil.pids()) + self.assertIn(zpid, [x.pid for x in psutil.process_iter()]) + psutil._pmap = {} + self.assertIn(zpid, [x.pid for x in psutil.process_iter()]) @unittest.skipIf(not POSIX, 'POSIX only') def test_zombie_process_is_running_w_exc(self):