Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not stop notifier if exception was handled #1645

Merged
merged 1 commit into from
Aug 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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