Commit a2d3f0fa authored by Denis Bilenko's avatar Denis Bilenko

greentest/util.py: better timeouts

parent af14625e
...@@ -79,8 +79,11 @@ def _kill(popen): ...@@ -79,8 +79,11 @@ def _kill(popen):
def kill(popen): def kill(popen):
if popen.timer is not None:
popen.timer.cancel()
if popen.poll() is not None: if popen.poll() is not None:
return return
popen.was_killed = True
try: try:
if getattr(popen, 'setpgrp_enabled', None): if getattr(popen, 'setpgrp_enabled', None):
killpg(popen.pid) killpg(popen.pid)
...@@ -137,10 +140,13 @@ def start(command, **kwargs): ...@@ -137,10 +140,13 @@ def start(command, **kwargs):
popen = Popen(command, preexec_fn=preexec_fn, env=env, **kwargs) popen = Popen(command, preexec_fn=preexec_fn, env=env, **kwargs)
popen.name = name popen.name = name
popen.setpgrp_enabled = preexec_fn is not None popen.setpgrp_enabled = preexec_fn is not None
popen.was_killed = False
popen.timer = None
if timeout is not None: if timeout is not None:
t = threading.Timer(timeout, kill, args=(popen, )) t = threading.Timer(timeout, kill, args=(popen, ))
t.setDaemon(True) t.setDaemon(True)
t.start() t.start()
popen.timer = t
return popen return popen
...@@ -177,7 +183,7 @@ def run(command, **kwargs): ...@@ -177,7 +183,7 @@ def run(command, **kwargs):
time_start = time.time() time_start = time.time()
out, err = popen.communicate() out, err = popen.communicate()
took = time.time() - time_start took = time.time() - time_start
if popen.poll() is None: if popen.was_killed or popen.poll() is None:
result = 'TIMEOUT' result = 'TIMEOUT'
else: else:
result = popen.poll() result = popen.poll()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment