From 6671cbb96dfb4ef41be910f12140b1c34e3b5989 Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Mon, 8 Oct 2018 10:59:43 +0200 Subject: [PATCH] Fix potential MQTT discovery race condition (#17208) * Fix potential MQTT discovery race condition * Rename data key --- homeassistant/components/mqtt/discovery.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/mqtt/discovery.py b/homeassistant/components/mqtt/discovery.py index fdb7948e4bff3..a762978a330c0 100644 --- a/homeassistant/components/mqtt/discovery.py +++ b/homeassistant/components/mqtt/discovery.py @@ -4,6 +4,7 @@ For more details about this component, please refer to the documentation at https://home-assistant.io/components/mqtt/#discovery """ +import asyncio import json import logging import re @@ -51,6 +52,7 @@ } ALREADY_DISCOVERED = 'mqtt_discovered_components' +DATA_CONFIG_ENTRY_LOCK = 'mqtt_config_entry_lock' CONFIG_ENTRY_IS_SETUP = 'mqtt_config_entry_is_setup' MQTT_DISCOVERY_UPDATED = 'mqtt_discovery_updated_{}' MQTT_DISCOVERY_NEW = 'mqtt_discovery_new_{}_{}' @@ -119,14 +121,16 @@ async def async_device_message_received(topic, payload, qos): return config_entries_key = '{}.{}'.format(component, platform) - if config_entries_key not in hass.data[CONFIG_ENTRY_IS_SETUP]: - hass.data[CONFIG_ENTRY_IS_SETUP].add(config_entries_key) - await hass.config_entries.async_forward_entry_setup( - config_entry, component) + async with hass.data[DATA_CONFIG_ENTRY_LOCK]: + if config_entries_key not in hass.data[CONFIG_ENTRY_IS_SETUP]: + await hass.config_entries.async_forward_entry_setup( + config_entry, component) + hass.data[CONFIG_ENTRY_IS_SETUP].add(config_entries_key) async_dispatcher_send(hass, MQTT_DISCOVERY_NEW.format( component, platform), payload) + hass.data[DATA_CONFIG_ENTRY_LOCK] = asyncio.Lock() hass.data[CONFIG_ENTRY_IS_SETUP] = set() await mqtt.async_subscribe(