Async notifier does not handle errors
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Idoneità per principianti
- 38/100
Direzione di ricerca
Inizia in notifier.py con Notifier._on_message_available e AsyncBufferedReader. Traccia il modo in cui gli errori di bus.recv() raggiungono _on_error() e in cui la coroutine in attesa consuma la coda dei messaggi. Il lavoro è completato quando gli errori di ricezione asincroni raggiungono i listener e le coroutine in attesa possono gestirli senza rimanere bloccate, preservando l’ordine dei messaggi.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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!
- Lingua principale
- Python
- Stelle
- 1.6k
- Fork
- 697
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di hardbyte/python-can
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
hardbyte/python-can#2103 ·
-
bug
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 78/100
hardbyte/python-can#2077 · 1 commento · 1 reazione ·
-
bug
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 68/100
hardbyte/python-can#1922 · 1 reazione ·
-
enhancement
Difficoltà 5/5 Più di una settimana Idoneità per principianti 30/100
hardbyte/python-can#2102 ·
-
bug
Difficoltà 3/5 1-2 giorni Idoneità per principianti 68/100
hardbyte/python-can#2092 ·
Tutte le issue di hardbyte/python-can
Issue simili
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
xinnan-tech/xiaozhi-fde-talk#263 ·
-
rules
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 90/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
huggingface/Repo2RLEnv#163 · 1 commento ·
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 95/100
huggingface/sentence-transformers#4074 ·
-
comp/dashboard invalid P3
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
NousResearch/hermes-agent#121143 ·