8000 fix: reduce debug logging overhead by adding missing checks to datagr… · python-zeroconf/python-zeroconf@ac5c50a · GitHub
[go: up one dir, main page]

Skip to content

Commit ac5c50a

Browse files
authored
fix: reduce debug logging overhead by adding missing checks to datagram_received (#1188)
1 parent 9ee3019 commit ac5c50a

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

src/zeroconf/_core.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -282,18 +282,20 @@ def datagram_received(
282282
else:
283283
# https://github.com/python/mypy/issues/1178
284284
addr, port, flow, scope = addrs # type: ignore
285-
log.debug('IPv6 scope_id %d associated to the receiving interface', scope)
285+
if debug:
286+
log.debug('IPv6 scope_id %d associated to the receiving interface', scope)
286287
v6_flow_scope = (flow, scope)
287288

288289
if data_len > _MAX_MSG_ABSOLUTE:
289290
# Guard against oversized packets to ensure bad implementations cannot overwhelm
290291
# the system.
291-
log.debug(
292-
"Discarding incoming packet with length %s, which is larger "
293-
"than the absolute maximum size of %s",
294-
data_len,
295-
_MAX_MSG_ABSOLUTE,
296-
)
292+
if debug:
293+
log.debug(
294+
"Discarding incoming packet with length %s, which is larger "
295+
"than the absolute maximum size of %s",
296+
data_len,
297+
_MAX_MSG_ABSOLUTE,
298+
)
297299
return
298300

299301
now = current_time_millis()

tests/test_core.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,22 @@ def test_guard_against_oversized_packets():
761761
is None
762762
)
763763

764+
logging.getLogger('zeroconf').setLevel(logging.INFO)
765+
766+
listener.datagram_received(over_sized_packet, ('::1', const._MDNS_PORT, 1, 1))
767+
assert (
768+
zc.cache.async_get_unique(
769+
r.DNSText(
770+
"packet0.local.",
771+
const._TYPE_TXT,
772+
const._CLASS_IN | const._CLASS_UNIQUE,
773+
500,
774+
b'path=/~paulsm/',
775+
)
776+
)
777+
is None
778+
)
779+
764780
zc.close()
765781

766782

0 commit comments

Comments
 (0)
0