You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
serial: a frame cut off by the read timeout raises struct.error / TypeError instead of CanOperationError #2157
SerialBus._recv_internal reads a frame field by field with self._ser.read(n) and assumes each read returns the full length. When the port's read timeout expires partway through a frame (the adapter stalls, gets unplugged, or the frame is split across reads), the short read reaches struct.unpack or ord() and raises:
struct.error: unpack requires a buffer of 4 bytes when the timestamp or arbitration ID is short
TypeError: ord() expected a character, but string of length 0 found when the DLC or end delimiter byte is missing
Neither error is a can.CanError, so code that handles bus errors with except can.CanError (or CanOperationError, which the same method already raises for a bad delimiter and for SerialException) doesn't catch it. A short data payload also goes unnoticed until the delimiter read fails.
To Reproduce
Using the loop:// URL from pyserial, so no hardware is needed:
3 error unpack requires a buffer of 4 bytes
6 error unpack requires a buffer of 4 bytes
12 TypeError ord() expected a character, but string of length 0 found
18 TypeError ord() expected a character, but string of length 0 found
Expected behavior
A truncated frame raises can.CanOperationError (for example "serial read timed out in the middle of a frame"), like the invalid-delimiter case already does. It could also be dropped with a warning, but it shouldn't surface as struct.error / TypeError.
Additional context
OS and version: Linux
Python version: 3.13
python-can version: main (a594781)
python-can interface/s (if applicable): serial
The ValueError("received DLC may not exceed 8 bytes") in the same method has the same problem: it isn't a CanError either.
I plan to make the serial receiver detect short reads at the point they happen and raise can.CanOperationError with a clear message, including truncated timestamp, arbitration ID, DLC, payload and delimiter reads. I'll also add regression coverage using the loop:// serial URL so the tests don't require CAN hardware.
I'm using AI assistance under my direction and will disclose that in the PR. Please let me know if you have a preferred error-message convention or another approach you'd like the patch to follow.
Activity
katarilokesh9-del commentedon Nov 27, 2026
I'd like to prepare a focused fix for this.
I plan to make the serial receiver detect short reads at the point they happen and raise
can.CanOperationErrorwith a clear message, including truncated timestamp, arbitration ID, DLC, payload and delimiter reads. I'll also add regression coverage using theloop://serial URL so the tests don't require CAN hardware.I'm using AI assistance under my direction and will disclose that in the PR. Please let me know if you have a preferred error-message convention or another approach you'd like the patch to follow.