serial: a frame cut off by the read timeout raises struct.error / TypeError instead of CanOperationError

未关闭 适合新手
#2,157 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@aloneangel45 已经在做这个了。

开始于 2026年11月30日。

  • #2175 来自 @aloneangel45 —— 未关闭

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
78/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
python

调研方向

从 can/interfaces/serial/serial_can.py 中的 SerialBus._recv_internal 开始,追踪在调用 struct.unpack 或 ord() 之前对每个读取字段的处理方式。检查串行接口的现有测试,并补充对字段截断和 DLC 超大情况的覆盖。完成的标准是,这些失败会以 can.CanOperationError 的形式出现(或在发出警告后被丢弃),而不是产生未捕获的 struct.error、TypeError 或 ValueError。

由索引模型根据 Issue 内容生成。

描述

bug
Describe the bug

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:

import struct
from can.interfaces.serial.serial_can import SerialBus

bus = SerialBus("loop://", timeout=0.05)
frame = (bytes([0xAA]) + struct.pack("<I", 0) + bytes([8]) + struct.pack("<I", 0x123)
         + bytes(range(1, 9)) + bytes([0xBB]))

for cut in (3, 6, 12, len(frame) - 1):
    bus._ser.write(frame[:cut])
    try:
        print(cut, bus.recv(timeout=0.05))
    except Exception as e:
        print(cut, type(e).__name__, e)
    bus._ser.reset_input_buffer()
bus.shutdown()

Output on current main (a5947819):

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 (a5947819)
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.

主要语言
Python
星标
1.6k
派生
715
平均合并
2 天 4 小时
30 天内合并 PR
42

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

hardbyte/python-can 的其他 Issue

查看 hardbyte/python-can 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。