summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--HISTORY.rst4
-rw-r--r--psutil/__init__.py7
-rwxr-xr-xpsutil/tests/test_process.py28
3 files changed, 36 insertions, 3 deletions
diff --git a/HISTORY.rst b/HISTORY.rst
index 97c2c20e..003dad23 100644
--- a/HISTORY.rst
+++ b/HISTORY.rst
@@ -19,6 +19,10 @@
(e.g. directory no longer exists), in which case we returned either ``None``
or an empty string. This was consolidated and we now return ``""`` on all
platforms.
+- 2239_, [UNIX]: if process is a zombie, and we can only determine part of the
+ its truncated `Process.name()`_ (15 chars), don't fail with `ZombieProcess`_
+ when we try to guess the full name from the `Process.cmdline()`_. Just
+ return the truncated name.
**Bug fixes**
diff --git a/psutil/__init__.py b/psutil/__init__.py
index 6036cbe9..2b0b3c6b 100644
--- a/psutil/__init__.py
+++ b/psutil/__init__.py
@@ -626,7 +626,12 @@ class Process(object):
# Examples are "gnome-keyring-d" vs. "gnome-keyring-daemon".
try:
cmdline = self.cmdline()
- except AccessDenied:
+ except (AccessDenied, ZombieProcess):
+ # Just pass and return the truncated name: it's better
+ # than nothing. Note: there are actual cases where a
+ # zombie process can return a name() but not a
+ # cmdline(), see:
+ # https://github.com/giampaolo/psutil/issues/2239
pass
else:
if cmdline:
diff --git a/psutil/tests/test_process.py b/psutil/tests/test_process.py
index ec15ffda..a67baa72 100755
--- a/psutil/tests/test_process.py
+++ b/psutil/tests/test_process.py
@@ -732,7 +732,15 @@ class TestProcess(PsutilTestCase):
create_exe(testfn)
cmdline = [testfn] + (["0123456789"] * 20)
p = self.spawn_psproc(cmdline)
- self.assertEqual(p.cmdline(), cmdline)
+ if OPENBSD:
+ # XXX: for some reason the test process may turn into a
+ # zombie (don't know why).
+ try:
+ self.assertEqual(p.cmdline(), cmdline)
+ except psutil.ZombieProcess:
+ raise self.skipTest("OPENBSD: process turned into zombie")
+ else:
+ self.assertEqual(p.cmdline(), cmdline)
def test_name(self):
p = self.spawn_psproc(PYTHON_EXE)
@@ -745,7 +753,23 @@ class TestProcess(PsutilTestCase):
testfn = self.get_testfn(suffix="0123456789" * 2)
create_exe(testfn)
p = self.spawn_psproc(testfn)
- self.assertEqual(p.name(), os.path.basename(testfn))
+ if OPENBSD:
+ # XXX: for some reason the test process may turn into a
+ # zombie (don't know why). Because the name() is long, all
+ # UNIX kernels truncate it to 15 chars, so internally psutil
+ # tries to guess the full name() from the cmdline(). But the
+ # cmdline() of a zombie on OpenBSD fails (internally), so we
+ # just compare the first 15 chars. Full explanation:
+ # https://github.com/giampaolo/psutil/issues/2239
+ try:
+ self.assertEqual(p.name(), os.path.basename(testfn))
+ except AssertionError:
+ if p.status() == psutil.STATUS_ZOMBIE:
+ assert os.path.basename(testfn).startswith(p.name())
+ else:
+ raise
+ else:
+ self.assertEqual(p.name(), os.path.basename(testfn))
# XXX
@unittest.skipIf(SUNOS, "broken on SUNOS")