Repository navigation
fix: ha discovery workaround race - #14
Merged
Merged
Conversation
jmattsson
reviewed
Oct 18, 2026
| self._owner._internal_callback(request()) | ||
| lookup = self._owner._internal_callback(request()) | ||
| self._lookups[name] = lookup | ||
| lookup.add_done_callback(lambda task: self._forget_lookup(name, task)) |
Member
There was a problem hiding this comment.
It smells weird to have lookup and task aliased here, but I don't see how to improve it.
| # The plug was never dispatched, so there is nothing to remove either. | ||
| on_add.assert_not_awaited() | ||
| on_remove.assert_not_awaited() | ||
| assert 'removal ignored' not in caplog.text |
Member
There was a problem hiding this comment.
Shouldn't there (also) be a positive assert for "removed before it answered"?
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.
The workaround for HA zeroconf discovery introduced a rare race condition.
If a plug's PTR record is still cached but its SRV or A record has expired, we ask the plug for its records directly. If
remove_serviceis called before the plug answers (e.g. because the PTR record expired), the removal is ignored,since we don't know the plug's MAC yet, and the request keeps running. When the plug eventually answers, we add the device anyway, so it is reported as found after it was removed and is left stranded.
This PR fixes it by tracking each pending request by service name:
remove_servicecancels a pending request, so the plug is never added.add_serviceorupdate_servicewhile one is pending is ignored, as the pending request will dispatch the result.