fix racing condition

This commit is contained in:
Feng Wang 2024-12-22 19:51:58 +08:00
parent 463216d866
commit d13a6bfb11

View File

@ -491,6 +491,7 @@ class MIoTLan:
_profile_models: dict[str, dict] _profile_models: dict[str, dict]
_init_lock: asyncio.Lock
_init_done: bool _init_done: bool
# The following should be called from the main loop # The following should be called from the main loop
@ -547,6 +548,7 @@ class MIoTLan:
self._lan_state_sub_map = {} self._lan_state_sub_map = {}
self._lan_ctrl_vote_map = {} self._lan_ctrl_vote_map = {}
self._init_lock = asyncio.Lock()
self._init_done = False self._init_done = False
if ( if (
@ -571,6 +573,8 @@ class MIoTLan:
return self._init_done return self._init_done
async def init_async(self) -> None: async def init_async(self) -> None:
# Avoid race condition
async with self._init_lock:
if self._init_done: if self._init_done:
_LOGGER.info('miot lan already init') _LOGGER.info('miot lan already init')
return return
@ -1347,7 +1351,7 @@ class MIoTLan:
scan_time = self.__get_next_scan_time() scan_time = self.__get_next_scan_time()
self._scan_timer = self._internal_loop.call_later( self._scan_timer = self._internal_loop.call_later(
scan_time, self.__scan_devices) scan_time, self.__scan_devices)
_LOGGER.debug('next scan time: %sms', scan_time) _LOGGER.debug('next scan time: %ss', scan_time)
def __get_next_scan_time(self) -> float: def __get_next_scan_time(self) -> float:
if not self._last_scan_interval: if not self._last_scan_interval: