From 2233b6bc4ceeee5524d2ee88ecae8234173feb5f Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 2 Aug 2023 21:36:52 -1000 Subject: [PATCH] fix: race with InvalidStateError when async_request times out (#1208) --- src/zeroconf/_services/info.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/zeroconf/_services/info.py b/src/zeroconf/_services/info.py index cc1db05f..b75d6277 100644 --- a/src/zeroconf/_services/info.py +++ b/src/zeroconf/_services/info.py @@ -89,6 +89,12 @@ def instance_name_from_service_info(info: "ServiceInfo") -> str: _cached_ip_addresses = lru_cache(maxsize=256)(ip_address) +def _set_future_none_if_not_done(fut: asyncio.Future) -> None: + """Set a future to None if it is not done.""" + if not fut.done(): # pragma: no branch + fut.set_result(None) + + class ServiceInfo(RecordUpdateListener): """Service information. @@ -235,7 +241,7 @@ async def async_wait(self, timeout: float) -> None: loop = asyncio.get_running_loop() future = loop.create_future() self._new_records_futures.append(future) - handle = loop.call_later(millis_to_seconds(timeout), future.set_result, None) + handle = loop.call_later(millis_to_seconds(timeout), _set_future_none_if_not_done, future) try: await future finally: