From d539e581cfb0b222aa16a7999a6fbd20c9fd3633 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Sun, 10 Jul 2016 19:53:14 +0200 Subject: [PATCH] reap_children(): keep track of the subprocess references and wait() for them in order to avoid ResourceWarnings on python 3.6 --- psutil/tests/__init__.py | 49 ++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/psutil/tests/__init__.py b/psutil/tests/__init__.py index 1e2c52e16..60f27c590 100644 --- a/psutil/tests/__init__.py +++ b/psutil/tests/__init__.py @@ -223,7 +223,7 @@ def get_test_subprocess(cmd=None, wait=False, **kwds): warn("couldn't make sure test file was actually created") else: wait_for_pid(sproc.pid) - _subprocesses_started.add(psutil.Process(sproc.pid)) + _subprocesses_started.add(sproc) return sproc @@ -271,28 +271,33 @@ def reap_children(search_all=False): no zombies stick around to hog resources and create problems when looking for refleaks. """ - global _subprocesses_started - procs = _subprocesses_started.copy() - if search_all: - this_process = psutil.Process() - for p in this_process.children(recursive=True): - procs.add(p) - for p in procs: - try: - p.terminate() - except psutil.NoSuchProcess: - pass - gone, alive = psutil.wait_procs(procs, timeout=GLOBAL_TIMEOUT) - for p in alive: - warn("couldn't terminate process %s" % p) + subprocs = _subprocesses_started.copy() + _subprocesses_started.clear() + for subp in subprocs: try: - p.kill() - except psutil.NoSuchProcess: - pass - _, alive = psutil.wait_procs(alive, timeout=GLOBAL_TIMEOUT) - if alive: - warn("couldn't not kill processes %s" % str(alive)) - _subprocesses_started = set(alive) + subp.terminate() + except OSError as err: + if err.errno != errno.ESRCH: + raise + subp.wait() + + if search_all: + children = psutil.Process().children(recursive=True) + for p in children: + try: + p.terminate() + except psutil.NoSuchProcess: + pass + gone, alive = psutil.wait_procs(children, timeout=GLOBAL_TIMEOUT) + for p in alive: + warn("couldn't terminate process %s" % p) + try: + p.kill() + except psutil.NoSuchProcess: + pass + _, alive = psutil.wait_procs(alive, timeout=GLOBAL_TIMEOUT) + if alive: + warn("couldn't not kill processes %s" % str(alive)) # ===================================================================