Skip to content

Commit

Permalink
Fix applying mqtt.reconnect_count by reordering except clauses (#331)
Browse files Browse the repository at this point in the history
* Fix applying reconnect_count

reconnect_count is not applied on server disconnect because exception caught before except clause to handle reconnect.

* ignore pylint warning "Missing library stubs or py.typed marker"for asyncio_mqtt.error

---------

Co-authored-by: Benji <[email protected]>
  • Loading branch information
zzeekk and BenjiU authored Feb 26, 2024
1 parent fe3a537 commit 2c9da19
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions mqtt_io/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from hashlib import sha1
from importlib import import_module
from typing import Any, Dict, List, Optional, Tuple, Type, Union, overload
from asyncio_mqtt.error import MqttCodeError # type: ignore

import backoff # type: ignore
from typing_extensions import Literal
Expand Down Expand Up @@ -1394,21 +1395,16 @@ async def _main_loop(self) -> None:
if self.config["mqtt"].get("ha_discovery", {}).get("enabled"):
self._ha_discovery_announce()

try:
await asyncio.gather(*self.critical_tasks)
except asyncio.CancelledError:
break
except Exception: # pylint: disable=broad-except
#_LOG.exception("Exception in critical task:")
_LOG.error("Exception in critical task")
await asyncio.gather(*self.critical_tasks)
except asyncio.CancelledError:
break
except MQTTException:
except (MQTTException,MqttCodeError):
if reconnects_remaining is not None:
reconnect = reconnects_remaining > 0
reconnects_remaining -= 1
#_LOG.exception("Connection to MQTT broker failed")
_LOG.error("Connection to MQTT broker failed")
_LOG.exception("Connection to MQTT broker failed")
except Exception: # pylint: disable=broad-except
_LOG.exception("Exception in critical task:")

finally:
_LOG.debug("Clearing events and cancelling 'critical_tasks'")
Expand Down

0 comments on commit 2c9da19

Please sign in to comment.