smarthomeNG / smarthome

Device integration platform for your smart home
https://www.smarthomeNG.de
GNU General Public License v3.0
122 stars 91 forks source link

Bug in ``Utils.get_all_local_ipv4_addresses()`` #646

Open bmxp opened 7 months ago

bmxp commented 7 months ago

There seems to be a bug in Utils.get_all_local_ipv4_addresses()

2024-04-21  17:00:20 ERROR    cherrypy.error.140570659457104 [21/Apr/2024:17:00:20] HTTP 
> Traceback (most recent call last):
>   File "/home/smarthome/.local/lib/python3.9/site-packages/cherrypy/_cprequest.py", line 638, in respond
>     self._do_respond(path_info)
>   File "/home/smarthome/.local/lib/python3.9/site-packages/cherrypy/_cprequest.py", line 697, in _do_respond
>     response.body = self.handler()
>   File "/home/smarthome/.local/lib/python3.9/site-packages/cherrypy/lib/encoding.py", line 223, in __call__
>     self.body = self.oldhandler(*args, **kwargs)
>   File "/home/smarthome/.local/lib/python3.9/site-packages/cherrypy/_cpdispatch.py", line 54, in __call__
>     return self.callable(*self.args, **self.kwargs)
>   File "/usr/local/smarthome/modules/admin/systemdata.py", line 137, in systeminfo_json
>     ip = Utils.get_all_local_ipv4_addresses()
>   File "/usr/local/smarthome/lib/utils.py", line 211, in get_all_local_ipv4_addresses
>     for link in ifaddresses(interface)[AF_INET]:
> KeyError: 2

I just can not really reproduce it, it's very rare and I do not find any reason when it comes up. Since it is just a KeyError we could catch it probably very easy...

msinn commented 7 months ago

The Error comes from ifaddresses(interface)[AF_INET] and AF_INET is a constant with the value of 2.

This looks like the system has no ipv4 address.

Does it happen directly after a system start and the ip stack is not completely initialized? Can it be, that no dhcp address is assigned to the system yet?

If we just catch the error, should the function just return an empty list?

bmxp commented 7 months ago

Hm. No. The basic system ist running for some weeks now and I do have an IP4 adress. Yesterday the error occurred twice while using the admin interface (looking at logics, logs and items)

msinn commented 7 months ago

I caught the exception and log a warning. Could you elaborate on your environment (os, python version, ...)?

bmxp commented 7 months ago

This is the log entry:

2024-04-24 19:14:23 WARNING lib.utils get_all_local_ipv4_addresses: Exception 2 - {17: [{'addr': '06:af:73:fe:f7:11', 'broadcast': 'ff:ff:ff:ff:ff:ff'}]}

Running Python 3.9.2 on Linux (5.10.0-27-amd64 #1 SMP Debian 5.10.205-2 (2023-12-31) x86_64 GNU/Linux)

msinn commented 7 months ago

Ok, there seems to be an interface that has no ipv4 address (AF_INET, which is 2) and no ipv6 address (AF_INET6, which is 10)

I added further info to the logging. Could you test again and post the (multiline) log entry?

bmxp commented 7 months ago

This results in:

2024-04-26  06:54:56 WARNING  lib.utils         get_all_local_ipv4_addresses: Exception 2 
- interfaces=['lo', 'eno1', 'wlo2']
-act. interface=wlo2
-act. interface data={'lo': {17: [{'addr': '00:00:00:00:00:00', 'peer': '00:00:00:00:00:00'}], 2: [{'addr': '127.0.0.1', 'netmask': '255.0.0.0', 'peer': '127.0.0.1'}], 10: [{'addr': '::1', 'netmask': 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128'}]}, 'eno1': {17: [{'addr': '1c:69:7a:a4:9c:0d', 'broadcast': 'ff:ff:ff:ff:ff:ff'}], 2: [{'addr': '192.168.20.13', 'netmask': '255.255.255.0', 'broadcast': '192.168.20.255'}], 10: [{'addr': 'fe80::1e69:7aff:fea4:9c0d%eno1', 'netmask': 'ffff:ffff:ffff:ffff::/64'}]}, 'wlo2': {17: [{'addr': 'da:81:02:8b:0d:37', 'broadcast': 'ff:ff:ff:ff:ff:ff'}]}}
- {17: [{'addr': 'da:81:02:8b:0d:37', 'broadcast': 'ff:ff:ff:ff:ff:ff'}]}
msinn commented 7 months ago

I have pushed a fix (and removed the logging)