fix: tolerate removing unregistered record listener#1783
Draft
bluetoothbot wants to merge 2 commits into
Draft
Conversation
Demonstrates GHSA-5pv9-xcmm-gqc7: set.remove() raises KeyError, which the except ValueError clause cannot catch.
self.listeners is a set, so .remove() raises KeyError, never ValueError — the existing except clause could not catch the only realistic failure mode. Switch to the set-native discard idiom so removing a listener that was never registered (e.g. during teardown / reconnect sequences) no longer breaks the shutdown flow.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1783 +/- ##
==========================================
+ Coverage 99.77% 99.80% +0.02%
==========================================
Files 33 33
Lines 3536 3534 -2
Branches 498 499 +1
==========================================
- Hits 3528 3527 -1
+ Misses 5 4 -1
Partials 3 3 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
bdraco
requested changes
May 26, 2026
bdraco
left a comment
Member
There was a problem hiding this comment.
this should still log debug if listener was never there
Member
Contributor
Author
Rebase with requested adjustmentsBranch StatsActions performed
CI statusCI will be checked asynchronously. Automated by Kōan |
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.
What
Switch
RecordManager.async_remove_listenerfromset.remove()+except ValueErrortoset.discard(), so removing a listener that was never registered no longer raises.Why
self.listenersis aset, so.remove(listener)raisesKeyErroron a missing element — notValueError. The existingexcept ValueErrorclause could not catch the only realistic failure mode of that line, so the documented "log and continue" intent was effectively dead code. A teardown / reconnect path that double-removes a listener propagatesKeyErrorout ofZeroconf.async_remove_listenerand breaks the surrounding shutdown.How
Use
set.discard()(the set-native "remove if present" idiom) gated byif listener in self.listeners, soasync_notify_all()only fires when something actually changed. Thetry/exceptis gone.Testing
Added
tests/test_handlers.py::test_async_remove_listener_missing_does_not_raisein a separate commit against the unfixed code first, so the before/after is explicit. Full suite (SKIP_CYTHON=1 pytest): 393 passed.Quality Report
Changes: 2 files changed, 16 insertions(+), 4 deletions(-)
Code scan: clean
Tests: passed (4 PASSED)
Branch hygiene: clean
Generated by Kōan post-mission quality pipeline