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が大きすぎるケースのテストを追加してください。これらの失敗が未処理の struct.error、TypeError、ValueError として伝播せず、can.CanOperationError として表面化する(または警告とともに破棄される)ことが完了の条件です。
索引モデルが 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時間
- マージ済み PR(30日)
- 42
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
hardbyte/python-can のほかの issue
-
bug
難易度 1/5 1時間未満 初心者へのやさしさ 68/100
hardbyte/python-can#1922 · リアクション 1 件 ·
メンテナーはふだん 1 日以内に返信
-
enhancement
難易度 5/5 1週間以上 初心者へのやさしさ 25/100
hardbyte/python-can#2159 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 35/100
hardbyte/python-can#2121 · リアクション 2 件 ·
メンテナーはふだん 1 日以内に返信
-
Make socket binding exceptions more debugable対応中かも @vonpanda が 57 日前に担当しました。 オープンenhancement
難易度 3/5 1〜2日 初心者へのやさしさ 75/100
hardbyte/python-can#2104 ·
メンテナーはふだん 1 日以内に返信
-
enhancement
難易度 5/5 1週間以上 初心者へのやさしさ 30/100
hardbyte/python-can#2102 ·
メンテナーはふだん 1 日以内に返信
hardbyte/python-can の issue をすべて見る
似ている issue
-
essreduce
難易度 2/5 1〜3時間 初心者へのやさしさ 69/100
メンテナーはふだん 1 日以内に返信
-
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