From d8abc88e4c4c62928c620d1a40808c4a20b06732 Mon Sep 17 00:00:00 2001 From: James Prestwood Date: Thu, 25 Feb 2021 14:01:02 -0800 Subject: [PATCH] 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. --- tools/test-runner | 48 +++++++++++++++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/tools/test-runner b/tools/test-runner index 80bdae30..ae02578e 100755 --- a/tools/test-runner +++ b/tools/test-runner @@ -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: