From 86dac6d23536d946d52b7c0f2a798c2fcd9acf93 Mon Sep 17 00:00:00 2001 From: Nir Izraeli Date: Fri, 6 Aug 2021 11:21:20 +0300 Subject: [PATCH] Add a subprocess.Popen timeout when possible The linux timeout command seems to fail terminating processes occasionally, so when possible without breaking the interface I've added a subprocess.Popen timeout (to be double the linux timeout) as a backup timeout. This is a bit hackish but has been useful for me. --- afl-cov | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/afl-cov b/afl-cov index 3b07bf1..8708aa1 100755 --- a/afl-cov +++ b/afl-cov @@ -856,7 +856,7 @@ def run_cmd( collect: int, aflrun: bool, fn: str, - timeout: Optional[int] = None, + timeout: Optional[str] = None, ) -> Tuple[int, List[bytes]]: out = [] @@ -877,9 +877,22 @@ def run_cmd( if timeout: cmd = "timeout -s KILL %s %s" % (timeout, cmd) - exit_code = subprocess.call( - cmd, stdin=None, stdout=fh, stderr=subprocess.STDOUT, shell=True - ) + # If timeout is in seconds, add an extra layer of timeout + # the timeout linux command failed me on several occasions + sp_timeout = int(timeout) * 2 if timeout and timeout.isdigit() else None + + try: + exit_code = subprocess.call( + cmd, stdin=None, stdout=fh, stderr=subprocess.STDOUT, shell=True, + timeout=sp_timeout + ) + except subprocess.TimeoutExpired: + if cargs.verbose: + if log_file: + logr(b" CMD: %s timedout: %s" % (cmd.encode(), str(ex)), log_file, cargs) + else: + print(" CMD: %s timedout: %s" % (cmd, str(ex))) + exit_code = -1 fh.close()