@@ -223,7 +223,7 @@ def get_test_subprocess(cmd=None, wait=False, **kwds):
223223 warn ("couldn't make sure test file was actually created" )
224224 else :
225225 wait_for_pid (sproc .pid )
226- _subprocesses_started .add (psutil . Process ( sproc . pid ) )
226+ _subprocesses_started .add (sproc )
227227 return sproc
228228
229229
@@ -271,28 +271,33 @@ def reap_children(search_all=False):
271271 no zombies stick around to hog resources and create problems when
272272 looking for refleaks.
273273 """
274- global _subprocesses_started
275- procs = _subprocesses_started .copy ()
276- if search_all :
277- this_process = psutil .Process ()
278- for p in this_process .children (recursive = True ):
279- procs .add (p )
280- for p in procs :
281- try :
282- p .terminate ()
283- except psutil .NoSuchProcess :
284- pass
285- gone , alive = psutil .wait_procs (procs , timeout = GLOBAL_TIMEOUT )
286- for p in alive :
287- warn ("couldn't terminate process %s" % p )
274+ subprocs = _subprocesses_started .copy ()
275+ _subprocesses_started .clear ()
276+ for subp in subprocs :
288277 try :
289- p .kill ()
290- except psutil .NoSuchProcess :
291- pass
292- _ , alive = psutil .wait_procs (alive , timeout = GLOBAL_TIMEOUT )
293- if alive :
294- warn ("couldn't not kill processes %s" % str (alive ))
295- _subprocesses_started = set (alive )
278+ subp .terminate ()
279+ except OSError as err :
280+ if err .errno != errno .ESRCH :
281+ raise
282+ subp .wait ()
283+
284+ if search_all :
285+ children = psutil .Process ().children (recursive = True )
286+ for p in children :
287+ try :
288+ p .terminate ()
289+ except psutil .NoSuchProcess :
290+ pass
291+ gone , alive = psutil .wait_procs (children , timeout = GLOBAL_TIMEOUT )
292+ for p in alive :
293+ warn ("couldn't terminate process %s" % p )
294+ try :
295+ p .kill ()
296+ except psutil .NoSuchProcess :
297+ pass
298+ _ , alive = psutil .wait_procs (alive , timeout = GLOBAL_TIMEOUT )
299+ if alive :
300+ warn ("couldn't not kill processes %s" % str (alive ))
296301
297302
298303# ===================================================================
0 commit comments