auto-t: add stop event/non-blocking to hlrauc.py
PATCH 1/2 auto-t: add stop event/non-blocking to hlrauc.py
There was a problem with AuthCenter where if an unhandled exception was thrown the read thread never exited, since stop() would never be called. In addition the read() call itself was blocking which may have led to additional issues trying to stop the thread.
Now an explicit stop event was added and the socket was set to non-blocking. This will require autotest modification in order to stop AuthCenter in an appropriate location to handle any exceptions.
--- autotests/util/hlrauc.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-)
autotests/util/hlrauc.py +11 -5
@@ -3,6 +3,7 @@ import os import threading import sys import signal +from time import sleep from Crypto.Cipher import AES class AuthCenter:
@@ -15,11 +16,13 @@ class AuthCenter: self._sock_path = sock_path self._read_config(config_file) self._socket = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM) + self._socket.setblocking(0) if os.path.exists(sock_path): os.unlink(sock_path) self._socket.bind(sock_path) - self._rxhandle = threading.Thread(target=self._rx_thread) + self.stop_event = threading.Event() + self._rxhandle = threading.Thread(target=self._rx_thread, args=(self, self.stop_event)) self._rxhandle.ready = threading.Event() self._rxhandle.start()
@@ -30,15 +33,17 @@ class AuthCenter: os.remove(self._sock_path) self._socket.close() - def _rx_thread(self): + @staticmethod + def _rx_thread(self, stop_event): self._rxhandle.ready.set() - while (True): + while not stop_event.isSet(): try: data, addr = self._socket.recvfrom(1000) data = data.decode('ascii') resp = self._process_data(data) - except OSError: - break + except BlockingIOError: + sleep(0.5) + continue except: print("Exception:", sys.exc_info()[0]) break
@@ -241,6 +246,7 @@ class AuthCenter: ''' Stop the Authentication server and close the socket ''' + self.stop_event.set() self._socket.shutdown(socket.SHUT_RDWR) self._socket.close() self._rxhandle.join()
Signature · 1 line
2.26.2
PATCH 2/2 auto-t: move AuthCenter to Setup/Tear down
By moving the AuthCenter initializer to SetUpClass we can now call stop() in TearDownClass which handles any exception the test might throw.
--- autotests/testEAP-AKA-ofono/connection_test.py | 14 +++++--------- .../testEAP-AKA-prime-ofono/connection_test.py | 14 ++++---------- autotests/testEAP-SIM-ofono/connection_test.py | 14 +++++--------- 3 files changed, 14 insertions(+), 28 deletions(-)
autotests/testEAP-AKA-ofono/connection_test.py +5 -9
@@ -12,10 +12,7 @@ from ofono import Ofono from config import ctx class Test(unittest.TestCase): - def test_connection_success(self): - auth = AuthCenter('/tmp/hlrauc.sock', '/tmp/sim.db') - ofono = Ofono() ofono.enable_modem('/phonesim') ofono.wait_for_sim_auth()
@@ -40,11 +37,7 @@ class Test(unittest.TestCase): condition = 'not obj.connected' wd.wait_for_object_condition(ordered_network.network_object, condition) - try: - ordered_network.network_object.connect() - except: - auth.stop() - raise + ordered_network.network_object.connect() condition = 'obj.state == DeviceState.connected' wd.wait_for_object_condition(device, condition)
@@ -54,7 +47,6 @@ class Test(unittest.TestCase): condition = 'not obj.connected' wd.wait_for_object_condition(ordered_network.network_object, condition) - auth.stop() @classmethod def setUpClass(cls): if not ctx.is_process_running('ofonod'):
@@ -62,9 +54,13 @@ class Test(unittest.TestCase): IWD.copy_to_storage('ssidEAP-AKA.8021x') + cls.auth = AuthCenter('/tmp/hlrauc.sock', '/tmp/sim.db') + @classmethod def tearDownClass(cls): IWD.clear_storage() + cls.auth.stop() + if __name__ == '__main__': unittest.main(exit=True)
autotests/testEAP-AKA-prime-ofono/connection_test.py +4 -10
@@ -12,10 +12,7 @@ from ofono import Ofono from config import ctx class Test(unittest.TestCase): - def test_connection_success(self): - auth = AuthCenter('/tmp/hlrauc.sock', '/tmp/sim.db') - ofono = Ofono() ofono.enable_modem('/phonesim') ofono.wait_for_sim_auth()
@@ -40,11 +37,7 @@ class Test(unittest.TestCase): condition = 'not obj.connected' wd.wait_for_object_condition(ordered_network.network_object, condition) - try: - ordered_network.network_object.connect() - except: - auth.stop() - raise + ordered_network.network_object.connect() condition = 'obj.state == DeviceState.connected' wd.wait_for_object_condition(device, condition)
@@ -54,8 +47,6 @@ class Test(unittest.TestCase): condition = 'not obj.connected' wd.wait_for_object_condition(ordered_network.network_object, condition) - auth.stop() - @classmethod def setUpClass(cls): if not ctx.is_process_running('ofonod'):
@@ -63,9 +54,12 @@ class Test(unittest.TestCase): IWD.copy_to_storage('ssidEAP-AKA.8021x') + cls.auth = AuthCenter('/tmp/hlrauc.sock', '/tmp/sim.db') + @classmethod def tearDownClass(cls): IWD.clear_storage() + cls.auth.stop() if __name__ == '__main__': unittest.main(exit=True)
autotests/testEAP-SIM-ofono/connection_test.py +5 -9
@@ -14,8 +14,6 @@ from config import ctx class Test(unittest.TestCase): def test_connection_success(self): - auth = AuthCenter('/tmp/hlrauc.sock', '/tmp/sim.db') - ofono = Ofono() ofono.enable_modem('/phonesim') ofono.wait_for_sim_auth()
@@ -40,11 +38,7 @@ class Test(unittest.TestCase): condition = 'not obj.connected' wd.wait_for_object_condition(ordered_network.network_object, condition) - try: - ordered_network.network_object.connect() - except: - auth.stop() - raise + ordered_network.network_object.connect() condition = 'obj.state == DeviceState.connected' wd.wait_for_object_condition(device, condition)
@@ -54,8 +48,6 @@ class Test(unittest.TestCase): condition = 'not obj.connected' wd.wait_for_object_condition(ordered_network.network_object, condition) - auth.stop() - @classmethod def setUpClass(cls): if not ctx.is_process_running('ofonod'):
@@ -63,9 +55,13 @@ class Test(unittest.TestCase): IWD.copy_to_storage('ssidEAP-SIM.8021x') + cls.auth = AuthCenter('/tmp/hlrauc.sock', '/tmp/sim.db') + @classmethod def tearDownClass(cls): IWD.clear_storage() + cls.auth.stop() + if __name__ == '__main__': unittest.main(exit=True)
Signature · 1 line
2.26.2
Denis Kenzior
AppliedRe: [PATCH 2/2] auto-t: move AuthCenter to Setup/Tear down
Hi James,
Quoted 9 lines · James Prestwood
By moving the AuthCenter initializer to SetUpClass we can now call
stop()in TearDownClass which handles any exception the test might throw.--- autotests/testEAP-AKA-ofono/connection_test.py | 14 +++++--------- .../testEAP-AKA-prime-ofono/connection_test.py | 14 ++++---------- autotests/testEAP-SIM-ofono/connection_test.py | 14 +++++--------- 3 files changed, 14 insertions(+), 28 deletions(-)
Applied, thanks.
Regards,
-Denis
Ignore this. It is not needed and redundant. Closing the socket achieves the same goal. The problem is actually fixed in Patch 2.
Quoted 71 lines · James Prestwood
There was a problem with AuthCenter where if an unhandled exception was thrown the read thread never exited, since
stop()would never be called. In addition theread()call itself was blocking which may have led to additional issues trying to stop the thread.Now an explicit stop event was added and the socket was set to non-blocking. This will require autotest modification in order to stop AuthCenter in an appropriate location to handle any exceptions.
--- autotests/util/hlrauc.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-)autotests/util/hlrauc.py +4
-1@@ -3,6 +3,7 @@ import os import threading import sys import signal +from time import sleep from Crypto.Cipher import AES class AuthCenter:@@ -15,11 +16,13 @@ class AuthCenter: self._sock_path = sock_path self._read_config(config_file) self._socket = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM) + self._socket.setblocking(0) if os.path.exists(sock_path): os.unlink(sock_path) self._socket.bind(sock_path) - self._rxhandle = threading.Thread(target=self._rx_thread) + self.stop_event = threading.Event() + self._rxhandle = threading.Thread(target=self._rx_thread, args=(self, self.stop_event)) self._rxhandle.ready = threading.Event()
self._rxhandle.start()autotests/util/hlrauc.py +7
-4@@ -30,15 +33,17 @@ class AuthCenter: os.remove(self._sock_path) self._socket.close() - def _rx_thread(self): + @staticmethod + def _rx_thread(self, stop_event): self._rxhandle.ready.set() - while (True): + while not stop_event.isSet(): try: data, addr = self._socket.recvfrom(1000) data = data.decode('ascii') resp = self._process_data(data) - except OSError: - break + except BlockingIOError: + sleep(0.5) + continue except: print("Exception:", sys.exc_info()[0]) break@@ -241,6 +246,7 @@ class AuthCenter: ''' Stop the Authentication server and close the socket ''' + self.stop_event.set() self._socket.shutdown(socket.SHUT_RDWR) self._socket.close() self._rxhandle.join()