Avoid race condition with UDP voice server starting in ESPHome (#111644)

* Avoid race condition with UDP server starting

* Fix test
pull/111649/head
Michael Hansen 2024-02-27 10:54:56 -06:00 committed by GitHub
parent 00d11ff68e
commit 890e651bdd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 6 deletions

View File

@ -160,10 +160,10 @@ class VoiceAssistantUDPServer(asyncio.DatagramProtocol):
async def _iterate_packets(self) -> AsyncIterable[bytes]:
"""Iterate over incoming packets."""
if not self.is_running:
raise RuntimeError("Not running")
while data := await self.queue.get():
if not self.is_running:
break
yield data
def _event_callback(self, event: PipelineEvent) -> None:

View File

@ -224,9 +224,13 @@ async def test_udp_server_queue(
voice_assistant_udp_server_v1.close()
with pytest.raises(RuntimeError):
async for data in voice_assistant_udp_server_v1._iterate_packets():
assert data == bytes(1024)
# Stopping the UDP server should cause _iterate_packets to break out
# immediately without yielding any data.
has_data = False
async for _data in voice_assistant_udp_server_v1._iterate_packets():
has_data = True
assert not has_data, "Server was stopped"
async def test_error_calls_handle_finished(