serial: a frame cut off by the read timeout raises struct.error / TypeError instead of CanOperationError
Los mantenedores suelen responder en 1 día
Evaluación
- Dificultad
- 2/5
- Tiempo estimado
- 1-3 horas
- Aptitud para principiantes
- 78/100
- Tipo de issue
- Error
- Claridad
- Bien especificado
- Estado de actividad
- Activo
- Stack tecnológico
- python
- Área
- backend-api-design
Línea de trabajo
Empieza en can/interfaces/serial/serial_can.py, en SerialBus._recv_internal, y sigue cómo se procesa cada campo leído antes de struct.unpack u ord(). Revisa las pruebas existentes para la interfaz serie y añade cobertura para los campos truncados y el caso de un DLC demasiado grande. El trabajo estará hecho cuando estos errores se manifiesten como can.CanOperationError (o se descarten con una advertencia), en lugar de propagarse como struct.error, TypeError o ValueError.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
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.
- Lenguaje dominante
- Python
- Estrellas
- 1.6k
- Forks
- 715
- Merge medio
- 2 d 4 h
- PR fusionados (30 d)
- 42
Preparar el entorno
- Sin Dockerfile ni archivo de Docker Compose
- Tiene una plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de hardbyte/python-can
-
bug
Dificultad 1/5 Menos de una hora Aptitud para principiantes 68/100
hardbyte/python-can#1922 · 1 reacción ·
Los mantenedores suelen responder en 1 día
-
enhancement
Dificultad 5/5 Más de una semana Aptitud para principiantes 25/100
hardbyte/python-can#2159 · 2 comentarios ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 35/100
hardbyte/python-can#2121 · 2 reacciones ·
Los mantenedores suelen responder en 1 día
-
Make socket binding exceptions more debugablePosiblemente ocupada @vonpanda la tomó hace 57 días. Abiertoenhancement
Dificultad 3/5 1-2 días Aptitud para principiantes 75/100
hardbyte/python-can#2104 ·
Los mantenedores suelen responder en 1 día
-
enhancement
Dificultad 5/5 Más de una semana Aptitud para principiantes 30/100
hardbyte/python-can#2102 ·
Los mantenedores suelen responder en 1 día
Todos los issues de hardbyte/python-can
Issues similares
-
essreduce
Dificultad 2/5 1-3 horas Aptitud para principiantes 69/100
Los mantenedores suelen responder en 1 día
-
area-a2a bug port python
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
strands-agents/harness-sdk#6697 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
TigreGotico/orthography2ipa#2950 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
TigreGotico/text2spoken#929 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100