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