autotest: fix possible hlrauc race condition

The AuthCenter will now wait for the RX thread to start before
continuing with the test.

Also removed the non blocking option and fixed the loop to
handle a blocking recvfrom call.
This commit is contained in:
James Prestwood 2018-01-18 10:49:37 -08:00 committed by Denis Kenzior
parent ca183343e2
commit 6a9de526a8
1 changed files with 10 additions and 9 deletions

View File

@ -14,28 +14,29 @@ class AuthCenter:
def __init__(self, sock_path, config_file): def __init__(self, sock_path, config_file):
self._read_config(config_file) self._read_config(config_file)
self._socket = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM) self._socket = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
self._socket.setblocking(0)
if os.path.exists(sock_path): if os.path.exists(sock_path):
os.unlink(sock_path) os.unlink(sock_path)
self._socket.bind(sock_path) self._socket.bind(sock_path)
self._rxhandle = threading.Thread(target=self._rx_thread) self._rxhandle = threading.Thread(target=self._rx_thread)
self._rxhandle.shutdown = False self._rxhandle.ready = threading.Event()
self._rxhandle.start() self._rxhandle.start()
# wait for rx thread to start
self._rxhandle.ready.wait()
def _rx_thread(self): def _rx_thread(self):
self._rxhandle.ready.set()
while (True): while (True):
if self._rxhandle.shutdown == True:
break
try: try:
data, addr = self._socket.recvfrom(1000) data, addr = self._socket.recvfrom(1000)
data = data.decode('ascii') data = data.decode('ascii')
resp = self._process_data(data) resp = self._process_data(data)
except BlockingIOError: except OSError:
continue break
except: except:
print("Exception:", sys.exc_info()[0]) print("Exception:", sys.exc_info()[0])
continue break
if resp: if resp:
self._socket.sendto(bytearray(resp, 'UTF-8'), addr) self._socket.sendto(bytearray(resp, 'UTF-8'), addr)
@ -235,9 +236,9 @@ class AuthCenter:
''' '''
Stop the Authentication server and close the socket Stop the Authentication server and close the socket
''' '''
self._rxhandle.shutdown = True self._socket.shutdown(socket.SHUT_RDWR)
self._rxhandle.join()
self._socket.close() self._socket.close()
self._rxhandle.join()
if __name__ == '__main__': if __name__ == '__main__':
''' '''