Fix race when waiting for MQTT ACK (#39193)

This commit is contained in:
Erik Montnemery 2020-08-24 11:13:12 +02:00 committed by GitHub
parent 71acb2c665
commit 3df67ff9e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -939,10 +939,9 @@ class MQTT:
self.hass.add_job(self._mqtt_handle_mid, mid)
async def _mqtt_handle_mid(self, mid) -> None:
if mid in self._pending_operations:
self._pending_operations[mid].set()
else:
_LOGGER.warning("Unknown mid %d", mid)
if mid not in self._pending_operations:
self._pending_operations[mid] = asyncio.Event()
self._pending_operations[mid].set()
def _mqtt_on_disconnect(self, _mqttc, _userdata, result_code: int) -> None:
"""Disconnected callback."""
@ -957,7 +956,8 @@ class MQTT:
async def _wait_for_mid(self, mid):
"""Wait for ACK from broker."""
self._pending_operations[mid] = asyncio.Event()
if mid not in self._pending_operations:
self._pending_operations[mid] = asyncio.Event()
try:
await asyncio.wait_for(self._pending_operations[mid].wait(), TIMEOUT_ACK)
except asyncio.TimeoutError: