Skip to content

Commit 2060eb2

Browse files
authored
fix: ensure ServiceInfo cache is cleared when adding to the registry (#1279)
* There were production use cases that mutated the service info and re-registered it that need to be accounted for
1 parent b0fa5ca commit 2060eb2

4 files changed

Lines changed: 18 additions & 0 deletions

File tree

src/zeroconf/_services/info.pxd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,5 @@ cdef class ServiceInfo(RecordUpdateListener):
107107

108108
@cython.locals(cacheable=cython.bint)
109109
cdef cython.set _get_address_and_nsec_records(self, object override_ttl)
110+
111+
cpdef async_clear_cache(self)

src/zeroconf/_services/info.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,14 @@ def properties(self) -> Dict[Union[str, bytes], Optional[Union[str, bytes]]]:
273273
assert self._properties is not None
274274
return self._properties
275275

276+
def async_clear_cache(self) -> None:
277+
"""Clear the cache for this service info."""
278+
self._dns_address_cache = None
279+
self._dns_pointer_cache = None
280+
self._dns_service_cache = None
281+
self._dns_text_cache = None
282+
self._get_address_and_nsec_records_cache = None
283+
276284
async def async_wait(self, timeout: float, loop: Optional[asyncio.AbstractEventLoop] = None) -> None:
277285
"""Calling task waits for a given number of milliseconds or until notified."""
278286
if not self._new_records_futures:

src/zeroconf/_services/registry.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ def _add(self, info: ServiceInfo) -> None:
9191
if info.key in self._services:
9292
raise ServiceNameAlreadyRegistered
9393

94+
info.async_clear_cache()
9495
self._services[info.key] = info
9596
self.types.setdefault(info.type.lower(), []).append(info.key)
9697
self.servers.setdefault(info.server_key, []).append(info.key)

tests/test_asyncio.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,20 @@ def update_service(self, zeroconf: Zeroconf, type: str, name: str) -> None:
171171
)
172172
task = await aiozc.async_update_service(new_info)
173173
await task
174+
assert new_info.dns_service().server_key == "ash-2.local."
175+
new_info.server = "ash-3.local."
176+
task = await aiozc.async_update_service(new_info)
177+
await task
178+
assert new_info.dns_service().server_key == "ash-3.local."
179+
174180
task = await aiozc.async_unregister_service(new_info)
175181
await task
176182
await aiozc.async_close()
177183

178184
assert calls == [
179185
('add', type_, registration_name),
180186
('update', type_, registration_name),
187+
('update', type_, registration_name),
181188
('remove', type_, registration_name),
182189
]
183190

0 commit comments

Comments
 (0)