From 2c6cc2620fada3d71f82321667d53e8cf9aabc76 Mon Sep 17 00:00:00 2001 From: Mihail Stoyanov Date: Mon, 27 Jun 2016 12:37:12 +0100 Subject: [PATCH] Add support for subprocess execution timeout for when calling backend compilers --- tools/utils.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/tools/utils.py b/tools/utils.py index 55752b8750d..e255c68e2eb 100644 --- a/tools/utils.py +++ b/tools/utils.py @@ -17,6 +17,7 @@ import sys import inspect import os +from time import sleep from os import listdir, remove, makedirs from shutil import copyfile from os.path import isdir, join, exists, split, relpath, splitext @@ -33,10 +34,18 @@ def cmd(l, check=True, verbose=False, shell=False, cwd=None): raise Exception('ERROR %d: "%s"' % (rc, text)) -def run_cmd(command, wd=None, redirect=False): +def run_cmd(command, wd=None, redirect=False, timeout=15): assert is_cmd_valid(command[0]) try: p = Popen(command, stdout=PIPE, stderr=STDOUT if redirect else PIPE, cwd=wd) + if timeout: + wait = 0.0 + while p.poll() is None: + wait += 0.01 + if wait > timeout: + p.kill() + raise OSError(1, "Subprocess timed out after %0.2f seconds" % timeout) + sleep(0.01) _stdout, _stderr = p.communicate() except OSError as e: print "[OS ERROR] Command: "+(' '.join(command))