Async notifier does not handle errors
Ninguém assumiu esta issue ainda.
Avaliação
- Dificuldade
- 3/5
- Tempo estimado
- 1-2 dias
- Facilidade para iniciantes
- 38/100
Direção de pesquisa
Comece em notifier.py com Notifier._on_message_available e AsyncBufferedReader. Rastreie como os erros de bus.recv() chegam a _on_error() e como a coroutine em espera consome a fila de mensagens. Está concluído quando os erros de recebimento assíncronos chegam aos listeners e as coroutines em espera conseguem tratá-los sem permanecer bloqueadas, preservando a ordem das mensagens.
Escrita pelo modelo de indexação a partir do texto da issue.
Descrição
Greetings! I'm using python-can to talk to my inverter's battery.
Describe the bug
In class Notifier:
def _on_message_available(self, bus: BusABC) -> None:
if msg := bus.recv(0):
self._on_message_received(msg)
When an exception occurs, for example the CAN interface going down or being unplugged, bus.recv() raises but the exception is not handled. The threaded version does call _on_error(), so I guess the async version should too. I fixed it by inheriting the class, but these 3 lines of code would be better in notifier.py.
class Notifier_e (can.Notifier):
def _on_message_available(self, bus):
try:
super()._on_message_available(bus)
except Exception as exc:
if not self._on_error(exc):
# If it was not handled, raise the exception here
raise
Then _on_error() passes the exception to the listeners, for example AsyncBufferedReader, but it doesn't handle exceptions either: instead the coroutine waiting for messages simply stays stuck forever. So I extended it in the same way:
class AsyncBufferedReader_e( can.AsyncBufferedReader ):
def on_error( self, exc ):
self.buffer.put_nowait( exc )
async def __anext__(self):
m = await self.buffer.get()
if isinstance( m, BaseException ):
raise m
return m
It puts the exception into the queue with messages, so when the coroutine waiting on these messages gets to the exception, it is raised, and the coroutine can process it. This preserves order between messages and exceptions, so messages received and pushed into the queue before the exception occurs will be processed.
I use it to cleanly disconnect the CAN interface, then connect again when it is plugged back into the USB port.
I'm not sure about Exception vs BaseException. I guess it would be useful to pass KeyboardInterrupt to the waiting coroutine, so it would be terminated in the same way as a Ctrl-C occurring during a blocking recv().
Have a nice day!
- Linguagem predominante
- Python
- Estrelas
- 1.6k
- Forks
- 697
- Métricas de merge de PRs
- Nenhum PR com merge em 30d
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
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 88/100
hardbyte/python-can#2103 ·
-
bug
Dificuldade 1/5 Menos de uma hora Facilidade para iniciantes 78/100
hardbyte/python-can#2077 · 1 comentário · 1 reação ·
-
bug
Dificuldade 1/5 Menos de uma hora Facilidade para iniciantes 68/100
hardbyte/python-can#1922 · 1 reação ·
-
enhancement
Dificuldade 5/5 Mais de uma semana Facilidade para iniciantes 30/100
hardbyte/python-can#2102 ·
-
bug
Dificuldade 3/5 1-2 dias Facilidade para iniciantes 68/100
hardbyte/python-can#2092 ·
Todas as issues de hardbyte/python-can
Issues semelhantes
-
bug confirmed issue
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 75/100
open-webui/open-webui#30750 · 1 comentário ·
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 75/100
-
enhancement
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 75/100
OpenwaterHealth/openmotion-bloodflow-app#604 · 1 comentário ·
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 70/100
-
good first issue
Dificuldade 1/5 Menos de uma hora Facilidade para iniciantes 90/100