Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 1 | From 325898f3f2951bbde07da47888175c427b11ddc3 Mon Sep 17 00:00:00 2001 |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 2 | From: Hongxu Jia <hongxu.jia@windriver.com> |
| 3 | Date: Mon, 8 May 2017 16:18:02 +0800 |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 4 | Subject: [PATCH 03/11] support infinit timeout |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 5 | |
| 6 | Upstream-Status: Pending |
| 7 | |
| 8 | Signed-off-by: Hongxu Jia <hongxu.jia@windriver.com> |
| 9 | --- |
| 10 | blivet/util.py | 12 ++++++++---- |
| 11 | 1 file changed, 8 insertions(+), 4 deletions(-) |
| 12 | |
| 13 | diff --git a/blivet/util.py b/blivet/util.py |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 14 | index 05a253c..d6804be 100644 |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 15 | --- a/blivet/util.py |
| 16 | +++ b/blivet/util.py |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 17 | @@ -157,6 +157,7 @@ class Path(str): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 18 | def __hash__(self): |
| 19 | return self._path.__hash__() |
| 20 | |
| 21 | +# timeout = -1 means infinite timeout, always wait. |
| 22 | def timeout_command(argv, timeout, *args, **kwargs): |
| 23 | """call shell-command and either return its output or kill it |
| 24 | if it doesn't normally exit within timeout seconds and return None""" |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 25 | @@ -168,7 +169,7 @@ def timeout_command(argv, timeout, *args, **kwargs): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 26 | while proc.poll() is None: |
| 27 | time.sleep(0.1) |
| 28 | now = datetime.datetime.now() |
| 29 | - if (now - start).seconds> timeout: |
| 30 | + if timeout != -1 and (now - start).seconds> timeout: |
| 31 | os.kill(proc.pid, signal.SIGKILL) |
| 32 | os.waitpid(-1, os.WNOHANG) |
| 33 | program_log.debug("%d seconds timeout" % timeout) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 34 | @@ -182,7 +183,7 @@ def timeout_command(argv, timeout, *args, **kwargs): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 35 | program_log.debug("Return code: %d", proc.returncode) |
| 36 | return (proc.returncode, proc.stdout.read()) |
| 37 | |
| 38 | -def _run_program(argv, root='/', stdin=None, env_prune=None, stderr_to_stdout=False, binary_output=False): |
| 39 | +def _run_program(argv, root='/', stdin=None, env_prune=None, stderr_to_stdout=False, binary_output=False, timeout=10): |
| 40 | if env_prune is None: |
| 41 | env_prune = [] |
| 42 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 43 | @@ -191,7 +192,10 @@ def _run_program(argv, root='/', stdin=None, env_prune=None, stderr_to_stdout=Fa |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 44 | os.chroot(root) |
| 45 | |
| 46 | with program_log_lock: # pylint: disable=not-context-manager |
| 47 | - program_log.info("Running... %s", " ".join(argv)) |
| 48 | + if timeout != -1: |
| 49 | + program_log.info("Running... %s", " ".join(argv)) |
| 50 | + else: |
| 51 | + program_log.info("Running... %s ...infinite timeout", " ".join(argv)) |
| 52 | |
| 53 | env = os.environ.copy() |
| 54 | env.update({"LC_ALL": "C", |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 55 | @@ -204,7 +208,7 @@ def _run_program(argv, root='/', stdin=None, env_prune=None, stderr_to_stdout=Fa |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 56 | else: |
| 57 | stderr_dir = subprocess.PIPE |
| 58 | |
| 59 | - res, out = timeout_command(argv, 10, |
| 60 | + res, out = timeout_command(argv, timeout, |
| 61 | stdin=stdin, |
| 62 | stdout=subprocess.PIPE, |
| 63 | stderr=stderr_dir, |
| 64 | -- |
| 65 | 2.7.4 |
| 66 | |