Skip to content

feat(socketcan): name the channel when binding a socket fails - #2184

Open
ChuangMuLinze wants to merge 1 commit into
hardbyte:mainfrom
ChuangMuLinze:feat/socketcan-bind-error-context
Open

ChuangMuLinze wants to merge 1 commit into
hardbyte:mainfrom
ChuangMuLinze:feat/socketcan-bind-error-context

Conversation

@ChuangMuLinze

Copy link
Copy Markdown

Closes #2104

Description of the Change

bind_socket() re-raised the OSError from socket.bind() unchanged. Client code that catches it therefore had no way to tell which bus failed when several were in use, unless debug logging was on.

The channel is now attached as a note on the original exception, and the failure is logged:

    try:
        sock.bind((channel,))
    except OSError as exc:
        exc.add_note(f"Failed to bind SocketCAN socket to channel {channel!r}.")
        log.exception("Error binding SocketCAN socket to channel %r", channel)
        raise

A traceback now ends with:

OSError: [Errno 19] No such device
Failed to bind SocketCAN socket to channel 'vcan42'.

I used add_note() rather than re-raising a new exception so that except OSError keeps working and errno/strerror are preserved — only the context is added. add_note() is available on every version python-can supports (requires-python = ">=3.11").

How I Tested It

Two tests in test/test_socketcan_helpers.py, both driven by a mocked socket so they need no CAN hardware:

  • test_bind_socket_adds_context_to_oserror — the raised OSError carries a note naming the channel. Fails on main (expected the channel in []), passes with the change.
  • test_bind_socket_keeps_original_error — the exception stays an OSError with errno == 19 and the original strerror.
$ python -m pytest test/test_socketcan.py test/test_socketcan_helpers.py -q
17 passed, 5 skipped, 7 subtests passed

black --check and ruff report nothing new on the touched files.

Scope

I kept this to the one call site the issue points at. The issue also suggests auditing other places that raise naked OSErrors without context — I'd rather do that separately, so this stays reviewable in one pass.

@zariiii9003

Copy link
Copy Markdown
Collaborator

AI Review: socketcan: name the channel when binding a socket fails (#2184)

Adding the channel as a note on the original OSError is the right approach: except OSError, errno and strerror keep working, and the traceback now names the failing channel. Two points need changes.

Code

  • can/interfaces/socketcan/socketcan.py, bind_socket: remove log.exception(...). The only caller, SocketcanBus.__init__, already catches this OSError, logs it with log.error("Could not access SocketCAN device %s (%s)", ...) and re-raises. With this PR, can.Bus(interface="socketcan", channel="vcan42") logs the failure twice at ERROR level, the first time with a full traceback, even when the application catches and handles the exception. The note already gives the context requested in Make socket binding exceptions more debugable #2104. Keep only exc.add_note(...) and raise.

Tests

  • Move the tests to test/test_socketcan.py. That file covers can.interfaces.socketcan.socketcan (see its module docstring and test_init_ignores_so_timestampns_oserror); test/test_socketcan_helpers.py covers socketcan.utils. Add them to SocketCANTest instead of a new class placed after the if __name__ == "__main__": block.
  • Merge the two tests into one. They share the same setup. A single test that asserts errno, strerror and the channel in __notes__ guards the same regressions with half the code.

Verdict

request changes

@ChuangMuLinze
ChuangMuLinze force-pushed the feat/socketcan-bind-error-context branch from ccc50be to 036662c Compare December 6, 2026 05:58
@ChuangMuLinze

Copy link
Copy Markdown
Author

Thanks — all three points are addressed in 036662c.

  • Dropped log.exception(). Only exc.add_note(...) and raise remain, so the failure is logged exactly once, by SocketcanBus.__init__.
  • Tests moved to test/test_socketcan.py, inside SocketCANTest, instead of the new class appended after if __name__ == "__main__":. test_socketcan_helpers.py is back to its original state.
  • Merged into a single test, test_bind_socket_adds_channel_to_oserror, which asserts the bind call, errno, strerror and the channel in __notes__.

python -m pytest test/test_socketcan.py test/test_socketcan_helpers.py → 16 passed, 5 skipped. ruff check on both changed files is clean.

@ChuangMuLinze ChuangMuLinze reopened this Dec 6, 2026
@ChuangMuLinze
ChuangMuLinze force-pushed the feat/socketcan-bind-error-context branch from 036662c to 041cc5e Compare December 6, 2026 06:03
bind_socket() re-raised the OSError from socket.bind() unchanged, so client
code that catches it had no way to tell which bus failed when several were in
use. Add the channel as a note on the exception, following the pattern used
by candump for the same failure.
@ChuangMuLinze
ChuangMuLinze force-pushed the feat/socketcan-bind-error-context branch from 041cc5e to 10de6b3 Compare December 6, 2026 06:10
@ChuangMuLinze

Copy link
Copy Markdown
Author

Rebased onto current main (4eaf26c) — the branch had gone stale and showed a conflict with #2176. test/test_socketcan.py keeps both test_init_ignores_so_timestampns_oserror and the new test; pytest test/test_socketcan.py test/test_socketcan_helpers.py → 18 passed, 5 skipped.

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.

Make socket binding exceptions more debugable

2 participants