From 90e4eeb1d43d31f9ec1f6766fba5b0e837a008ac Mon Sep 17 00:00:00 2001 From: Robert Resch Date: Tue, 11 Jul 2023 20:49:21 +0200 Subject: [PATCH 1/3] don't suppress exception on aexit --- aiomqtt/client.py | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/aiomqtt/client.py b/aiomqtt/client.py index 87d0cd9..97f7fb9 100644 --- a/aiomqtt/client.py +++ b/aiomqtt/client.py @@ -511,15 +511,19 @@ async def connect(self, *, timeout: float | None = None) -> None: if self._disconnected.done(): self._disconnected = asyncio.Future() - async def disconnect(self, *, timeout: float | None = None) -> None: - """Disconnect from the broker.""" - # Early out if already disconnected... + def _early_out_on_disconnected(self)-> bool: + # Early out if already disconnected... if self._disconnected.done(): - disc_exc = self._disconnected.exception() - if disc_exc is not None: + if (disc_exc:= self._disconnected.exception()) is not None: # ...by raising the error that caused the disconnect raise disc_exc # ...by returning since the disconnect was intentional + return True + return False + + async def disconnect(self, *, timeout: float | None = None) -> None: + """Disconnect from the broker.""" + if self._early_out_on_disconnected(): return # Try to gracefully disconnect from the broker rc = self._client.disconnect() @@ -1037,13 +1041,16 @@ async def __aexit__( ) -> None: """Try to gracefully disconnect from the broker.""" try: - await self.disconnect() - except MqttError as error: - # We tried to be graceful. Now there is no mercy. - self._logger.warning( - f'Could not gracefully disconnect due to "{error}". Forcing' - " disconnection." - ) + if self._early_out_on_disconnected(): + return + try: + await self.disconnect() + except MqttError as error: + # We tried to be graceful. Now there is no mercy. + self._logger.warning( + f'Could not gracefully disconnect due to "{error}". Forcing' + " disconnection." + ) finally: self._force_disconnect() self._lock.release() From a35dad7f23d47c4ab509bb83feb83a1000936832 Mon Sep 17 00:00:00 2001 From: Robert Resch Date: Thu, 13 Jul 2023 16:59:41 +0200 Subject: [PATCH 2/3] fix py3.7 --- aiomqtt/client.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/aiomqtt/client.py b/aiomqtt/client.py index 97f7fb9..4eac715 100644 --- a/aiomqtt/client.py +++ b/aiomqtt/client.py @@ -514,7 +514,8 @@ async def connect(self, *, timeout: float | None = None) -> None: def _early_out_on_disconnected(self)-> bool: # Early out if already disconnected... if self._disconnected.done(): - if (disc_exc:= self._disconnected.exception()) is not None: + disc_exc = self._disconnected.exception() + if disc_exc is not None: # ...by raising the error that caused the disconnect raise disc_exc # ...by returning since the disconnect was intentional From 3ef2c231dddbf3ceff52fad03693bf01d50f1e12 Mon Sep 17 00:00:00 2001 From: Robert Resch Date: Sat, 15 Jul 2023 10:37:24 +0200 Subject: [PATCH 3/3] fix formatting --- aiomqtt/client.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/aiomqtt/client.py b/aiomqtt/client.py index 4eac715..46b0a02 100644 --- a/aiomqtt/client.py +++ b/aiomqtt/client.py @@ -511,8 +511,8 @@ async def connect(self, *, timeout: float | None = None) -> None: if self._disconnected.done(): self._disconnected = asyncio.Future() - def _early_out_on_disconnected(self)-> bool: - # Early out if already disconnected... + def _early_out_on_disconnected(self) -> bool: + # Early out if already disconnected... if self._disconnected.done(): disc_exc = self._disconnected.exception() if disc_exc is not None: