Raise exception if command timeout is triggered (#43078)

* Raise exception if command timeout is triggered

Fixes #43076

If persistent connection timeout is triggered, riase
exception which will be send over socket to module code
instead of silently shutting down the socket.

* Fix CI failure

* Fix review comment

* Fix CI failure

* Fix review comment

* Fix review comment
pull/43533/head
Ganesh Nalawade 6 years ago committed by GitHub
parent b771913c0d
commit 3f3101dfe5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -16,6 +16,7 @@ import os
import signal import signal
import socket import socket
import sys import sys
import time
import traceback import traceback
import errno import errno
import json import json
@ -141,22 +142,28 @@ class ConnectionProcess(object):
self.exception = traceback.format_exc() self.exception = traceback.format_exc()
finally: finally:
# when done, close the connection properly and cleanup # allow time for any exception msg send over socket to receive at other end before shutting down
# the socket file so it can be recreated time.sleep(0.1)
# when done, close the connection properly and cleanup the socket file so it can be recreated
self.shutdown() self.shutdown()
def connect_timeout(self, signum, frame): def connect_timeout(self, signum, frame):
display.display('persistent connection idle timeout triggered, timeout value is %s secs' msg = 'persistent connection idle timeout triggered, timeout value is %s secs.\nSee the timeout setting options in the Network Debug and ' \
% self.connection.get_option('persistent_connect_timeout'), log_only=True) 'Troubleshooting Guide.' % self.connection.get_option('persistent_connect_timeout')
self.shutdown() display.display(msg, log_only=True)
raise Exception(msg)
def command_timeout(self, signum, frame): def command_timeout(self, signum, frame):
display.display('command timeout triggered, timeout value is %s secs' % self.connection.get_option('persistent_command_timeout'), log_only=True) msg = 'command timeout triggered, timeout value is %s secs.\nSee the timeout setting options in the Network Debug and Troubleshooting Guide.'\
self.shutdown() % self.connection.get_option('persistent_command_timeout')
display.display(msg, log_only=True)
raise Exception(msg)
def handler(self, signum, frame): def handler(self, signum, frame):
display.display('signal handler called with signal %s' % signum, log_only=True) msg = 'signal handler called with signal %s.' % signum
self.shutdown() display.display(msg, log_only=True)
raise Exception(msg)
def shutdown(self): def shutdown(self):
""" Shuts down the local domain socket """ Shuts down the local domain socket

@ -111,10 +111,9 @@ class Connection(object):
req = request_builder(name, *args, **kwargs) req = request_builder(name, *args, **kwargs)
reqid = req['id'] reqid = req['id']
troubleshoot = 'https://docs.ansible.com/ansible/latest/network/user_guide/network_debug_troubleshooting.html#category-socket-path-issue'
if not os.path.exists(self.socket_path): if not os.path.exists(self.socket_path):
raise ConnectionError('socket_path does not exist or cannot be found. Please check %s' % troubleshoot) raise ConnectionError('socket_path does not exist or cannot be found.'
'\nSee the socket_path issue catergory in Network Debug and Troubleshooting Guide')
try: try:
data = json.dumps(req) data = json.dumps(req)
@ -122,8 +121,8 @@ class Connection(object):
response = json.loads(out) response = json.loads(out)
except socket.error as e: except socket.error as e:
raise ConnectionError('unable to connect to socket. Please check %s' % troubleshoot, err=to_text(e, errors='surrogate_then_replace'), raise ConnectionError('unable to connect to socket. See the socket_path issue catergory in Network Debug and Troubleshooting Guide',
exception=traceback.format_exc()) err=to_text(e, errors='surrogate_then_replace'), exception=traceback.format_exc())
if response['id'] != reqid: if response['id'] != reqid:
raise ConnectionError('invalid json-rpc id received') raise ConnectionError('invalid json-rpc id received')

Loading…
Cancel
Save