From: Andrei Pavel Date: Wed, 25 Jan 2023 22:12:05 +0000 (+0200) Subject: [#2696] hammer.py: check Popen.poll() at the end X-Git-Tag: Kea-2.3.5~78 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b4498e10845248aa95fe6674c8ac8692d2b233a7;p=thirdparty%2Fkea.git [#2696] hammer.py: check Popen.poll() at the end There was a wrong assumption that the first call to Popen.poll() is always None. This is not the case when the process is fast enough to finish before it gets polled, in which case it is not None, but it is the return code instead. This caused some executes to have empty output. --- diff --git a/hammer.py b/hammer.py index 01eaf489be..0dae1cf647 100755 --- a/hammer.py +++ b/hammer.py @@ -361,9 +361,8 @@ def execute(cmd, timeout=60, cwd=None, env=None, raise_error=True, dry_run=False if capture: output = '' t0 = time.time() - t1 = time.time() # repeat until process is running or timeout not occurred - while p.poll() is None and (timeout is None or t1 - t0 < timeout): + while True: line = p.stdout.readline() if line: line_decoded = line.decode(encoding='ascii', errors='ignore').rstrip() + '\r' @@ -374,6 +373,8 @@ def execute(cmd, timeout=60, cwd=None, env=None, raise_error=True, dry_run=False if log_file_path: log_file.write(line) t1 = time.time() + if p.poll() is not None or (timeout is not None and timeout < t1 - t0): + break # If no exitcode yet, ie. process is still running then it means that timeout occurred. # In such case terminate the process and raise an exception.