Browse Source

fixed fatal error on mqtt reconnect: tried to re-acquire shutdown inhibitor lock

Fabian Peter Hammerle 3 weeks ago
parent
commit
43835e1fc9
2 changed files with 11 additions and 2 deletions
  1. 4 0
      CHANGELOG.md
  2. 7 2
      systemctl_mqtt/__init__.py

+ 4 - 0
CHANGELOG.md

@@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 - home assistant: enable [automatic discovery](https://www.home-assistant.io/docs/mqtt/discovery/#discovery_prefix)
   for logind's `PreparingForShutdown` signal
 
+### Fixed
+- fatal error on MQTT reconnect:
+  tried to re-acquire shutdown inhibitor lock
+
 ## [0.2.0] - 2020-06-21
 ### Added
 - forward logind's [PreparingForShutdown](https://www.freedesktop.org/wiki/Software/systemd/inhibit/)

+ 7 - 2
systemctl_mqtt/__init__.py

@@ -61,6 +61,10 @@ class _State:
     def mqtt_topic_prefix(self) -> str:
         return self._mqtt_topic_prefix
 
+    @property
+    def shutdown_lock_acquired(self) -> bool:
+        return self._shutdown_lock is not None
+
     def acquire_shutdown_lock(self) -> None:
         with self._shutdown_lock_mutex:
             assert self._shutdown_lock is None
@@ -234,7 +238,8 @@ def _mqtt_on_connect(
     assert return_code == 0, return_code  # connection accepted
     mqtt_broker_host, mqtt_broker_port = mqtt_client.socket().getpeername()
     _LOGGER.debug("connected to MQTT broker %s:%d", mqtt_broker_host, mqtt_broker_port)
-    state.acquire_shutdown_lock()
+    if not state.shutdown_lock_acquired:
+        state.acquire_shutdown_lock()
     state.register_prepare_for_shutdown_handler(mqtt_client=mqtt_client)
     state.publish_preparing_for_shutdown(mqtt_client=mqtt_client)
     state.publish_preparing_for_shutdown_homeassistant_config(mqtt_client=mqtt_client)
@@ -246,7 +251,7 @@ def _mqtt_on_connect(
             sub=topic, callback=action.mqtt_message_callback
         )
         _LOGGER.debug(
-            "registered MQTT callback for topic %s triggering %r", topic, action.action
+            "registered MQTT callback for topic %s triggering %r", topic, action.action,
         )