Title: feat: add async_update_interfaces to rescan network interfaces at runtime by bdraco · Pull Request #1797 · python-zeroconf/python-zeroconf · GitHub
Open Graph Title: feat: add async_update_interfaces to rescan network interfaces at runtime by bdraco · Pull Request #1797 · python-zeroconf/python-zeroconf
X Title: feat: add async_update_interfaces to rescan network interfaces at runtime by bdraco · Pull Request #1797 · python-zeroconf/python-zeroconf
Description: Closes #1777. Summary Adds a runtime API to reconcile the sockets zeroconf uses to the live interface set, so an interface that appears or disappears no longer requires recreating the whole Zeroconf. async_update_interfaces (with a sync update_interfaces wrapper) adds a responder socket for each interface that showed up, tears down the socket for each interface that went away, and re-announces existing registrations when a new sender appeared. It accepts the same interfaces, ip_version, and apple_p2p arguments as the constructor, each defaulting to the construction-time value, so a caller reconfigures with the same vocabulary it started with. The listen socket is rebuilt when the new set needs a different address family; unicast mode is fixed at construction. How this maps to #1777 Works live, never tears down listeners for interfaces that haven't changed — an interface present in both the old and new set keeps its exact transport object; no teardown, no rebind, no packet loss on it. Accepts all the arguments we accept at startup — interfaces, ip_version, apple_p2p. Only tears down listeners for addresses that changed — the diff keys on (address, scope_id); only senders whose key left the desired set are closed, only keys that are new get a socket. An IP change is remove-old + add-new. Behavior No-op when nothing changed: if the reconciled set matches what is already bound, no socket is touched and nothing is re-announced. Re-announcement only fires when a new sender was actually added (a returning/new interface needs to advertise; removals need no re-announce on the survivors). Listen socket adapts when it must: a rescan that keeps the same address family never touches the listen socket; a set that needs a new family rebuilds it (re-joining the interfaces that stay), and moving a Default single-family instance to an explicit list converts its dual-use listen/responder socket to a pure listener, then adds per-interface responders for the whole set. The conversion rebuilds the socket clean so the new joins do not collide with the memberships it already held, and demotes it first so nothing double-announces. Details Zeroconf.__init__ retains interfaces, ip_version, and apple_p2p; the rescan re-runs normalize_interface_choice against the live set and brings each interface up through the same net.add_interface primitive construction uses, so setup and rescan stay in lockstep. (address, scope_id) keying lives on _WrappedTransport.interface_key (paired with _interface_key for the desired side), so link-local IPv6 addresses that repeat across interfaces don't collide; _WrappedTransport.multicast_interface gives the value membership calls take. New drop_multicast_member in net.py is the inverse of add_multicast_member (IP_DROP_MEMBERSHIP / IPV6_LEAVE_GROUP), swallowing the same benign errnos (including WSAEINVAL on Windows). Removal is proactive: the gone sender's protocol and reader/sender wrappers are dropped from the engine lists, then the transport is closed. Re-announce piggybacks on the existing _async_broadcast_service; the listener answers on whichever transport received the query, so a new reader needs no extra wiring. RFC 6762 section 8.3. AsyncZeroconf.async_update_interfaces delegates to Zeroconf, mirroring async_register_service. How a consumer drives this (e.g. Home Assistant) A consumer that constructs Zeroconf(interfaces=[...]) with an explicit address list calls async_update_interfaces(new_list) from its own adapter-change signal; the diff reconciles the new list against the bound sockets and leaves unchanged ones in place. Consumers with native adapter-change events use this directly; those without can opt into the polling monitor in the follow-up PR. This is the socket-side half of cases like home-assistant/core#59553 (it re-announces whatever ServiceInfo the consumer registered; correcting announced URL/TXT content after a network change is the consumer's job via async_update_service). Test plan tests/test_interface_update.py covers no-op (no sockets touched, no re-announce), default-to-stored-choice, ip_version/apple_p2p overrides, keep-unchanged-sender-untouched (same transport object), remove and re-add, re-announce on add, failed join, None responder rollback, the dual-use guard, the unicast no-listen-socket path, and the sync wrapper, plus _WrappedTransport.interface_key / multicast_interface / _interface_key unit tests. test_update_interfaces_reconciles_mixed_set drives the engine diff over a 4-sender v4+v6 set in one pass: keeps unchanged, drops gone, adds new, and distinguishes two interfaces sharing fe80::1 by scope_id (scope-2 kept, scope-3 dropped) end to end, asserting each gone interface leaves its group with its own representation. tests/utils/test_net.py::test_drop_multicast_member covers success, IPv6, benign errnos, reraise, no-IPv6, and the Windows WSAEINVAL path. _engine and _transport diff fully covered (lines and branches); compiled (REQUIRE_CYTHON=1) and pure-Python suites green; pre-commit run clean.
Open Graph Description: Closes #1777. Summary Adds a runtime API to reconcile the sockets zeroconf uses to the live interface set, so an interface that appears or disappears no longer requires recreating the whole Zerocon...
X Description: Closes #1777. Summary Adds a runtime API to reconcile the sockets zeroconf uses to the live interface set, so an interface that appears or disappears no longer requires recreating the whole Zerocon...
Opengraph URL: https://github.com/python-zeroconf/python-zeroconf/pull/1797
X: @github
Domain: github.com
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:8eda11a2-679b-99ce-40e4-c47116993851 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | DC1C:D9D90:19210C:222E72:6B00831A |
| html-safe-nonce | 9c09b025d756fe2c078234f68699e1fd3d6534450ad26da07b5db9c97c61a906 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEQzFDOkQ5RDkwOjE5MjEwQzoyMjJFNzI6NkIwMDgzMUEiLCJ2aXNpdG9yX2lkIjoiNDI3NDMyMjk0NDc4MTQ4NDgyNiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 453829e9d6eabe6ebe137f83cc229688eef795cab64ef57e7231e1a3ce532d07 |
| hovercard-subject-tag | pull_request:3908190372 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/python-zeroconf/python-zeroconf/pull/1797/files |
| twitter:image | https://avatars.githubusercontent.com/u/663432?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/663432?s=400&v=4 |
| og:image:alt | Closes #1777. Summary Adds a runtime API to reconcile the sockets zeroconf uses to the live interface set, so an interface that appears or disappears no longer requires recreating the whole Zerocon... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4d3d8e50f02c1d7bf86d5acfa1d3bfe9366abd6528a6b429eb07a168429d2b61 |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/python-zeroconf/python-zeroconf git https://github.com/python-zeroconf/python-zeroconf.git |
| octolytics-dimension-user_id | 120192235 |
| octolytics-dimension-user_login | python-zeroconf |
| octolytics-dimension-repository_id | 21548731 |
| octolytics-dimension-repository_nwo | python-zeroconf/python-zeroconf |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 21548731 |
| octolytics-dimension-repository_network_root_nwo | python-zeroconf/python-zeroconf |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | db758f8e02b8257935fb03edf81a3e76eb3abd11 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width