From 811d4ddfbdaea2b2fa43676bd53190bdacf60ab6 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 23 Apr 2026 21:48:18 -0500 Subject: [PATCH] [mdns] Tighten ESP8266 guards, expand window to 15s, document derivation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - mdns_esp8266.cpp: narrow setup()'s listener block and on_ip_state from USE_MDNS_EVENT_DRIVEN_POLLING to USE_MDNS_WIFI_LISTENER. ESP8266 has no ethernet so the practical effect is identical, but the tighter guard is symmetric with mdns_rp2040.cpp and more defensible if the listener macros ever drift. - MDNS_POLL_WINDOW_MS 12000 → 15000. The RFC-compliant LEAmDNS phase is ~9s worst case, but a blocked main loop (long component setup, WiFi scan, flash writes) can stretch the deadlines between our polls. 6s of margin absorbs that without materially extending the steady-state quiet period. - Header comment for MDNS_POLL_WINDOW_MS now breaks down the 9s derivation with the exact LEAmDNS constants (MDNS_PROBE_DELAY × MDNS_PROBE_COUNT + MDNS_ANNOUNCE_DELAY × MDNS_ANNOUNCE_COUNT + jitter + debounce hop) so future library bumps can be justified against the same math. --- esphome/components/mdns/mdns_component.h | 12 +++++++++++- esphome/components/mdns/mdns_esp8266.cpp | 4 ++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/esphome/components/mdns/mdns_component.h b/esphome/components/mdns/mdns_component.h index 4f651f7d84..798af0e0bf 100644 --- a/esphome/components/mdns/mdns_component.h +++ b/esphome/components/mdns/mdns_component.h @@ -76,7 +76,17 @@ class MDNSComponent final : public Component // and update() becomes pure overhead. We arm a bounded polling window from IP state // listener events so update() runs only during that phase. static constexpr uint32_t MDNS_UPDATE_INTERVAL_MS = 50; - static constexpr uint32_t MDNS_POLL_WINDOW_MS = 12000; // ~9s phase + jitter/restart margin + // Must exceed LEAmDNS's longest restart-to-announce-complete path: + // MDNS_PROBE_DELAY (250ms) × MDNS_PROBE_COUNT (3) = 750ms probing + // + MDNS_ANNOUNCE_DELAY (1000ms) × MDNS_ANNOUNCE_COUNT (8) = 8000ms announcing + // + rand() % MDNS_PROBE_DELAY jitter on first probe (0–250ms) + // + debounced schedule_function() hop when statusChangeCB fires on ESP8266 + // ≈ 9s nominal. 15s gives ~6s margin to absorb main-loop blocking (long + // component setup, WiFi scan, flash writes) that could stretch the deadlines + // between our polls. If LEAmDNS ever extends its phase (upstream library + // update) this constant needs to grow. Constants defined in LEAmDNS_Priv.h + // (ESP8266 core 3.1.2 / arduino-pico 5.5.1). + static constexpr uint32_t MDNS_POLL_WINDOW_MS = 15000; static constexpr uint32_t MDNS_POLL_ID = 0; static constexpr uint32_t MDNS_POLL_STOP_ID = 1; #endif diff --git a/esphome/components/mdns/mdns_esp8266.cpp b/esphome/components/mdns/mdns_esp8266.cpp index 95137ea116..f6d5786675 100644 --- a/esphome/components/mdns/mdns_esp8266.cpp +++ b/esphome/components/mdns/mdns_esp8266.cpp @@ -48,7 +48,7 @@ void MDNSComponent::start_polling_window_() { void MDNSComponent::setup() { this->setup_buffers_and_register_(register_esp8266); -#ifdef USE_MDNS_EVENT_DRIVEN_POLLING +#ifdef USE_MDNS_WIFI_LISTENER // LEAmDNS's own LwipIntf::statusChangeCB drives _restart() on netif changes; we just // arm the window around the initial probe/announce and each reconnect. Unconditional // here is safe: setup_priority::AFTER_CONNECTION guarantees the network is up. @@ -57,7 +57,7 @@ void MDNSComponent::setup() { #endif } -#ifdef USE_MDNS_EVENT_DRIVEN_POLLING +#ifdef USE_MDNS_WIFI_LISTENER void MDNSComponent::on_ip_state(const network::IPAddresses &ips, const network::IPAddress &, const network::IPAddress &) { // IP listener only fires on acquisition (not loss), so any notification is a fresh