{"record":{"id":"d6cda68fe1d178c4","repo":"commaai/openpilot","slug":"failed-to-create-can-socket","errorCode":null,"errorMessage":"Failed to create CAN socket\n","messagePattern":"Failed to create CAN socket\n","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"openpilot/tools/cabana/streams/socketcanstream.cc","lineNumber":48,"sourceCode":"SocketCanStream::~SocketCanStream() {\n  stop();\n  if (sock_fd >= 0) {\n    ::close(sock_fd);\n    sock_fd = -1;\n  }\n}\n\nbool SocketCanStream::available() {\n  int fd = socket(PF_CAN, SOCK_RAW, CAN_RAW);\n  if (fd < 0) return false;\n  ::close(fd);\n  return true;\n}\n\nbool SocketCanStream::connect() {\n  sock_fd = socket(PF_CAN, SOCK_RAW, CAN_RAW);\n  if (sock_fd < 0) {\n    fprintf(stderr, \"Failed to create CAN socket\\n\");\n    return false;\n  }\n\n  // Enable CAN-FD\n  int fd_enable = 1;\n  setsockopt(sock_fd, SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &fd_enable, sizeof(fd_enable));\n\n  struct ifreq ifr = {};\n  strncpy(ifr.ifr_name, config.device.c_str(), IFNAMSIZ - 1);\n  if (ioctl(sock_fd, SIOCGIFINDEX, &ifr) < 0) {\n    fprintf(stderr, \"Failed to get interface index for %s\\n\", config.device.c_str());\n    ::close(sock_fd);\n    sock_fd = -1;\n    return false;\n  }\n\n  struct sockaddr_can addr = {};\n  addr.can_family = AF_CAN;","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/commaai/openpilot/blob/516ec1e68203439a73f340f1d0b3b91eabc626ee/openpilot/tools/cabana/streams/socketcanstream.cc#L30-L66","documentation":"Emitted by SocketCanStream::connect() in tools/cabana/streams/socketcanstream.cc when socket(PF_CAN, SOCK_RAW, CAN_RAW) returns a negative fd. Creating a raw CAN socket requires kernel SocketCAN support and (for raw sockets) appropriate privilege; failure at this first step means the process cannot talk to any CAN interface at all.","triggerScenarios":"Starting a SocketCAN stream (connect()) on a machine where the kernel lacks the can/can_raw modules (socket() fails with EAFNOSUPPORT/EPROTONOSUPPORT), or the process lacks privileges for SOCK_RAW (EACCES — raw sockets need CAP_NET_RAW or root). Note SocketCanStream::available() uses the same call, so this usually implies available() was bypassed or raced.","commonSituations":"Running cabana on a non-Linux platform or a minimal kernel without CONFIG_CAN; container/WSL1 environments without CAN support; unprivileged user without CAP_NET_RAW (no udev/CAP granting); can_raw module not loaded (`modprobe can_raw` never run and not auto-loaded).","solutions":["Verify SocketCAN: `sudo modprobe can can_raw` then re-run; check /proc/net/can exists.","Run with privileges for the raw socket: root, or grant CAP_NET_RAW to the cabana binary (`setcap cap_net_raw+ep $(which cabana)`).","Confirm the platform: SocketCAN needs a real Linux kernel — on WSL use WSL2 with usbip, on macOS use a different stream type.","Check that available() is honored in the stream picker so the option is hidden on unsupported hosts."],"exampleFix":"# before\n# stderr: Failed to create CAN socket\nlsmod | grep can_raw   # nothing\n\n# after\nsudo modprobe can can_raw\nsudo setcap cap_net_raw+ep $(which cabana)\ncabana   # socket(PF_CAN, SOCK_RAW, CAN_RAW) now returns a valid fd","handlingStrategy":"validation","validationCode":"// Honor the library's own availability probe before offering/starting the stream:\nif (!SocketCanStream::available()) {\n  // socket(PF_CAN, SOCK_RAW, CAN_RAW) would fail here: hide/disable the option\n  return;\n}\n// Or probe directly:\nint fd = socket(PF_CAN, SOCK_RAW, CAN_RAW);\nbool socketcanSupported = fd >= 0;\nif (fd >= 0) ::close(fd);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["`modprobe can can_raw` on hosts where it is not auto-loaded.","Grant CAP_NET_RAW to cabana or run privileged for raw CAN sockets.","On containers/WSL, verify /proc/net/can exists before promising SocketCAN support."],"tags":["socketcan","linux","can","kernel","privileges"],"backgroundTag":null,"analyzedSha":"516ec1e68203439a73f340f1d0b3b91eabc626ee","analyzedAt":"2026-08-15T00:17:37.461Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}