python-can icon indicating copy to clipboard operation
python-can copied to clipboard

Gracefully handle errors when binding SocketCAN fails

Open acolomb opened this issue 1 year ago • 2 comments

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() before re-raising the exception.

There might be a better approach which also works for the other backends? The flag could be initialized to True and only reset to False when the BusABC constructor runs (which happens last in derived classes)?

Thus leaving this as a draft for further discussion.

acolomb avatar Apr 24 '24 10:04 acolomb

The alternative approach is now proposed in #1774. This PR could still be useful in addition, to have a proper error log message. If that is desired...

acolomb avatar Apr 25 '24 07:04 acolomb

Adjusted as requested. Thanks for your consideration.

acolomb avatar Apr 30 '24 13:04 acolomb

Ping @hardbyte, want to merge this?

acolomb avatar Jun 13 '24 19:06 acolomb