From 715f0ca6f7c7d1dec65d4fd2e2670c4122c8e141 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 11 Apr 2026 14:54:02 -1000 Subject: [PATCH] [core] Extract WDT_FEED_INTERVAL_MS constexpr Replace the magic 3 in both the inline feed_wdt check and the slow path with a named constexpr so the rate-limit threshold is defined once. --- esphome/core/application.cpp | 7 +++---- esphome/core/application.h | 7 ++++++- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/esphome/core/application.cpp b/esphome/core/application.cpp index d1aa461d0e..84a1fc346f 100644 --- a/esphome/core/application.cpp +++ b/esphome/core/application.cpp @@ -199,10 +199,9 @@ void Application::process_dump_config_() { void HOT Application::feed_wdt_slow_(uint32_t time) { // Use provided time if available, otherwise get current time uint32_t now = time ? time : millis(); - // Compare in milliseconds (3ms threshold). The inline wrapper already - // performs this check when time != 0; repeat it here for the time == 0 - // entry and as a safety net. - if (now - this->last_wdt_feed_ > 3) { + // The inline wrapper already performs this check when time != 0; + // repeat it here for the time == 0 entry and as a safety net. + if (now - this->last_wdt_feed_ > WDT_FEED_INTERVAL_MS) { arch_feed_wdt(); this->last_wdt_feed_ = now; #ifdef USE_STATUS_LED diff --git a/esphome/core/application.h b/esphome/core/application.h index f8e9b0e360..7cde0bf85d 100644 --- a/esphome/core/application.h +++ b/esphome/core/application.h @@ -385,6 +385,11 @@ class Application { void schedule_dump_config() { this->dump_config_at_ = 0; } + /// Minimum interval between real arch_feed_wdt() calls. Chosen to keep the + /// rate of HAL pokes low while still being small enough that any plausible + /// watchdog timeout (seconds) has orders of magnitude of safety margin. + static constexpr uint32_t WDT_FEED_INTERVAL_MS = 3; + /// Feed the task watchdog. Hot-path inline rate-limit check: callers that /// already have a timestamp in hand pay only a load + sub + branch on the /// common (no-op) path. The actual arch feed + status LED update live in @@ -393,7 +398,7 @@ class Application { /// Pass time==0 to request millis() be read for you (low-frequency callers /// only — always takes the slow path). void ESPHOME_ALWAYS_INLINE feed_wdt(uint32_t time = 0) { - if (time != 0 && static_cast(time - this->last_wdt_feed_) <= 3) { + if (time != 0 && static_cast(time - this->last_wdt_feed_) <= WDT_FEED_INTERVAL_MS) { return; } this->feed_wdt_slow_(time);