Async notifier does not handle errors
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Anfängerfreundlichkeit
- 38/100
Rechercherichtung
Beginne in notifier.py mit Notifier._on_message_available und AsyncBufferedReader. Verfolge, wie Fehler von bus.recv() _on_error() erreichen und wie die wartende Coroutine die Nachrichtenwarteschlange konsumiert. Die Aufgabe ist abgeschlossen, wenn asynchrone Empfangsfehler die Listener erreichen und wartende Coroutines sie verarbeiten können, ohne weiterhin blockiert zu bleiben, wobei die Nachrichtenreihenfolge erhalten bleibt.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
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!
- Vorherrschende Sprache
- Python
- Sterne
- 1.6k
- Forks
- 697
- PR-Merge-Kennzahlen
- Keine gemergten PRs in 30 T.
Beitragsleitfaden
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus hardbyte/python-can
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 88/100
hardbyte/python-can#2103 ·
-
bug
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 78/100
hardbyte/python-can#2077 · 1 Kommentar · 1 Reaktion ·
-
bug
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 68/100
hardbyte/python-can#1922 · 1 Reaktion ·
-
enhancement
Schwierigkeit 5/5 Über eine Woche Anfängerfreundlichkeit 30/100
hardbyte/python-can#2102 ·
-
bug
Schwierigkeit 3/5 1-2 Tage Anfängerfreundlichkeit 68/100
hardbyte/python-can#2092 ·
Alle Issues in hardbyte/python-can
Ähnliche Issues
-
bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
stephrobert/dsoxlab#238 ·
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
sublimehq/package_control#1780 ·
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 65/100
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 70/100
nwg-piotr/nwg-displays#145 ·