Skip to content

Commit

Permalink
fix: ensure exceptions are logged when no reply is expected (#342)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Jan 7, 2025
1 parent 439b2da commit 1c20dcc
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/dbus_fast/aio/message_bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from ..errors import AuthError
from ..message import Message
from ..message_bus import BaseMessageBus, _block_unexpected_reply
from ..service import ServiceInterface
from ..service import ServiceInterface, _Method
from .message_reader import build_message_reader
from .proxy_object import ProxyObject

Expand Down Expand Up @@ -423,7 +423,19 @@ async def wait_for_disconnect(self):
"""
return await self._disconnect_future

def _make_method_handler(self, interface, method):
def _future_exception_no_reply(self, fut: asyncio.Future) -> None:
"""Log an exception from a future that was not expected."""
self._pending_futures.discard(fut)
try:
fut.result()
except asyncio.CancelledError:
pass
except Exception as e:
logging.error("unexpected exception in future", exc_info=e)

def _make_method_handler(
self, interface: "ServiceInterface", method: "_Method"
) -> Callable[[Message, Callable[[Message], None]], None]:
if not asyncio.iscoroutinefunction(method.fn):
return super()._make_method_handler(interface, method)

Expand All @@ -436,15 +448,15 @@ def _coroutine_method_handler(
) -> None:
"""A coroutine method handler."""
args = msg_body_to_args(msg) if msg.unix_fds else msg.body
fut = asyncio.ensure_future(method.fn(interface, *args))
fut: asyncio.Future = asyncio.ensure_future(method.fn(interface, *args))
# Hold a strong reference to the future to ensure
# it is not garbage collected before it is done.
self._pending_futures.add(fut)
if (
send_reply is _block_unexpected_reply
or msg.flags.value & NO_REPLY_EXPECTED_VALUE
):
fut.add_done_callback(self._pending_futures.discard)
fut.add_done_callback(self._future_exception_no_reply)
return

# We only create the closure function if we are actually going to reply
Expand Down

0 comments on commit 1c20dcc

Please sign in to comment.