fix: bound QuestionHistory size to prevent LAN-driven OOM#1733
Draft
bluetoothbot wants to merge 1 commit into
Draft
fix: bound QuestionHistory size to prevent LAN-driven OOM#1733bluetoothbot wants to merge 1 commit into
bluetoothbot wants to merge 1 commit into
Conversation
QuestionHistory._history was unbounded between the 10s cache-cleanup ticks. A LAN peer streaming distinct questions could grow the dict (and the retained known_answers sets) for ~10s before async_expire ran, easily reaching hundreds of MB at line-rate. Cap _history at _MAX_QUESTION_HISTORY_ENTRIES (10000). When the cap is hit on insert, first call async_expire(now) opportunistically to drop entries past _DUPLICATE_QUESTION_INTERVAL; if the dict is still full, evict the oldest insertion (dict is ordered) until there is room. Match the existing DNSCache cap pattern. Fixes python-zeroconf#1723
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1733 +/- ##
=======================================
Coverage 99.76% 99.76%
=======================================
Files 33 33
Lines 3440 3446 +6
Branches 473 475 +2
=======================================
+ Hits 3432 3438 +6
Misses 5 5
Partials 3 3 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
QuestionHistory._historywas unbounded between the periodic 10sasync_expireticks driven by_CACHE_CLEANUP_INTERVAL. A LAN peer streaming distinct questions could grow the dict (and the retainedknown_answerssets) for ~10s before the next cleanup, easily reaching hundreds of MB at line-rate.Cap
_historyat a configurable upper bound, with opportunistic expiry on insert and oldest-first FIFO eviction as a fallback. Mirrors the_MAX_CACHE_RECORDSpattern from #1718.Fixes #1723
Changes
_MAX_QUESTION_HISTORY_ENTRIES = 10000toconst.py.QuestionHistory.add_question_at_time, when the cap would be exceeded by a new entry, first callasync_expire(now)to drop entries past_DUPLICATE_QUESTION_INTERVAL; if still full, evict the oldest insertion (Pythondictpreserves insertion order) until there is room._history.pxdto declare the new constant for the Cython hot path.Test plan
tests/test_history.pycases:test_question_history_bounded— flooding past the cap keeps_historysize ≤ cap.test_question_history_evicts_oldest_first— earliest-inserted entry is the first dropped at cap.test_question_history_opportunistic_expire— stale entries are reclaimed before fresh ones are evicted.SKIP_CYTHON=1 poetry run pytest --timeout=60 tests→ 361 passed, 2 skipped.poetry run cython -3 src/zeroconf/_history.pycompiles cleanly with the updated.pxd.Generated by Kōan /fix
Quality Report
Changes: 4 files changed, 70 insertions(+), 1 deletion(-)
Code scan: clean
Tests: passed (4 PASSED)
Branch hygiene: clean
Generated by Kōan post-mission quality pipeline