Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion can/interfaces/socketcan/socketcan.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions doc/changelog.d/2004.fixed.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Retry SocketcanBus.send() while the device transmit queue is full (ENOBUFS) instead of failing before the timeout has expired.
58 changes: 58 additions & 0 deletions test/test_socketcan.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"""

import ctypes
import errno
import os
import select
import socket
import struct
import sys
Expand All @@ -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,
Expand Down Expand Up @@ -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()
Loading