Skip to content

[Bug] can.detect_available_configs("socketcan") fails with KeyError: "link_type" #1598

Description

@DanielBrandes-FlyNow

Describe the bug

Hi, we had here a docker container with host network setting enabled (Ubuntu 22.04 inside and outside). it creates vcan interfaces.

when I create a small file like

import can

can.detect_available_configs("socketcan")

and run it via python, i got the following error:

Traceback (most recent call last):
File "tmp.py", line 3, in
can.detect_available_configs("socketcan")
File "/usr/local/lib/python3.10/dist-packages/can/interface.py", line 186, in detect_available_configs
bus_class._detect_available_configs() # pylint: disable=protected-access
File "/usr/local/lib/python3.10/dist-packages/can/interfaces/socketcan/socketcan.py", line 878, in _detect_available_configs
for channel in find_available_interfaces()
File "/usr/local/lib/python3.10/dist-packages/can/interfaces/socketcan/utils.py", line 69, in find_available_interfaces
interfaces = [i["ifname"] for i in output_json if i["link_type"] == "can"]
File "/usr/local/lib/python3.10/dist-packages/can/interfaces/socketcan/utils.py", line 69, in
interfaces = [i["ifname"] for i in output_json if i["link_type"] == "can"]
KeyError: 'link_type'

The problem here is that the output of

ip -json link list up

results in

[
    {
        "ifindex": 1,
        "ifname": "lo",
        "flags": [
            "LOOPBACK",
            "UP",
            "LOWER_UP"
        ],
        "mtu": 65536,
        "qdisc": "noqueue",
        "operstate": "UNKNOWN",
        "linkmode": "DEFAULT",
        "group": "default",
        "txqlen": 1000,
        "link_type": "loopback",
        "address": "00:00:00:00:00:00",
        "broadcast": "00:00:00:00:00:00"
    },
    {},
    {
        "ifindex": 5,
        "ifname": "docker0",
        "flags": [
            "NO-CARRIER",
            "BROADCAST",
            "MULTICAST",
            "UP"
        ],
        "mtu": 1500,
        "qdisc": "noqueue",
        "operstate": "DOWN",
        "linkmode": "DEFAULT",
        "group": "default",
        "link_type": "ether",
        "address": "02:42:48:75:80:6b",
        "broadcast": "ff:ff:ff:ff:ff:ff"
    },
...

so especially there is one empty json object in between the other devices. This is the problematic one. So to fix it, in

python-can/can/interfaces/socketcan/utils.py

at line 69 one need to check if i is empty or maybe even better if i contains "link_type".

To Reproduce

I found no way to easy reproduce this one without our docker container at the moment, it can be simulated if instead of the "ip -json link list up" call (python-can/can/interfaces/socketcan/utils.py row 52) in the code some json is read (or python hardcoded list) that contains an empty dict / object.

Expected behavior

The python script given above should run without errors :-).

Additional context

OS and version: Ubuntu 22.04 inside docker Ubuntu 22.04
Python version: 3.10.6
python-can version: 4.2.1
python-can interface/s (if applicable): vcan

Traceback and logs

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions