Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug in Utils.get_all_local_ipv4_addresses() #646

Open
bmxp opened this issue Apr 21, 2024 · 7 comments
Open

Bug in Utils.get_all_local_ipv4_addresses() #646

bmxp opened this issue Apr 21, 2024 · 7 comments
Assignees

Comments

@bmxp
Copy link
Member

bmxp commented Apr 21, 2024

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...

@bmxp bmxp added the bug label Apr 21, 2024
@msinn
Copy link
Member

msinn commented Apr 21, 2024

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
Copy link
Member Author

bmxp commented Apr 22, 2024

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
Copy link
Member

msinn commented Apr 23, 2024

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

@bmxp
Copy link
Member Author

bmxp commented Apr 24, 2024

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
Copy link
Member

msinn commented Apr 24, 2024

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
Copy link
Member Author

bmxp commented Apr 26, 2024

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
Copy link
Member

msinn commented Apr 27, 2024

I have pushed a fix (and removed the logging)

@msinn msinn added the resolved label Apr 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants