Skip to content

Commit

Permalink
Fix blocking IO calls in mqtt client setup (home-assistant#119647)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbouwh authored Jun 13, 2024
1 parent cd80b9b commit a992654
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
2 changes: 1 addition & 1 deletion homeassistant/components/mqtt/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class AsyncMQTTClient(MQTTClient):
that is not needed since we are running in an async event loop.
"""

def async_setup(self) -> None:
def setup(self) -> None:
"""Set up the client.
All the threading locks are replaced with NullLock
Expand Down
29 changes: 24 additions & 5 deletions homeassistant/components/mqtt/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,16 +277,30 @@ class Subscription:
class MqttClientSetup:
"""Helper class to setup the paho mqtt client from config."""

_client: AsyncMQTTClient

def __init__(self, config: ConfigType) -> None:
"""Initialize the MQTT client setup helper."""
"""Initialize the MQTT client setup helper.
self.setup must be run in an executor job.
"""

self._config = config

def setup(self) -> None:
"""Set up the MQTT client.
The setup of the MQTT client should be run in an executor job,
because it accesses files, so it does IO.
"""
# We don't import on the top because some integrations
# should be able to optionally rely on MQTT.
import paho.mqtt.client as mqtt # pylint: disable=import-outside-toplevel

# pylint: disable-next=import-outside-toplevel
from .async_client import AsyncMQTTClient

config = self._config
if (protocol := config.get(CONF_PROTOCOL, DEFAULT_PROTOCOL)) == PROTOCOL_31:
proto = mqtt.MQTTv31
elif protocol == PROTOCOL_5:
Expand All @@ -298,11 +312,14 @@ def __init__(self, config: ConfigType) -> None:
# PAHO MQTT relies on the MQTT server to generate random client IDs.
# However, that feature is not mandatory so we generate our own.
client_id = mqtt.base62(uuid.uuid4().int, padding=22)
transport = config.get(CONF_TRANSPORT, DEFAULT_TRANSPORT)
transport: str = config.get(CONF_TRANSPORT, DEFAULT_TRANSPORT)
self._client = AsyncMQTTClient(
client_id, protocol=proto, transport=transport, reconnect_on_failure=False
client_id,
protocol=proto,
transport=transport,
reconnect_on_failure=False,
)
self._client.async_setup()
self._client.setup()

# Enable logging
self._client.enable_logger()
Expand Down Expand Up @@ -544,7 +561,9 @@ async def async_init_client(self) -> None:
self.hass, "homeassistant.components.mqtt.async_client"
)

mqttc = MqttClientSetup(self.conf).client
mqttc_setup = MqttClientSetup(self.conf)
await self.hass.async_add_executor_job(mqttc_setup.setup)
mqttc = mqttc_setup.client
# on_socket_unregister_write and _async_on_socket_close
# are only ever called in the event loop
mqttc.on_socket_close = self._async_on_socket_close
Expand Down
4 changes: 3 additions & 1 deletion homeassistant/components/mqtt/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,9 @@ def try_connection(
# should be able to optionally rely on MQTT.
import paho.mqtt.client as mqtt # pylint: disable=import-outside-toplevel

client = MqttClientSetup(user_input).client
mqtt_client_setup = MqttClientSetup(user_input)
mqtt_client_setup.setup()
client = mqtt_client_setup.client

result: queue.Queue[bool] = queue.Queue(maxsize=1)

Expand Down

0 comments on commit a992654

Please sign in to comment.