Skip to content

SocketCAN send timeout semantics - #2073

Open
jafrivacation wants to merge 1 commit into
hardbyte:mainfrom
jafrivacation:fix-socketcan-timeout-semantics
Open

jafrivacation wants to merge 1 commit into
hardbyte:mainfrom
jafrivacation:fix-socketcan-timeout-semantics

Conversation

@jafrivacation

@jafrivacation jafrivacation commented Jun 29, 2026 •

Copy link
Copy Markdown

Fixes #2005.

SocketCAN now follows the BusABC timeout contract: send(timeout=None) waits indefinitely for the transmit queue instead of polling immediately.

Validation:

  • python -m pytest test/test_socketcan.py -q
  • python -m compileall can/interfaces/socketcan/socketcan.py

@jafrivacation jafrivacation changed the title Fix SocketCAN send timeout semantics SocketCAN send timeout semantics Jun 29, 2026
@jafrivacation
jafrivacation marked this pull request as ready for review June 29, 2026 17:28
@mergify

mergify Bot commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue).

  • Queue this pull request

@zariiii9003

Copy link
Copy Markdown
Collaborator

AI Review

Verdict: Request changes. The fix is correct, but it must be rebased, and as it stands it probably does not fix the scenario in #2005.

Correctness

The change makes SocketcanBus.send() follow the BusABC.send() contract: timeout=None now waits indefinitely for the socket to become writable, instead of polling once. I applied the production change to current main and checked:

  • send(msg) passes None to the poll call, including on retries after a partial write.
  • send(msg, timeout=0) and send(msg, timeout=0.2) behave as before.
  • The existing SocketCAN tests pass.

Requested changes

  1. Rebase onto main. Replaced select.select() with select.epoll() in SocketcanBus #2056 replaced select.select() with select.epoll() in SocketcanBus. The production change still applies cleanly, but test/test_socketcan.py conflicts. The new test also patches select.select, which send() no longer calls, and on main it fails with AttributeError: 'SocketcanBus' object has no attribute '_epoll'. Please rewrite the test against the epoll-based implementation.

  2. Do not close SocketcanBus.send() timeout semantics differ from BusABC.send() #2005 with this PR, or extend the fix. When the transmit queue of a SocketCAN interface is full, Linux usually returns ENOBUFS from send() rather than reporting the socket as not writable. This is the default case: CAN devices use txqueuelen 10, which fills well before the socket send buffer does. ENOBUFS goes through _send_once(), which still raises CanOperationError("Failed to transmit: No buffer space available") immediately, whatever the timeout. So the reproducer in SocketcanBus.send() timeout semantics differ from BusABC.send() #2005 most likely still fails with this change. Waiting only takes effect when the socket send buffer fills before the device queue. To fully fix SocketcanBus.send() timeout semantics differ from BusABC.send() #2005, send() would also need to retry on ENOBUFS until the timeout expires. This analysis is based on kernel behaviour and has not been reproduced on hardware.

  3. Add a changelog fragment. For users who rely on the old behaviour, this is a visible change: send() without a timeout can now block instead of raising. This also affects ThreadSafeBus (a blocked send() holds the send lock, so shutdown() waits as well) and thread-based cyclic tasks, which call send() without a timeout. The fragment should state the new behaviour explicitly, for example:

    ``SocketcanBus.send()`` now waits indefinitely for the transmit queue when ``timeout`` is ``None``, as documented for ``BusABC.send()``, instead of raising immediately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SocketcanBus.send() timeout semantics differ from BusABC.send()

3 participants