From 7079d0fb5833659cba8470ed4efadd33e8e446b3 Mon Sep 17 00:00:00 2001 From: Dan Shinde <112338485+DanShinde@users.noreply.github.com> Date: Sun, 22 Nov 2026 17:22:37 +0000 Subject: [PATCH] socketcan: retry send() on ENOBUFS until the timeout expires A full device queue can make send() fail with ENOBUFS even though epoll reports the socket as writable. Wait briefly and retry while the timeout has not expired, instead of failing right away. Closes #2004 --- can/interfaces/socketcan/socketcan.py | 16 +++++++- doc/changelog.d/2004.fixed.rst | 1 + test/test_socketcan.py | 58 +++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 doc/changelog.d/2004.fixed.rst diff --git a/can/interfaces/socketcan/socketcan.py b/can/interfaces/socketcan/socketcan.py index a47f3a882..5970d8a20 100644 --- a/can/interfaces/socketcan/socketcan.py +++ b/can/interfaces/socketcan/socketcan.py @@ -47,6 +47,9 @@ CMSG_SPACE(RECEIVED_TIMESTAMP_STRUCT.size) if CMSG_SPACE_available else 0 ) +# Delay between send attempts while the device queue is full (ENOBUFS) +ENOBUFS_RETRY_DELAY = 0.001 + MSG_DONTROUTE = int(socket.MSG_DONTROUTE) @@ -875,7 +878,18 @@ def send(self, msg: Message, timeout: float | None = None) -> None: # Timeout break channel = str(msg.channel) if msg.channel else None - sent = self._send_once(data, channel) + try: + sent = self._send_once(data, channel) + except can.CanOperationError as error: + if error.error_code != errno.ENOBUFS: + raise + # The device queue is full even though the socket reported it + # as writable. Wait a little and retry until the timeout expires. + time_left = timeout - (time.time() - started) + if time_left <= 0: + raise + time.sleep(min(ENOBUFS_RETRY_DELAY, time_left)) + continue if sent == len(data): return # Not all data were sent, try again with remaining data diff --git a/doc/changelog.d/2004.fixed.rst b/doc/changelog.d/2004.fixed.rst new file mode 100644 index 000000000..5916f5dfa --- /dev/null +++ b/doc/changelog.d/2004.fixed.rst @@ -0,0 +1 @@ +Retry SocketcanBus.send() while the device transmit queue is full (ENOBUFS) instead of failing before the timeout has expired. diff --git a/test/test_socketcan.py b/test/test_socketcan.py index 35e18323b..3b5430a14 100644 --- a/test/test_socketcan.py +++ b/test/test_socketcan.py @@ -5,7 +5,9 @@ """ import ctypes +import errno import os +import select import socket import struct import sys @@ -27,6 +29,7 @@ from can.interfaces.socketcan.socketcan import ( RECEIVED_TIMESTAMP_STRUCT, BcmMsgHead, + SocketcanBus, bcm_header_factory, build_bcm_header, build_bcm_transmit_header, @@ -477,6 +480,61 @@ def test_capture_message(self): received.equals(expected), f"{received!r} != {expected!r}" ) + @staticmethod + def _bus_with_mock_socket(send_side_effect) -> SocketcanBus: + bus = SocketcanBus.__new__(SocketcanBus) + bus.channel = "vcan0" + bus.socket = Mock() + bus.socket.send.side_effect = send_side_effect + bus._epoll = Mock() + bus._epoll.poll.return_value = [(3, select.EPOLLOUT)] + return bus + + @unittest.skipUnless(IS_LINUX, "epoll is only available on Linux") + def test_send_retries_on_enobufs(self): + """send() retries while the device queue is full (ENOBUFS) until the timeout""" + msg = can.Message(arbitration_id=0x123, data=[1, 2, 3]) + frame_len = len(build_can_frame(msg)) + enobufs = OSError(errno.ENOBUFS, os.strerror(errno.ENOBUFS)) + bus = self._bus_with_mock_socket([enobufs, enobufs, frame_len]) + + with patch("time.sleep") as sleep: + bus.send(msg, timeout=1.0) + + self.assertEqual(bus.socket.send.call_count, 3) + self.assertEqual(sleep.call_count, 2) + + @unittest.skipUnless(IS_LINUX, "epoll is only available on Linux") + def test_send_raises_enobufs_after_timeout(self): + """send() gives up with the ENOBUFS error once the timeout has expired""" + msg = can.Message(arbitration_id=0x123, data=[1, 2, 3]) + enobufs = OSError(errno.ENOBUFS, os.strerror(errno.ENOBUFS)) + + # without a timeout the error is raised right away + bus = self._bus_with_mock_socket(enobufs) + with self.assertRaises(can.CanOperationError) as ctx: + bus.send(msg) + self.assertEqual(ctx.exception.error_code, errno.ENOBUFS) + self.assertEqual(bus.socket.send.call_count, 1) + + # with a timeout it keeps retrying until the timeout has expired + bus = self._bus_with_mock_socket(enobufs) + with self.assertRaises(can.CanOperationError) as ctx: + bus.send(msg, timeout=0.05) + self.assertEqual(ctx.exception.error_code, errno.ENOBUFS) + self.assertGreater(bus.socket.send.call_count, 1) + + @unittest.skipUnless(IS_LINUX, "epoll is only available on Linux") + def test_send_does_not_retry_other_errors(self): + """send() does not retry errors other than ENOBUFS""" + msg = can.Message(arbitration_id=0x123, data=[1, 2, 3]) + bus = self._bus_with_mock_socket(OSError(errno.ENETDOWN, "Network is down")) + + with self.assertRaises(can.CanOperationError) as ctx: + bus.send(msg, timeout=1.0) + self.assertEqual(ctx.exception.error_code, errno.ENETDOWN) + self.assertEqual(bus.socket.send.call_count, 1) + if __name__ == "__main__": unittest.main()