Commit 4a202f4a authored by Pere Cortes's avatar Pere Cortes Committed by Sebastien Robin

Security Thread added to kill blocked process

parent e2c62ce3
This diff is collapsed.
......@@ -30,6 +30,9 @@ import subprocess
import threading
import signal
import sys
import time
MAX_TIMEOUT = 5
class SubprocessError(EnvironmentError):
def __init__(self, status_dict):
......@@ -39,6 +42,12 @@ class SubprocessError(EnvironmentError):
def __str__(self):
return 'Error %i' % self.status_code
class TimeoutError(EnvironmentError):
def __init__(self):
pass
def __str__(self):
return 'Timeout expired. Process killed'
class CancellationError(EnvironmentError):
pass
......@@ -98,8 +107,16 @@ class ProcessManager(object):
self.process_pid_set = set()
signal.signal(signal.SIGTERM, self.sigterm_handler)
self.under_cancellation = False
self.p = None
self.result = None
def spawn(self, *args, **kw):
def timeoutExpired(p):
time.sleep(MAX_TIMEOUT)
if p.poll() is None:
p.terminate()
raise TimeoutError
if self.under_cancellation:
raise CancellationError("Test Result was cancelled")
get_output = kw.pop('get_output', True)
......@@ -120,6 +137,8 @@ class ProcessManager(object):
p = subprocess.Popen(args, stdin=self.stdin, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, env=env, **subprocess_kw)
self.process_pid_set.add(p.pid)
thread = threading.Thread(target=timeoutExpired, args=(p,))
thread.start()
stdout, stderr = subprocess_capture(p, self.log, log_prefix,
get_output=get_output)
result = dict(status_code=p.returncode, command=command,
......@@ -127,7 +146,7 @@ class ProcessManager(object):
self.process_pid_set.discard(p.pid)
if self.under_cancellation:
raise CancellationError("Test Result was cancelled")
if raise_error_if_fail and p.returncode:
if raise_error_if_fail and p.returncode != -15 and p.returncode:
raise SubprocessError(result)
return result
......
......@@ -257,7 +257,7 @@ branch = %(branch)s
We will build slapos software needed by the testnode itself,
like the building of selenium-runner by default
"""
self._prepareSlapOS(self.config['slapos_directory'],
return self._prepareSlapOS(self.config['slapos_directory'],
test_node_slapos, create_partition=0,
software_path_list=self.config.get("software_list"))
......
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