serial: a frame cut off by the read timeout raises struct.error / TypeError instead of CanOperationError
Mantenedores costumam responder em até 1 dia
Avaliação
- Dificuldade
- 2/5
- Tempo estimado
- 1-3 horas
- Facilidade para iniciantes
- 78/100
- Tipo de issue
- Bug
- Clareza
- Claramente especificada
- Status de atividade
- Ativa
- Stack de tecnologia
- python
- Domínio
- backend-api-design
Direção de pesquisa
Comece em can/interfaces/serial/serial_can.py, em SerialBus._recv_internal, e acompanhe como cada campo lido é tratado antes de struct.unpack ou ord(). Verifique os testes existentes para a interface serial e adicione cobertura para campos truncados e para o caso de DLC acima do limite. O trabalho estará concluído quando essas falhas forem apresentadas como can.CanOperationError (ou descartadas com um aviso), em vez de propagarem struct.error, TypeError ou ValueError sem tratamento.
Escrita pelo modelo de indexação a partir do texto da issue.
Descrição
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.
- Linguagem predominante
- Python
- Estrelas
- 1.6k
- Forks
- 715
- Merge médio
- 2d 4h
- PRs com merge (30d)
- 42
Preparar o ambiente
- Sem Dockerfile nem arquivo Docker Compose
- Tem um modelo de pull request
- Ler o guia de contribuição
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Mais de hardbyte/python-can
-
bug
Dificuldade 1/5 Menos de uma hora Facilidade para iniciantes 68/100
hardbyte/python-can#1922 · 1 reação ·
Mantenedores costumam responder em até 1 dia
-
enhancement
Dificuldade 5/5 Mais de uma semana Facilidade para iniciantes 25/100
hardbyte/python-can#2159 · 2 comentários ·
Mantenedores costumam responder em até 1 dia
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 35/100
hardbyte/python-can#2121 · 2 reações ·
Mantenedores costumam responder em até 1 dia
-
Make socket binding exceptions more debugableTalvez já em andamento @vonpanda assumiu há 57 dias. Abertaenhancement
Dificuldade 3/5 1-2 dias Facilidade para iniciantes 75/100
hardbyte/python-can#2104 ·
Mantenedores costumam responder em até 1 dia
-
enhancement
Dificuldade 5/5 Mais de uma semana Facilidade para iniciantes 30/100
hardbyte/python-can#2102 ·
Mantenedores costumam responder em até 1 dia
Todas as issues de hardbyte/python-can
Issues semelhantes
-
essreduce
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 69/100
Mantenedores costumam responder em até 1 dia
-
[PORT] A2A: map audio file parts to audio content and ignore MIME parameters (TS→Py)Talvez já em andamento Um pull request vinculado a esta issue está aberto ou já foi mesclado. Abertaarea-a2a bug port python
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 68/100
strands-agents/harness-sdk#6697 · 1 comentário ·
Mantenedores costumam responder em até 1 dia
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 76/100
TigreGotico/orthography2ipa#2950 ·
Mantenedores costumam responder em até 1 dia
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 78/100
TigreGotico/text2spoken#929 ·
Mantenedores costumam responder em até 1 dia
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 78/100