Fix bug in timeout util related to multiple global freezes (#122466)

This commit is contained in:
Erik Montnemery 2024-07-29 10:12:18 +02:00 committed by GitHub
parent 5f08883227
commit 9b497aebb4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 30 additions and 11 deletions

View File

@ -61,18 +61,16 @@ class _GlobalFreezeContext:
def _enter(self) -> None:
"""Run freeze."""
if not self._manager.freezes_done:
return
if self._manager.freezes_done:
# Global reset
for task in self._manager.global_tasks:
task.pause()
# Global reset
for task in self._manager.global_tasks:
task.pause()
# Zones reset
for zone in self._manager.zones.values():
if not zone.freezes_done:
continue
zone.pause()
# Zones reset
for zone in self._manager.zones.values():
if not zone.freezes_done:
continue
zone.pause()
self._manager.global_freezes.append(self)

View File

@ -338,3 +338,24 @@ async def test_simple_zone_timeout_zone_with_timeout_exeption() -> None:
raise RuntimeError
await asyncio.sleep(0.3)
async def test_multiple_global_freezes(hass: HomeAssistant) -> None:
"""Test multiple global freezes."""
timeout = TimeoutManager()
async def background(delay: float) -> None:
async with timeout.async_freeze():
await asyncio.sleep(delay)
async with timeout.async_timeout(0.1):
task = hass.async_create_task(background(0.2))
async with timeout.async_freeze():
await asyncio.sleep(0.1)
await task
async with timeout.async_timeout(0.1):
task = hass.async_create_task(background(0.2))
async with timeout.async_freeze():
await asyncio.sleep(0.3)
await task