Skip to content

Commit

Permalink
reap_children(): keep track of the subprocess references and wait() f…
Browse files Browse the repository at this point in the history
…or them in order to avoid ResourceWarnings on python 3.6
  • Loading branch information
giampaolo committed Jul 10, 2016
1 parent f4cf052 commit d539e58
Showing 1 changed file with 27 additions and 22 deletions.
49 changes: 27 additions & 22 deletions psutil/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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))


# ===================================================================
Expand Down

0 comments on commit d539e58

Please sign in to comment.