fix: race with InvalidStateError when async_request times out (#1208) · python-zeroconf/python-zeroconf@2233b6b

Original file line numberDiff line numberDiff line change

@@ -89,6 +89,12 @@ def instance_name_from_service_info(info: "ServiceInfo") -> str:

8989

_cached_ip_addresses = lru_cache(maxsize=256)(ip_address)

9090
9191
92+

def _set_future_none_if_not_done(fut: asyncio.Future) -> None:

93+

"""Set a future to None if it is not done."""

94+

if not fut.done(): # pragma: no branch

95+

fut.set_result(None)

96+
97+
9298

class ServiceInfo(RecordUpdateListener):

9399

"""Service information.

94100

@@ -235,7 +241,7 @@ async def async_wait(self, timeout: float) -> None:

235241

loop = asyncio.get_running_loop()

236242

future = loop.create_future()

237243

self._new_records_futures.append(future)

238-

handle = loop.call_later(millis_to_seconds(timeout), future.set_result, None)

244+

handle = loop.call_later(millis_to_seconds(timeout), _set_future_none_if_not_done, future)

239245

try:

240246

await future

241247

finally: