summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2020-04-28 04:15:25 +0200
committerGiampaolo Rodola <g.rodola@gmail.com>2020-04-28 04:15:25 +0200
commitefe1cdb3b7177a0e65531cf9b099d28df9bc8271 (patch)
tree69e1c6bec469e7e4f953b1af33efa13f41c3aadf
parent92e150ef5e309ff93378ae4538065f1ca5c00a17 (diff)
downloadpsutil-efe1cdb3b7177a0e65531cf9b099d28df9bc8271.tar.gz
create_zombie_proc() make it return parent so that we can kill zombie
-rw-r--r--psutil/tests/__init__.py13
-rwxr-xr-xpsutil/tests/test_osx.py8
-rwxr-xr-xpsutil/tests/test_process.py16
-rwxr-xr-xpsutil/tests/test_testutils.py9
4 files changed, 27 insertions, 19 deletions
diff --git a/psutil/tests/__init__.py b/psutil/tests/__init__.py
index b31b845d..fea5a5d0 100644
--- a/psutil/tests/__init__.py
+++ b/psutil/tests/__init__.py
@@ -362,7 +362,10 @@ def create_proc_children_pair():
def create_zombie_proc():
- """Create a zombie process and return its PID."""
+ """Create a zombie process and return a (parent, zombie) process tuple.
+ In order to kill the zombie parent must be terminate()d first, then
+ zombie must be wait()ed on.
+ """
assert psutil.POSIX
unix_file = get_testfn()
src = textwrap.dedent("""\
@@ -384,15 +387,15 @@ def create_zombie_proc():
sock = bind_unix_socket(unix_file)
with contextlib.closing(sock):
sock.settimeout(GLOBAL_TIMEOUT)
- pyrun(src)
+ parent = pyrun(src)
conn, _ = sock.accept()
try:
select.select([conn.fileno()], [], [], GLOBAL_TIMEOUT)
zpid = int(conn.recv(1024))
_pids_started.add(zpid)
- zproc = psutil.Process(zpid)
- call_until(lambda: zproc.status(), "ret == psutil.STATUS_ZOMBIE")
- return zpid
+ zombie = psutil.Process(zpid)
+ call_until(lambda: zombie.status(), "ret == psutil.STATUS_ZOMBIE")
+ return (parent, zombie)
finally:
conn.close()
diff --git a/psutil/tests/test_osx.py b/psutil/tests/test_osx.py
index f7862843..bcff0ba7 100755
--- a/psutil/tests/test_osx.py
+++ b/psutil/tests/test_osx.py
@@ -19,6 +19,7 @@ from psutil.tests import SYSMEM_TOLERANCE
from psutil.tests import reap_children
from psutil.tests import retry_on_failure
from psutil.tests import sh
+from psutil.tests import terminate
from psutil.tests import unittest
@@ -105,12 +106,13 @@ class TestZombieProcessAPIs(unittest.TestCase):
@classmethod
def setUpClass(cls):
- zpid = create_zombie_proc()
- cls.p = psutil.Process(zpid)
+ cls.parent, cls.zombie = create_zombie_proc()
+ cls.p = psutil.Process(cls.zombie.pid)
@classmethod
def tearDownClass(cls):
- reap_children(recursive=True)
+ terminate(cls.parent)
+ terminate(cls.zombie)
def test_pidtask_info(self):
self.assertEqual(self.p.status(), psutil.STATUS_ZOMBIE)
diff --git a/psutil/tests/test_process.py b/psutil/tests/test_process.py
index ac841eee..ef8d245f 100755
--- a/psutil/tests/test_process.py
+++ b/psutil/tests/test_process.py
@@ -60,6 +60,7 @@ from psutil.tests import retry_on_failure
from psutil.tests import sh
from psutil.tests import skip_on_access_denied
from psutil.tests import skip_on_not_implemented
+from psutil.tests import terminate
from psutil.tests import ThreadTask
from psutil.tests import TRAVIS
from psutil.tests import unittest
@@ -1337,10 +1338,11 @@ class TestProcess(unittest.TestCase):
except (psutil.ZombieProcess, psutil.AccessDenied):
pass
- zpid = create_zombie_proc()
- self.addCleanup(reap_children)
+ parent, zombie = create_zombie_proc()
+ self.addCleanup(terminate, zombie)
+ self.addCleanup(terminate, parent) # executed first
# A zombie process should always be instantiable
- zproc = psutil.Process(zpid)
+ zproc = psutil.Process(zombie.pid)
# ...and at least its status always be querable
self.assertEqual(zproc.status(), psutil.STATUS_ZOMBIE)
# ...and it should be considered 'running'
@@ -1392,15 +1394,15 @@ class TestProcess(unittest.TestCase):
# rid of a zombie is to kill its parent.
# self.assertEqual(zpid.ppid(), os.getpid())
# ...and all other APIs should be able to deal with it
- self.assertTrue(psutil.pid_exists(zpid))
+ self.assertTrue(psutil.pid_exists(zproc.pid))
if not TRAVIS and MACOS:
# For some reason this started failing all of the sudden.
# Maybe they upgraded MACOS 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()])
+ self.assertIn(zproc.pid, psutil.pids())
+ self.assertIn(zproc.pid, [x.pid for x in psutil.process_iter()])
psutil._pmap = {}
- self.assertIn(zpid, [x.pid for x in psutil.process_iter()])
+ self.assertIn(zproc.pid, [x.pid for x in psutil.process_iter()])
@unittest.skipIf(not POSIX, 'POSIX only')
def test_zombie_process_is_running_w_exc(self):
diff --git a/psutil/tests/test_testutils.py b/psutil/tests/test_testutils.py
index 45b2557f..fd32e0b7 100755
--- a/psutil/tests/test_testutils.py
+++ b/psutil/tests/test_testutils.py
@@ -45,6 +45,7 @@ from psutil.tests import safe_mkdir
from psutil.tests import safe_rmpath
from psutil.tests import serialrun
from psutil.tests import tcp_socketpair
+from psutil.tests import terminate
from psutil.tests import TestMemoryLeak
from psutil.tests import unittest
from psutil.tests import unix_socketpair
@@ -240,10 +241,10 @@ class TestProcessUtils(unittest.TestCase):
@unittest.skipIf(not POSIX, "POSIX only")
def test_create_zombie_proc(self):
- zpid = create_zombie_proc()
- self.addCleanup(reap_children, recursive=True)
- p = psutil.Process(zpid)
- self.assertEqual(p.status(), psutil.STATUS_ZOMBIE)
+ parent, zombie = create_zombie_proc()
+ self.addCleanup(terminate, zombie)
+ self.addCleanup(terminate, parent) # executed first
+ self.assertEqual(zombie.status(), psutil.STATUS_ZOMBIE)
class TestNetUtils(unittest.TestCase):