fix: ensure ServiceInfo cache is cleared when adding to the registry … · python-zeroconf/python-zeroconf@2060eb2

4 files changed

lines changed

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)

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:

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)

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