From cd6197caa33e9356e97518eaf54a5cfa592670da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matt=C3=A9o=20Delabre?= Date: Wed, 27 Jan 2021 12:42:01 +0100 Subject: [PATCH] Fight against zombies --- runall.py | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/runall.py b/runall.py index 533cb7d..3348c54 100644 --- a/runall.py +++ b/runall.py @@ -1,26 +1,31 @@ from itertools import product -import subprocess +import signal +from subprocess import DEVNULL, PIPE, Popen, TimeoutExpired chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ ' +# Prevent zombie processes +signal.signal(signal.SIGCHLD, signal.SIG_IGN) + def check_pair(script, instr, outstr): + process = Popen( + ["/usr/bin/env", "bash", "-c", "--", script], + stdin=PIPE, + stdout=PIPE, + stderr=DEVNULL, + ) + try: - result = subprocess.run( - [ - "/usr/bin/env", "bash", "-c", - ";\n".join(( - # Cleanup child processes on exit - "trap 'kill -9 $(jobs -p)' SIGINT SIGTERM EXIT", - script, - )) - ], - timeout=5, - input=instr.encode(), - capture_output=True, - ) - return result.returncode == 0 and result.stdout == outstr.encode() - except subprocess.TimeoutExpired: + stdout, stderr = process.communicate(instr.encode(), timeout=5) + return process.returncode == 0 and stdout == outstr.encode() + except TimeoutExpired: + try: + process.kill() + stdout, stderr = process.communicate() + except ProcessLookupError: + pass + return False