From 491bcedd9cfe46a8525d4c17967b3403deb90f22 Mon Sep 17 00:00:00 2001 From: Millian Poquet Date: Thu, 22 Feb 2018 16:10:20 +0100 Subject: [PATCH] [tesh] psychorigid try/except to avoid vexing bsd --- tools/tesh/tesh.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/tools/tesh/tesh.py b/tools/tesh/tesh.py index 6a2df5523c..e389c25f67 100755 --- a/tools/tesh/tesh.py +++ b/tools/tesh/tesh.py @@ -102,16 +102,20 @@ except NameError: # # +DO_NOT_CLEAN_SUBPROCESSES = -1 # Global variable. Stores which process group should be killed (or -1 if none) -pgtokill = -1 +pgtokill = DO_NOT_CLEAN_SUBPROCESSES def kill_process_group(pgid): # print("Kill process group {}".format(pgid)) - os.killpg(pgid, signal.SIGTERM) + try: + os.killpg(pgid, signal.SIGTERM) + except: # Ugly and psychorigid. Please improve python doc so we know what exceptions killpg can throw. + pass def signal_handler(signal, frame): print("Caught signal {}".format(SIGNALS_TO_NAMES_DICT[signal])) - if pgtokill != -1: + if pgtokill != DO_NOT_CLEAN_SUBPROCESSES: kill_process_group(pgtokill) tesh_exit(5) @@ -311,7 +315,11 @@ class Cmd(object): try: proc = subprocess.Popen(args, bufsize=1, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True, preexec_fn=os.setsid) - pgtokill = os.getpgid(proc.pid) + try: + pgtokill = os.getpgid(proc.pid) + except: # Ugly and psychorigid. Please improve python doc so we know what exceptions getpgid can throw. + # os.getpgid failed. OK, no cleanup. + pass except FileNotFoundError: print("["+FileReader().filename+":"+str(self.linenumber)+"] Cannot start '"+args[0]+"': File not found") tesh_exit(3) @@ -323,7 +331,7 @@ class Cmd(object): cmdName = FileReader().filename+":"+str(self.linenumber) try: (stdout_data, stderr_data) = proc.communicate("\n".join(self.input_pipe), self.timeout) - pgtokill = -1 + pgtokill = DO_NOT_CLEAN_SUBPROCESSES except subprocess.TimeoutExpired: print("Test suite `"+FileReader().filename+"': NOK (<"+cmdName+"> timeout after "+str(self.timeout)+" sec)") kill_process_group(pgtokill) -- 2.20.1