Skip to content

Commit 43835e1

Browse files
committed
fixed fatal error on mqtt reconnect: tried to re-acquire shutdown inhibitor lock
1 parent f1371e1 commit 43835e1

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
- home assistant: enable [automatic discovery](https://www.home-assistant.io/docs/mqtt/discovery/#discovery_prefix)
1010
for logind's `PreparingForShutdown` signal
1111

12+
### Fixed
13+
- fatal error on MQTT reconnect:
14+
tried to re-acquire shutdown inhibitor lock
15+
1216
## [0.2.0] - 2020-06-21
1317
### Added
1418
- forward logind's [PreparingForShutdown](https://www.freedesktop.org/wiki/Software/systemd/inhibit/)

systemctl_mqtt/__init__.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ def __init__(
6161
def mqtt_topic_prefix(self) -> str:
6262
return self._mqtt_topic_prefix
6363

64+
@property
65+
def shutdown_lock_acquired(self) -> bool:
66+
return self._shutdown_lock is not None
67+
6468
def acquire_shutdown_lock(self) -> None:
6569
with self._shutdown_lock_mutex:
6670
assert self._shutdown_lock is None
@@ -234,7 +238,8 @@ def _mqtt_on_connect(
234238
assert return_code == 0, return_code # connection accepted
235239
mqtt_broker_host, mqtt_broker_port = mqtt_client.socket().getpeername()
236240
_LOGGER.debug("connected to MQTT broker %s:%d", mqtt_broker_host, mqtt_broker_port)
237-
state.acquire_shutdown_lock()
241+
if not state.shutdown_lock_acquired:
242+
state.acquire_shutdown_lock()
238243
state.register_prepare_for_shutdown_handler(mqtt_client=mqtt_client)
239244
state.publish_preparing_for_shutdown(mqtt_client=mqtt_client)
240245
state.publish_preparing_for_shutdown_homeassistant_config(mqtt_client=mqtt_client)
@@ -246,7 +251,7 @@ def _mqtt_on_connect(
246251
sub=topic, callback=action.mqtt_message_callback
247252
)
248253
_LOGGER.debug(
249-
"registered MQTT callback for topic %s triggering %r", topic, action.action
254+
"registered MQTT callback for topic %s triggering %r", topic, action.action,
250255
)
251256

252257

0 commit comments

Comments
 (0)