commaai/openpilot · error
Failed to create CAN socket
Error message
Failed to create CAN socket
What it means
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.
Source
Thrown at openpilot/tools/cabana/streams/socketcanstream.cc:48
SocketCanStream::~SocketCanStream() {
stop();
if (sock_fd >= 0) {
::close(sock_fd);
sock_fd = -1;
}
}
bool SocketCanStream::available() {
int fd = socket(PF_CAN, SOCK_RAW, CAN_RAW);
if (fd < 0) return false;
::close(fd);
return true;
}
bool SocketCanStream::connect() {
sock_fd = socket(PF_CAN, SOCK_RAW, CAN_RAW);
if (sock_fd < 0) {
fprintf(stderr, "Failed to create CAN socket\n");
return false;
}
// Enable CAN-FD
int fd_enable = 1;
setsockopt(sock_fd, SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &fd_enable, sizeof(fd_enable));
struct ifreq ifr = {};
strncpy(ifr.ifr_name, config.device.c_str(), IFNAMSIZ - 1);
if (ioctl(sock_fd, SIOCGIFINDEX, &ifr) < 0) {
fprintf(stderr, "Failed to get interface index for %s\n", config.device.c_str());
::close(sock_fd);
sock_fd = -1;
return false;
}
struct sockaddr_can addr = {};
addr.can_family = AF_CAN;View on GitHub (pinned to 516ec1e682)
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.
Example fix
# before # stderr: Failed to create CAN socket lsmod | grep can_raw # nothing # after sudo modprobe can can_raw sudo setcap cap_net_raw+ep $(which cabana) cabana # socket(PF_CAN, SOCK_RAW, CAN_RAW) now returns a valid fd
Defensive patterns
Strategy: validation
Validate before calling
// Honor the library's own availability probe before offering/starting the stream:
if (!SocketCanStream::available()) {
// socket(PF_CAN, SOCK_RAW, CAN_RAW) would fail here: hide/disable the option
return;
}
// Or probe directly:
int fd = socket(PF_CAN, SOCK_RAW, CAN_RAW);
bool socketcanSupported = fd >= 0;
if (fd >= 0) ::close(fd); Prevention
- `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.
When it happens
Trigger: 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.
Common situations: 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).
Related errors
- Failed to bind CAN socket
- Failed to get interface index for %s
- too few samples found in route
- signal %s out of bounds.start_bit: %d size: %d
- error: --socketcan is only supported on Linux
AI-assisted analysis of commaai/openpilot@516ec1e682 (2026-08-15).
Data as JSON: /api/errors/d6cda68fe1d178c4.
Report an issue: GitHub.