diff --git a/src/zeroconf/_services/info.pxd b/src/zeroconf/_services/info.pxd index 223883165..dcfc3a8fe 100644 --- a/src/zeroconf/_services/info.pxd +++ b/src/zeroconf/_services/info.pxd @@ -107,3 +107,5 @@ cdef class ServiceInfo(RecordUpdateListener): @cython.locals(cacheable=cython.bint) cdef cython.set _get_address_and_nsec_records(self, object override_ttl) + + cpdef async_clear_cache(self) diff --git a/src/zeroconf/_services/info.py b/src/zeroconf/_services/info.py index 0600d5d34..ee033c823 100644 --- a/src/zeroconf/_services/info.py +++ b/src/zeroconf/_services/info.py @@ -273,6 +273,14 @@ def properties(self) -> Dict[Union[str, bytes], Optional[Union[str, bytes]]]: assert self._properties is not None return self._properties + def async_clear_cache(self) -> None: + """Clear the cache for this service info.""" + self._dns_address_cache = None + self._dns_pointer_cache = None + self._dns_service_cache = None + self._dns_text_cache = None + self._get_address_and_nsec_records_cache = None + async def async_wait(self, timeout: float, loop: Optional[asyncio.AbstractEventLoop] = None) -> None: """Calling task waits for a given number of milliseconds or until notified.""" if not self._new_records_futures: diff --git a/src/zeroconf/_services/registry.py b/src/zeroconf/_services/registry.py index 12051275e..e9dc4a62b 100644 --- a/src/zeroconf/_services/registry.py +++ b/src/zeroconf/_services/registry.py @@ -91,6 +91,7 @@ def _add(self, info: ServiceInfo) -> None: if info.key in self._services: raise ServiceNameAlreadyRegistered + info.async_clear_cache() self._services[info.key] = info self.types.setdefault(info.type.lower(), []).append(info.key) self.servers.setdefault(info.server_key, []).append(info.key) diff --git a/tests/test_asyncio.py b/tests/test_asyncio.py index d77e7e832..25dd4681d 100644 --- a/tests/test_asyncio.py +++ b/tests/test_asyncio.py @@ -171,6 +171,12 @@ def update_service(self, zeroconf: Zeroconf, type: str, name: str) -> None: ) task = await aiozc.async_update_service(new_info) await task + assert new_info.dns_service().server_key == "ash-2.local." + new_info.server = "ash-3.local." + task = await aiozc.async_update_service(new_info) + await task + assert new_info.dns_service().server_key == "ash-3.local." + task = await aiozc.async_unregister_service(new_info) await task await aiozc.async_close() @@ -178,6 +184,7 @@ def update_service(self, zeroconf: Zeroconf, type: str, name: str) -> None: assert calls == [ ('add', type_, registration_name), ('update', type_, registration_name), + ('update', type_, registration_name), ('remove', type_, registration_name), ]