Skip to content

Commit c245c2f

Browse files
authored
fix: shorten the duplicate packet window to 800 ms (#1930)
1 parent 4f017f5 commit c245c2f

3 files changed

Lines changed: 25 additions & 4 deletions

File tree

‎src/zeroconf/_listener.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ def _process_datagram_at_time(
251251
# _DUPLICATE_PACKET_SUPPRESSION_INTERVAL` so a cache miss never
252252
# triggers the branch even when `now` is small (time.monotonic
253253
# is allowed to start near zero, leaving `now - INTERVAL`
254-
# negative for the first ~1s after boot). Only non-QU payloads
254+
# negative for the first interval after boot). Only non-QU payloads
255255
# are cached, so any hit here is safe to suppress without re-
256256
# checking has_qu_question.
257257
recent_time = self._recent_packets.get(data, -1e30)

‎src/zeroconf/const.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,16 @@
1515
_LISTENER_TIME = 200 # ms
1616
_REGISTER_TIME = 225 # ms
1717
_UNREGISTER_TIME = 125 # ms
18-
_DUPLICATE_PACKET_SUPPRESSION_INTERVAL = 1000 # ms
18+
# Below the ~1s spacing of repeated announcements (RFC 6762 §8.3), which some
19+
# responders jitter to a little under a second.
20+
_DUPLICATE_PACKET_SUPPRESSION_INTERVAL = 800 # ms
1921
# Per-listener bounded recency window. 16 is large enough to defeat
2022
# the alternating-payload bypass (RFC 6762 §6.2, issue #1724 — even a
2123
# rotation of a dozen distinct payloads still dedups), and small
2224
# enough that the dict bookkeeping per miss stays cheap under a
2325
# hostile flood.
2426
_RECENT_PACKETS_MAX = 16
25-
_DUPLICATE_QUESTION_INTERVAL = 999 # ms # Must be 1ms less than _DUPLICATE_PACKET_SUPPRESSION_INTERVAL
27+
_DUPLICATE_QUESTION_INTERVAL = 999 # ms
2628
_CACHE_CLEANUP_INTERVAL = 10 # s
2729
_LOADED_SYSTEM_TIMEOUT = 10 # s
2830
_STARTUP_TIMEOUT = 9 # s must be lower than _LOADED_SYSTEM_TIMEOUT

‎tests/test_listener.py‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ def test_recent_packets_miss_with_small_now_is_not_suppressed(zc: Zeroconf) -> N
389389
"""A cache miss must not trigger suppression when `now` is below the suppression interval."""
390390
# time.monotonic() can start near zero on freshly booted systems, so
391391
# `now - _DUPLICATE_PACKET_SUPPRESSION_INTERVAL` is negative for the
392-
# first second of process lifetime. A 0.0 default on the recency
392+
# first interval of process lifetime. A 0.0 default on the recency
393393
# dict would let any negative `now - INTERVAL` satisfy the compare
394394
# and suppress legitimate traffic.
395395
zc.registry.async_add(
@@ -425,3 +425,22 @@ def handle_query_or_defer(
425425
with patch.object(listener, "handle_query_or_defer") as _handle_query_or_defer:
426426
listener._process_datagram_at_time(False, len(packet), 0.0, packet, addrs)
427427
_handle_query_or_defer.assert_called_once()
428+
429+
430+
def test_repeated_announcement_under_one_second_is_processed(zc: Zeroconf) -> None:
431+
"""An announcement repeated a little under a second later is not dropped as a duplicate."""
432+
listener = _listener.AsyncListener(zc)
433+
listener.transport = MagicMock()
434+
response = r.DNSOutgoing(const._FLAGS_QR_RESPONSE | const._FLAGS_AA)
435+
response.add_answer_at_time(
436+
r.DNSAddress("device.local.", const._TYPE_A, const._CLASS_IN, 120, b"\x7f\x00\x00\x01"), 0
437+
)
438+
packet = response.packets()[0]
439+
addrs = ("1.2.3.4", 5353)
440+
441+
now = current_time_millis()
442+
listener._process_datagram_at_time(False, len(packet), now, packet, addrs)
443+
listener._process_datagram_at_time(False, len(packet), now + 5, packet, addrs)
444+
assert listener.last_time == now
445+
listener._process_datagram_at_time(False, len(packet), now + 926, packet, addrs)
446+
assert listener.last_time == now + 926

0 commit comments

Comments
 (0)