Skip to content

Commit

Permalink
fix: race with InvalidStateError when async_request times out (#1208)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco committed Aug 3, 2023
1 parent 063b5d9 commit 2233b6b
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/zeroconf/_services/info.py
Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 2233b6b

Please sign in to comment.