Fix race condition in deconz (#91328)

* Fix race condition in deconz

* Use a new separate test
This commit is contained in:
epenet 2023-04-13 15:04:21 +02:00 committed by GitHub
parent 8ca3440f33
commit c1b7aa084c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 2 deletions

View File

@ -235,9 +235,15 @@ class DeconzGateway:
) -> None: ) -> None:
"""Handle signals of config entry being updated. """Handle signals of config entry being updated.
This is a static method because a class method (bound method), cannot be used with weak references. This is a static method because a class method (bound method),
Causes for this is either discovery updating host address or config entry options changing. cannot be used with weak references.
Causes for this is either discovery updating host address or
config entry options changing.
""" """
if entry.entry_id not in hass.data[DECONZ_DOMAIN]:
# A race condition can occur if multiple config entries are
# unloaded in parallel
return
gateway = get_gateway_from_config_entry(hass, entry) gateway = get_gateway_from_config_entry(hass, entry)
if gateway.api.host != gateway.host: if gateway.api.host != gateway.host:

View File

@ -1,4 +1,5 @@
"""Test deCONZ component setup process.""" """Test deCONZ component setup process."""
import asyncio
from unittest.mock import patch from unittest.mock import patch
from homeassistant.components.deconz import ( from homeassistant.components.deconz import (
@ -133,6 +134,31 @@ async def test_unload_entry_multiple_gateways(
assert hass.data[DECONZ_DOMAIN][config_entry2.entry_id].master assert hass.data[DECONZ_DOMAIN][config_entry2.entry_id].master
async def test_unload_entry_multiple_gateways_parallel(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None:
"""Test race condition when unloading multiple config entries in parallel."""
config_entry = await setup_deconz_integration(hass, aioclient_mock)
aioclient_mock.clear_requests()
data = {"config": {"bridgeid": "01234E56789B"}}
with patch.dict(DECONZ_WEB_REQUEST, data):
config_entry2 = await setup_deconz_integration(
hass,
aioclient_mock,
entry_id="2",
unique_id="01234E56789B",
)
assert len(hass.data[DECONZ_DOMAIN]) == 2
await asyncio.gather(
config_entry.async_unload(hass), config_entry2.async_unload(hass)
)
assert len(hass.data[DECONZ_DOMAIN]) == 0
async def test_update_group_unique_id(hass: HomeAssistant) -> None: async def test_update_group_unique_id(hass: HomeAssistant) -> None:
"""Test successful migration of entry data.""" """Test successful migration of entry data."""
old_unique_id = "123" old_unique_id = "123"