improve generic timeout handling (#14913)
This commit is contained in:
parent
6ce1c13e66
commit
ae988f515f
|
@ -29,49 +29,51 @@ from mandayejs.applications import get_app_settings
|
|||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def run(send_end, data, script):
|
||||
phantom = subprocess.Popen([
|
||||
settings.PHANTOM_JS_BINARY,
|
||||
'--ignore-ssl-errors=yes', '--ssl-protocol=any',
|
||||
os.path.join(settings.BASE_DIR, 'mandayejs', script)],
|
||||
close_fds=True,
|
||||
stdin=subprocess.PIPE,
|
||||
stdout=subprocess.PIPE
|
||||
)
|
||||
stdout, stderr = phantom.communicate(json.dumps(data))
|
||||
|
||||
try:
|
||||
output = re.search('<mandayejs>(.*?)</mandayejs>', stdout, re.DOTALL)
|
||||
if not output:
|
||||
raise ValueError
|
||||
stdout = output.group(1)
|
||||
result = json.loads(stdout)
|
||||
except (ValueError,):
|
||||
result = {"result": "json_error"}
|
||||
logger.error("invalid json: %s" % stdout)
|
||||
|
||||
if result.get('stderr'):
|
||||
logger.warning(result['stderr'])
|
||||
if result.get('error'):
|
||||
logger.error('Error occured: %s' % result.get('reason'))
|
||||
|
||||
send_end.send(result)
|
||||
send_end.close()
|
||||
|
||||
|
||||
def exec_phantom(data, script='do_login.js'):
|
||||
|
||||
def run(send_end):
|
||||
phantom = subprocess.Popen([
|
||||
settings.PHANTOM_JS_BINARY,
|
||||
'--ignore-ssl-errors=yes', '--ssl-protocol=any',
|
||||
os.path.join(settings.BASE_DIR, 'mandayejs', script)],
|
||||
close_fds=True,
|
||||
stdin=subprocess.PIPE,
|
||||
stdout=subprocess.PIPE
|
||||
)
|
||||
stdout, stderr = phantom.communicate(json.dumps(data))
|
||||
|
||||
try:
|
||||
output = re.search('<mandayejs>(.*?)</mandayejs>', stdout, re.DOTALL)
|
||||
if not output:
|
||||
raise ValueError
|
||||
stdout = output.group(1)
|
||||
result = json.loads(stdout)
|
||||
except (ValueError,):
|
||||
result = {"result": "json_error"}
|
||||
logger.error("invalid json: %s" % stdout)
|
||||
|
||||
if result.get('stderr'):
|
||||
logger.warning(result['stderr'])
|
||||
if result.get('error'):
|
||||
logger.error(result.get('reason'))
|
||||
|
||||
send_end.send(result)
|
||||
|
||||
recv_end, send_end = multiprocessing.Pipe(False)
|
||||
process = multiprocessing.Process(target=run, args=(send_end,))
|
||||
process = multiprocessing.Process(target=run, args=(send_end, data, script))
|
||||
process.start()
|
||||
process.join(settings.PHANTOM_JS_TIMEOUT)
|
||||
|
||||
if process.is_alive():
|
||||
if recv_end.poll(settings.PHANTOM_JS_TIMEOUT):
|
||||
result = recv_end.recv()
|
||||
recv_end.close()
|
||||
else:
|
||||
process.terminate()
|
||||
send_end.close()
|
||||
# Don't log locators, they may contain credentials (passwords)
|
||||
context = {k: v for k, v in data.items() if k != 'locators'}
|
||||
logger.error("PhantomJS process timeout, context: %s" % context)
|
||||
result = {'result': 'timeout'}
|
||||
else:
|
||||
result = recv_end.recv()
|
||||
|
||||
return result
|
||||
|
||||
|
|
Reference in New Issue