diff --git a/src/cfclient/gui.py b/src/cfclient/gui.py index 31d622de..19432a7a 100644 --- a/src/cfclient/gui.py +++ b/src/cfclient/gui.py @@ -68,8 +68,6 @@ def _task_done_callback(task: asyncio.Task[object]) -> None: return try: task.result() - except DisconnectedError: - logger.debug("Task interrupted by disconnect: %s", task) except Exception: import traceback @@ -77,14 +75,22 @@ def _task_done_callback(task: asyncio.Task[object]) -> None: os._exit(1) +async def _wrap_disconnected(coro: Coroutine[object, object, object]) -> None: + try: + await coro + except DisconnectedError: + logger.debug("Task interrupted by disconnect: %r", coro) + + def create_task(coro: Coroutine[object, object, object]) -> asyncio.Task[object]: """Schedule a coroutine as a task with automatic exception logging. Use this instead of asyncio.ensure_future() to ensure exceptions - are logged immediately rather than silently swallowed. + are logged immediately rather than silently swallowed. DisconnectedError + is treated as a normal shutdown case and is logged at debug level only. """ logger.debug(f"create_task: {coro}") - task = asyncio.ensure_future(coro) + task = asyncio.ensure_future(_wrap_disconnected(coro)) task.add_done_callback(_task_done_callback) return task diff --git a/src/cfclient/ui/main.py b/src/cfclient/ui/main.py index ea86961a..77dcc006 100644 --- a/src/cfclient/ui/main.py +++ b/src/cfclient/ui/main.py @@ -616,8 +616,6 @@ async def _stream_battery(self, cf: Crazyflie) -> None: self._battery_signal.emit( data.data["pm.vbat"], int(data.data["pm.state"]) ) - except DisconnectedError: - pass finally: if stream is not None: try: diff --git a/src/cfclient/ui/pose_logger.py b/src/cfclient/ui/pose_logger.py index fa8302a3..9000f0dc 100644 --- a/src/cfclient/ui/pose_logger.py +++ b/src/cfclient/ui/pose_logger.py @@ -120,8 +120,6 @@ async def _stream_loop(self, cf: Crazyflie) -> None: data.data[self.LOG_NAME_ESTIMATE_YAW], ) self.data_received_cb.call(self, self.pose) - except DisconnectedError: - pass finally: if stream is not None: try: diff --git a/src/cfclient/ui/tabs/FlightTab.py b/src/cfclient/ui/tabs/FlightTab.py index 5464c7a3..2f8c4889 100644 --- a/src/cfclient/ui/tabs/FlightTab.py +++ b/src/cfclient/ui/tabs/FlightTab.py @@ -564,8 +564,6 @@ async def _stream_motors(self, cf: Crazyflie) -> None: while True: data = await stream.next() self._log_data_received(data.timestamp, data.data) - except DisconnectedError: - pass finally: if stream is not None: try: