From 60864de16b43f755aeff4d5cc7cb12472d08cfe2 Mon Sep 17 00:00:00 2001 From: Raf Geens Date: Mon, 20 Sep 2021 12:56:16 +0000 Subject: [PATCH] [FIX] hw_drivers: Block when getting network interface get_ip and get_mac_address respectively try to get the IP and MAC from the IoT box's active network interface. If the interface isn't fully up yet (due to waiting on a slow DHCP server response for example), both functions can end up raising a KeyError exception since neither interface has a value. This can be bad if it's uncaught and happens in something like the Manager thread, potentially killing it as a result. This can then disrupt the rest of the application. The network being gone for a bit or slow is something that can happen during everyday use, so we should be robust against it. Since the box can't do any useful work without an active network connection, this fix keeps retrying until one of the interfaces comes back up. opw-2357144 closes odoo/odoo#77052 X-original-commit: d0359858b17fb84c04c267bdf5cc1626569f5cce Signed-off-by: Quentin Lejeune (qle) --- addons/hw_drivers/tools/helpers.py | 34 +++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/addons/hw_drivers/tools/helpers.py b/addons/hw_drivers/tools/helpers.py index 3254e179e38..cf4a67b2770 100644 --- a/addons/hw_drivers/tools/helpers.py +++ b/addons/hw_drivers/tools/helpers.py @@ -134,16 +134,34 @@ def get_img_name(): return 'iotboxv%s_%s.zip' % (major, minor) def get_ip(): - try: - return netifaces.ifaddresses('eth0')[netifaces.AF_INET][0]['addr'] - except: - return netifaces.ifaddresses('wlan0')[netifaces.AF_INET][0]['addr'] + while True: + try: + return netifaces.ifaddresses('eth0')[netifaces.AF_INET][0]['addr'] + except KeyError: + pass + + try: + return netifaces.ifaddresses('wlan0')[netifaces.AF_INET][0]['addr'] + except KeyError: + pass + + _logger.warning("Couldn't get IP, sleeping and retrying.") + time.sleep(5) def get_mac_address(): - try: - return netifaces.ifaddresses('eth0')[netifaces.AF_LINK][0]['addr'] - except: - return netifaces.ifaddresses('wlan0')[netifaces.AF_LINK][0]['addr'] + while True: + try: + return netifaces.ifaddresses('eth0')[netifaces.AF_LINK][0]['addr'] + except KeyError: + pass + + try: + return netifaces.ifaddresses('wlan0')[netifaces.AF_LINK][0]['addr'] + except KeyError: + pass + + _logger.warning("Couldn't get MAC address, sleeping and retrying.") + time.sleep(5) def get_ssid(): ap = subprocess.call(['systemctl', 'is-active', '--quiet', 'hostapd']) # if service is active return 0 else inactive