From 295fa734cd273caa4a450593b9d0caed42339d79 Mon Sep 17 00:00:00 2001 From: Martin Hjelmare Date: Sat, 3 Feb 2024 10:42:54 +0100 Subject: [PATCH] Reset protocol also on error during context shutdown --- pytradfri/api/aiocoap_api.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/pytradfri/api/aiocoap_api.py b/pytradfri/api/aiocoap_api.py index e0ace9ea..f171c6eb 100644 --- a/pytradfri/api/aiocoap_api.py +++ b/pytradfri/api/aiocoap_api.py @@ -129,12 +129,14 @@ async def _reset_protocol(self, exc: BaseException | None = None) -> None: # Be responsible and clean up. protocol = await self._get_protocol(check_reset_lock=False) - await protocol.shutdown() - self._protocol = None - # The error callbacks are called when shutting down the protocol. - # Clear the saved callbacks - self._observations_err_callbacks.clear() + try: + await protocol.shutdown() + finally: + self._protocol = None + # The error callbacks are called when shutting down the protocol. + # Clear the saved callbacks + self._observations_err_callbacks.clear() async def shutdown(self, exc: Exception | None = None) -> None: """Shutdown the API events.