Skip to content

Commit 5d9864b

Browse files
committed
fix: restructure recent-packets dedup around pop() and type new tests
1 parent e3cfde8 commit 5d9864b

4 files changed

Lines changed: 6 additions & 6 deletions

File tree

src/zeroconf/_listener.pxd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ cdef class AsyncListener:
3232
@cython.locals(now=double, debug=cython.bint)
3333
cpdef datagram_received(self, cython.bytes bytes, cython.tuple addrs)
3434

35-
@cython.locals(msg=DNSIncoming, recent_packets=cython.dict, recent=cython.tuple)
35+
@cython.locals(msg=DNSIncoming, recent_packets=cython.dict, recent=cython.tuple, was_present=cython.bint)
3636
cpdef _process_datagram_at_time(self, bint debug, cython.uint data_len, double now, bytes data, cython.tuple addrs)
3737

3838
cdef _cancel_any_timers_for_addr(self, object addr)

src/zeroconf/_listener.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ def _process_datagram_at_time(
152152
msg = DNSIncoming(data, addr_port, scope, now)
153153
# Move-to-end so a repeat payload refreshes its position and only
154154
# cold entries are evicted when the window is full.
155-
recent_packets.pop(data, None)
156-
elif len(recent_packets) >= _RECENT_PACKETS_MAX:
155+
was_present = recent_packets.pop(data, None) is not None
156+
if not was_present and len(recent_packets) >= _RECENT_PACKETS_MAX:
157157
del recent_packets[next(iter(recent_packets))]
158158
recent_packets[data] = (now, msg.has_qu_question())
159159
if msg.valid is True:

tests/test_asyncio.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -901,7 +901,7 @@ async def test_async_unregister_all_services(quick_timing: None) -> None:
901901

902902

903903
@pytest.mark.asyncio
904-
async def test_async_zeroconf_service_types(quick_timing: None, disable_duplicate_packet_suppression) -> None:
904+
async def test_async_zeroconf_service_types(quick_timing: None, disable_duplicate_packet_suppression: None) -> None:
905905
type_ = "_test-srvc-type._tcp.local."
906906
name = "xxxyyy"
907907
registration_name = f"{name}.{type_}"

tests/test_listener.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ def handle_query_or_defer(
278278
zc.close()
279279

280280

281-
def test_guard_against_alternating_duplicate_packets():
281+
def test_guard_against_alternating_duplicate_packets() -> None:
282282
"""Alternating two distinct payloads must not bypass duplicate suppression."""
283283
zc = Zeroconf(interfaces=["127.0.0.1"])
284284
zc.registry.async_add(
@@ -334,7 +334,7 @@ def handle_query_or_defer(
334334
zc.close()
335335

336336

337-
def test_recent_packets_window_is_bounded():
337+
def test_recent_packets_window_is_bounded() -> None:
338338
"""Distinct payloads beyond the recency window evict oldest entries."""
339339
zc = Zeroconf(interfaces=["127.0.0.1"])
340340
zc.registry.async_add(

0 commit comments

Comments
 (0)