Skip to content

Commit ba5de3a

Browse files
committed
refactor: switch _mark_seen eviction from clear() to pop() and raise cap to 512
Smooth out the overflow path: instead of dropping the entire set at once when it hits the bound (which then re-emits warning-level logs for every previously-deduped key on its next occurrence), evict one arbitrary entry per insert past the bound. The cap effectively becomes a recency window of size `_MAX_SEEN_LOGS`. Also raises the bound from 256 to 512. Memory cost stays trivial (~40 KB of short exception strings), but under sustained malformed- packet pressure ops sees a steady trickle of new warnings rather than a burst of re-emissions every few hundred packets.
1 parent 85ac2ae commit ba5de3a

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

src/zeroconf/_logger.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,22 @@ def set_logger_level_if_unset() -> None:
3939
set_logger_level_if_unset()
4040

4141

42-
_MAX_SEEN_LOGS = 256
42+
_MAX_SEEN_LOGS = 512
4343
_seen_logs: set[str] = set()
4444

4545

4646
def _mark_seen(seen: set[str], key: str) -> bool:
4747
"""Record ``key`` in ``seen`` and return True if it was newly added.
4848
4949
Bounds the set so callers passing attacker-influenced keys (peer
50-
addresses, packet offsets) cannot grow it without bound.
50+
addresses, packet offsets) cannot grow it without bound. Evicts
51+
one arbitrary entry per overflow so warning-level re-emissions
52+
stay smooth rather than arriving in bursts.
5153
"""
5254
if key in seen:
5355
return False
5456
if len(seen) >= _MAX_SEEN_LOGS:
55-
seen.clear()
57+
seen.pop()
5658
seen.add(key)
5759
return True
5860

0 commit comments

Comments
 (0)