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

Đang mở Phù hợp với người mới
#2,157 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@aloneangel45 đang làm issue này rồi.

Từ ngày 30/11/2026.

  • #2175 của @aloneangel45 — đang mở

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
78/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python
Lĩnh vực
backend-api-design

Hướng nghiên cứu

Bắt đầu tại can/interfaces/serial/serial_can.py, ở SerialBus._recv_internal, và theo dõi cách xử lý từng trường được đọc trước khi gọi struct.unpack hoặc ord(). Kiểm tra các bài kiểm thử hiện có cho giao diện serial và bổ sung kiểm thử cho các trường bị cắt ngắn và trường hợp DLC quá lớn. Công việc hoàn tất khi các lỗi này xuất hiện dưới dạng can.CanOperationError (hoặc bị loại bỏ kèm cảnh báo), thay vì phát sinh struct.error, TypeError hoặc ValueError không được xử lý.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
Python
Star
1.6k
Fork
715
Merge trung bình
2 ngày 4 giờ
Pull request đã merge (30 ngày)
42

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của hardbyte/python-can

Tất cả issue của hardbyte/python-can

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.