diff --git a/src/gallia/services/uds/ecu.py b/src/gallia/services/uds/ecu.py index 1f96f17ba..34f16406b 100644 --- a/src/gallia/services/uds/ecu.py +++ b/src/gallia/services/uds/ecu.py @@ -370,18 +370,22 @@ async def wait_for_ecu( async def _tester_present_worker(self, interval: float) -> None: assert self.transport logger.debug("tester present worker started") - while True: + task = asyncio.current_task() + while task is not None and task.cancelling() == 0: try: await asyncio.sleep(interval) # TODO Only ping if there was no other UDS traffic for `interval` amount of time await self.ping(UDSRequestConfig(max_retry=1)) except asyncio.CancelledError: logger.debug("tester present worker terminated") - break + raise except ConnectionError: logger.info("connection lost; tester present waiting…") except Exception as e: logger.warning(f"Tester present worker got {e!r}") + logger.debug( + "Tester present worker was cancelled but received no asyncio.CancelledError" + ) async def start_cyclic_tester_present(self, interval: float) -> None: logger.debug("Starting tester present worker") @@ -402,12 +406,11 @@ async def stop_cyclic_tester_present(self) -> None: ) return - running = True - while running: - running = self.tester_present_task.cancel() - logger.debug("Cancelled tester present task") - await asyncio.sleep(0) - await self.tester_present_task + self.tester_present_task.cancel() + try: + await self.tester_present_task + except asyncio.CancelledError: + pass async def update_state( self, request: service.UDSRequest, response: service.UDSResponse