test-runner: move process cleanup into kill()

This moves all the de-init code into kill(), which fixes a few
reference issues causing processes to hang around longer than
desired. If the process terminates on its own and/or the last
reference is lost __del__ will kill the process and clean up.
This commit is contained in:
James Prestwood 2021-02-25 14:01:02 -08:00 committed by Denis Kenzior
parent 48d27c982d
commit d8abc88e4c
1 changed files with 32 additions and 16 deletions

View File

@ -161,6 +161,7 @@ class Process:
'''
def __init__(self, args, wait=False, env=None, ctx=None, check=False,
outfile=None, namespace=None, need_out=False, cleanup=None):
self.killed = False
self.args = args
self.wait = wait
self.name = args[0]
@ -238,6 +239,7 @@ class Process:
return
self.pid.wait()
self.killed = True
self.stdout.seek(self.io_position)
self.out = self.stdout.read()
@ -282,34 +284,49 @@ class Process:
def __del__(self):
print("Del process %s" % self.args)
self.stdout.close()
if self.io_watch:
GLib.source_remove(self.io_watch)
if not self.killed:
self.kill()
for f in self.write_fds:
f.close()
try:
os.remove(self.output_name)
except:
pass
if self.cleanup:
self.cleanup()
os.remove(self.output_name)
def kill(self, force=False):
print("Killing process %s" % self.args)
if self.killed:
return
if force:
os.kill(self.pid.pid, signal.SIGKILL)
else:
self.pid.kill()
else:
self.pid.terminate()
self.pid.wait(timeout=15)
self.pid = None
if self.ctx and self in self.ctx.processes:
self.ctx.processes.remove(self)
self.ctx = None
for f in self.write_fds:
f.close()
self.write_fds = []
if self.io_watch:
GLib.source_remove(self.io_watch)
self.io_watch = None
if self.cleanup:
self.cleanup()
self.cleanup = None
self.stdout.close()
self.killed = True
def wait_for_socket(self, socket, wait):
waited = 0
while not os.path.exists(socket):
@ -607,7 +624,6 @@ class Namespace:
def stop_process(self, p, force=False):
p.kill(force)
self.processes.remove(p)
def is_process_running(self, process):
for p in self.processes: