Repository navigation
Reconcile zeroconf sockets when network interfaces change - #1770
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1770 +/- ##
=======================================
Coverage 99.55% 99.55%
=======================================
Files 229 230 +1
Lines 18291 18345 +54
=======================================
+ Hits 18209 18263 +54
Misses 82 82
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This PR improves mDNS resilience by detecting host network interface/address changes (e.g., VPN/Wi‑Fi/Docker churn) and triggering python-zeroconf’s interface reconciliation so discovery doesn’t go stale until restart.
Changes:
- Add an
ifaddr-based polling monitor that snapshots host addresses and callsAsyncEsphomeZeroconf.async_update_interfaces()only on change. - Wire the monitor into
MdnsSourcelifecycle so it starts with zeroconf and is cancelled before zeroconf shutdown. - Add unit tests covering reconcile/no-op behavior and lifecycle teardown.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
esphome_device_builder/controllers/_device_state_monitor/mdns.py |
Spawns and cancels the interface-monitor task around zeroconf lifecycle. |
esphome_device_builder/controllers/_device_state_monitor/interface_monitor.py |
New polling monitor that snapshots addresses and reconciles zeroconf sockets on change. |
tests/test_interface_monitor.py |
New tests for reconcile triggering, no-op behavior, and retry semantics. |
tests/test_state_monitor_lifecycle.py |
Adds a lifecycle test ensuring the interface-monitor task is spawned and torn down during stop. |
|
|
ESPHome Bot (esphbot)
left a comment
There was a problem hiding this comment.
Blocking issues found.
- Snapshot executor calls are unguarded — a transient adapter-read error kills the monitor silently
|
Thanks, this was reviewed against the pre-fix commit 2785c73; the blocking item plus the follow-ups are all addressed now. Blocking, snapshot executor calls unguarded (also the HIGH and the line 41 MEDIUM): fixed in c67d6e4. Both the pre-loop and in-loop snapshots now go through Fire-and-forget task with no failure surfacing (mdns.py MEDIUM): fixed in 72f8d5a. The task now gets a done-callback, Dependency floor: added Second adapter-polling loop: aligned the cadence to DashboardAdvertiser's |
Re-reviewed at current HEAD. All items confirmed fixed. Verified the floor independently: |
PR Review — Reconcile zeroconf sockets when network interfaces changeAll prior blocking findings resolved and independently verified — merge-ready. What's solid:
Fixes since the prior review, all with regression tests:
Checklist
Automated review by Kōan (Claude) |
ESPHome Bot (esphbot)
left a comment
There was a problem hiding this comment.
No blocking issues found.
What does this implement/fix?
python-zeroconf binds its sockets once at construction, so it never notices interfaces that come or go afterward (a VPN connecting, Wi-Fi reconnecting, a Docker network attaching); mDNS discovery then silently goes deaf on the changed interface until the process restarts. python-zeroconf 0.150.0 added
async_update_interfaces, which rescans the host and reconciles the sockets in use (binds responders for interfaces that appeared, tears down ones that went away, re-announces existing registrations).This wires that in with a small interface monitor that snapshots the host addresses on a relaxed timer and calls
async_update_interfacesonly when the set actually changes; a no-op tick touches nothing.MdnsSourceowns the task; it comes up with the responder and is cancelled, then awaited, before zeroconf closes so it can never reconcile a closing instance or leak past shutdown.ifaddr.get_adapters()is blocking, so every snapshot runs in an executor and the event loop only awaits. Detection is the portable poll the zeroconf docs recommend when no netlink or framework push signal is wired, so it works on the Windows CI matrix too.Related issue or feature (if applicable):
async_update_interfaces: https://github.com/python-zeroconf/python-zeroconf/releases/tag/0.150.0Types of changes
bugfixnew-featureenhancementbreaking-changerefactordocsmaintenancecidependenciesFrontend coordination
Checklist
ruff,codespell, yaml/json/python checks).tests/where applicable.components.index.json/definitions/components/*.jsonhave not been hand-edited (regenerate viascript/sync_components.pyif a sync is needed).docs/ARCHITECTURE.mdand/ordocs/API.md.