serial: a frame cut off by the read timeout raises struct.error / TypeError instead of CanOperationError
维护者通常 1 天内回复
评估
- 难度
- 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 内容生成。
描述
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 byteswhen the timestamp or arbitration ID is shortTypeError: ord() expected a character, but string of length 0 foundwhen 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
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
hardbyte/python-can 的其他 Issue
-
bug
难度 1/5 1 小时以内 新手友好度 68/100
hardbyte/python-can#1922 · 1 个 reaction ·
维护者通常 1 天内回复
-
enhancement
难度 5/5 一周以上 新手友好度 25/100
hardbyte/python-can#2159 · 2 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 35/100
hardbyte/python-can#2121 · 2 个 reaction ·
维护者通常 1 天内回复
-
Make socket binding exceptions more debugable可能已有人在做 @vonpanda 于 57 天前认领。 未关闭enhancement
难度 3/5 1-2 天 新手友好度 75/100
hardbyte/python-can#2104 ·
维护者通常 1 天内回复
-
enhancement
难度 5/5 一周以上 新手友好度 30/100
hardbyte/python-can#2102 ·
维护者通常 1 天内回复
查看 hardbyte/python-can 的全部 Issue
相似的 Issue
-
essreduce
难度 2/5 1-3 小时 新手友好度 69/100
维护者通常 1 天内回复
-
[PORT] A2A: map audio file parts to audio content and ignore MIME parameters (TS→Py)可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭area-a2a bug port python
难度 2/5 1-3 小时 新手友好度 68/100
strands-agents/harness-sdk#6697 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 76/100
TigreGotico/orthography2ipa#2950 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
TigreGotico/text2spoken#929 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100