Skip to content

Commit

Permalink
do not stop notifier if exception was handled
Browse files Browse the repository at this point in the history
  • Loading branch information
zariiii9003 committed Aug 22, 2023
1 parent fcf615d commit ecaaa61
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions can/notifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
"""

import asyncio
import functools
import logging
import threading
import time
from typing import Awaitable, Callable, Iterable, List, Optional, Union
from typing import Awaitable, Callable, Iterable, List, Optional, Union, cast

from can.bus import BusABC
from can.listener import Listener
Expand Down Expand Up @@ -108,28 +109,33 @@ def stop(self, timeout: float = 5) -> None:
listener.stop()

def _rx_thread(self, bus: BusABC) -> None:
try:
while self._running:
# determine message handling callable early, not inside while loop
handle_message = cast(
Callable[[Message], None],
self._on_message_received
if self._loop is None
else functools.partial(
self._loop.call_soon_threadsafe, self._on_message_received
),
)

while self._running:
try:
if msg := bus.recv(self.timeout):
with self._lock:
if self._loop is not None:
self._loop.call_soon_threadsafe(
self._on_message_received, msg
)
else:
self._on_message_received(msg)
except Exception as exc: # pylint: disable=broad-except
self.exception = exc
if self._loop is not None:
self._loop.call_soon_threadsafe(self._on_error, exc)
# Raise anyway
raise
elif not self._on_error(exc):
# If it was not handled, raise the exception here
raise
else:
# It was handled, so only log it
logger.info("suppressed exception: %s", exc)
handle_message(msg)
except Exception as exc: # pylint: disable=broad-except
self.exception = exc
if self._loop is not None:
self._loop.call_soon_threadsafe(self._on_error, exc)
# Raise anyway
raise
elif not self._on_error(exc):
# If it was not handled, raise the exception here
raise
else:
# It was handled, so only log it
logger.debug("suppressed exception: %s", exc)

def _on_message_available(self, bus: BusABC) -> None:
if msg := bus.recv(0):
Expand Down

0 comments on commit ecaaa61

Please sign in to comment.