Skip to content

socketcand: read handshake replies from the stream buffer - #2150

Open
DanShinde wants to merge 3 commits into
hardbyte:mainfrom
DanShinde:socketcand-handshake-buffer
Open

DanShinde wants to merge 3 commits into
hardbyte:mainfrom
DanShinde:socketcand-handshake-buffer

Conversation

@DanShinde

Copy link
Copy Markdown
Contributor

Summary of Changes

  • _expect_msg() compared the result of a single recv() with the expected reply. TCP is a stream, so a reply can arrive in two reads, and after < rawmode > socketcand can send the first frames in the same segment as < ok > on a busy bus. Both cases raised CanError in the bus constructor.
  • The handshake now reads into the existing receive buffer until a whole reply is there, compares only that reply and keeps the rest. Frames that came with the reply are parsed (the parsing loop moved into _parse_receive_buffer(), unchanged) and returned by the next recv().
  • Tests run a small local TCP server: frames in the same read as the rawmode < ok >, replies split over reads, and an error reply still raising CanError.

Related Issues / Pull Requests

Type of Change

  • Bug fix
  • New feature
  • Documentation update
  • Refactoring
  • Other (please describe):

Checklist

  • I have followed the contribution guide.
  • I have added or updated tests as appropriate.
  • I have added or updated documentation as appropriate.
  • I have added a news fragment for towncrier.
  • All checks and tests pass (tox).

The handshake compared a single recv() with the expected reply. TCP may
split a reply over several reads, and after "< rawmode >" socketcand can
send the first frames in the same segment as "< ok >". Both raised
CanError during the bus constructor.

Collect data until a whole reply is in the receive buffer, compare only
that reply and keep anything after it, so frames that arrived with the
reply are returned by recv().
@zariiii9003

Copy link
Copy Markdown
Collaborator

AI Review

Verdict: Request changes. Compared to main, this fixes two ways the socketcand handshake fails: a reply split over several TCP reads, and the first frames arriving in the same read as the rawmode < ok >. It also adds a new way for the constructor to block indefinitely. One change is required.

Behaviour vs. main

  • Handshake: main compares the raw result of a single recv(256) with the expected reply, so a split reply or < ok >< frame … > raises CanError in the constructor. With this PR, _expect_msg() reads into __receive_buffer until it contains a >, compares only the first <…> reply, and keeps the rest of the buffer.
  • Frames that arrive with the reply: the leftover bytes are parsed into __message_buffer, and the first recv() after construction returns them. A partial frame stays in __receive_buffer and is completed by the next read.
  • Server closes the connection during the handshake: this still raises CanError.
  • Refactor: _parse_receive_buffer() is the parsing loop from _recv_internal(), moved without changes.
  • Compatibility: no public names, signatures or defaults change.

Required

can/interfaces/socketcand/socketcand.py, _expect_msg(): if the server sends data that never contains > and keeps the connection open, the constructor now blocks indefinitely, because the socket has no timeout. This happens, for example, when the configured port belongs to another service. Reproduced with a local server that sends SSH-2.0-OpenSSH_9.6\r\n and keeps the connection open: on main the constructor raises CanError after 0.02 s; with this PR it blocks until the server closes the connection.

A socketcand reply always starts with <, so fail as soon as the buffer cannot be a reply. The new exceptions should be CanInitializationError, as BusABC.__init__ documents for initialization failures. It is a subclass of CanError, so existing except CanError handlers still catch it.

while ">" not in self.__receive_buffer:
    buffer = self.__receive_buffer.lstrip()
    if (buffer and not buffer.startswith("<")) or len(buffer) > 200:
        raise can.CanInitializationError(
            f"Expected '{msg}' got: '{self.__receive_buffer}'"
        )
    data = self.__socket.recv(256)
    if not data:
        raise can.CanInitializationError(
            f"Expected '{msg}' got: '{self.__receive_buffer}' (connection closed)"
        )

…cand

Raise CanInitializationError instead of blocking when the server sends
data that does not start with '<' (e.g. another service on that port).
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.

2 participants