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 摘要。