Skip to content

Commit bf4f5b4

Browse files
committed
fix: report a scoped AAAA goodbye for an unscoped cached copy
A goodbye now takes the cached scope like any other copy, so listeners get the RecordUpdate for it. A live scoped copy removes the unscoped one right away instead of staging it with the removes, which a full cache could have evicted first.
1 parent 3fab5cb commit bf4f5b4

3 files changed

Lines changed: 14 additions & 7 deletions

File tree

‎src/zeroconf/_handlers/record_manager.pxd‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ cdef class RecordManager:
4040
cpdef void async_remove_listener(self, RecordUpdateListener listener)
4141

4242
@cython.locals(cached=DNSAddress)
43-
cdef DNSAddress _async_merge_address(self, DNSAddress record, object removes)
43+
cdef DNSAddress _async_merge_address(self, DNSAddress record)
4444

4545
@cython.locals(question=DNSQuestion, record=DNSRecord)
4646
cdef void _async_update_matching_records(self, RecordUpdateListener listener, cython.list questions)

‎src/zeroconf/_handlers/record_manager.py‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
_float = float
2525
_RecordUpdate = RecordUpdate
26-
_DNSRecordSet = set[DNSRecord]
2726
_DNSQuestion = DNSQuestion
2827

2928

@@ -122,7 +121,7 @@ def async_updates_from_response(self, msg: DNSIncoming) -> None:
122121
if maybe_entry is None and record_type == _TYPE_AAAA:
123122
if TYPE_CHECKING:
124123
assert isinstance(record, DNSAddress)
125-
maybe_entry = self._async_merge_address(record, removes)
124+
maybe_entry = self._async_merge_address(record)
126125

127126
if not record.is_expired(now):
128127
if record_type in _ADDRESS_RECORD_TYPES:
@@ -173,13 +172,13 @@ def async_updates_from_response(self, msg: DNSIncoming) -> None:
173172
if updates:
174173
self.async_updates_complete(new)
175174

176-
def _async_merge_address(self, record: DNSAddress, removes: _DNSRecordSet) -> DNSAddress | None:
175+
def _async_merge_address(self, record: DNSAddress) -> DNSAddress | None:
177176
"""Return the cached copy of the AAAA, keeping one per address and preferring a scoped one."""
178177
cached = self.cache.async_get_address(record)
179178
if cached is None:
180179
return None
181-
if record.scope_id and not cached.scope_id:
182-
removes.add(cached)
180+
if record.scope_id and not cached.scope_id and record.ttl:
181+
self.cache.async_remove_records((cached,))
183182
return None
184183
record._set_scope_id(cached.scope_id)
185184
return cached

‎tests/test_handlers.py‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1874,12 +1874,19 @@ async def test_aaaa_unscoped_copy_refreshes_scoped() -> None:
18741874
@pytest.mark.parametrize(("cached_scope", "goodbye_scope"), [(2, 0), (0, 2), (2, 15)])
18751875
@pytest.mark.asyncio
18761876
async def test_aaaa_goodbye_under_another_scope_removes_cached(cached_scope: int, goodbye_scope: int) -> None:
1877-
"""A goodbye for an address removes its cached AAAA whatever scope either was received with."""
1877+
"""A goodbye for an address removes and reports its cached AAAA whatever scope either came with."""
18781878
aiozc = AsyncZeroconf(interfaces=["127.0.0.1"])
18791879
zc = aiozc.zeroconf
18801880
zc.record_manager.async_updates_from_response(
18811881
r.DNSIncoming(aaaa_response_packet("host.local."), ("fe80::2", 5353), cached_scope)
18821882
)
1883+
updates: list[r.RecordUpdate] = []
1884+
1885+
class EventStore(r.RecordUpdateListener):
1886+
def async_update_records(self, zc: Zeroconf, now: float, records: list[r.RecordUpdate]) -> None:
1887+
updates.extend(records)
1888+
1889+
zc.record_manager.async_add_listener(EventStore(), None)
18831890
goodbye = r.DNSOutgoing(const._FLAGS_QR_RESPONSE)
18841891
goodbye.add_answer_at_time(
18851892
r.DNSAddress(
@@ -1895,4 +1902,5 @@ async def test_aaaa_goodbye_under_another_scope_removes_cached(cached_scope: int
18951902
r.DNSIncoming(goodbye.packets()[0], ("fe80::2", 5353), goodbye_scope)
18961903
)
18971904
assert zc.cache.async_all_by_details("host.local.", const._TYPE_AAAA, const._CLASS_IN) == []
1905+
assert [(update.new.ttl, update.old is not None) for update in updates] == [(0, True)]
18981906
await aiozc.async_close()

0 commit comments

Comments
 (0)