Skip to content

Commit 5e4e790

Browse files
committed
Gracefully handle errors when binding SocketCAN fails.
The parent class BusABC expects to be shutdown properly, checked via its self._is_shutdown flag during object deletion. But that flag is only set when the base class shutdown() is called, which doesn't happen if instantiation of the concrete class failed in can.Bus(). So there is no way to avoid the warning message "SocketcanBus was not properly shut down" if the constructor raised an exception. This change addresses that issue for the SocketCAN interface, by catching an OSError exception in bind_socket(), logging it, and calling self.shutdown().
1 parent 7dba449 commit 5e4e790

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

can/interfaces/socketcan/socketcan.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -705,14 +705,20 @@ def __init__(
705705
# so this is always supported by the kernel
706706
self.socket.setsockopt(socket.SOL_SOCKET, constants.SO_TIMESTAMPNS, 1)
707707

708-
bind_socket(self.socket, channel)
709-
kwargs.update(
710-
{
711-
"receive_own_messages": receive_own_messages,
712-
"fd": fd,
713-
"local_loopback": local_loopback,
714-
}
715-
)
708+
try:
709+
bind_socket(self.socket, channel)
710+
kwargs.update(
711+
{
712+
"receive_own_messages": receive_own_messages,
713+
"fd": fd,
714+
"local_loopback": local_loopback,
715+
}
716+
)
717+
except OSError as error:
718+
log.error("Could not access SocketCAN device %s (%s)", channel, error)
719+
# Clean up so the parent class doesn't complain about not being shut down properly
720+
self.shutdown()
721+
raise
716722
super().__init__(
717723
channel=channel,
718724
can_filters=can_filters,

0 commit comments

Comments
 (0)