ESPHome 2026.4.3
Loading...
Searching...
No Matches
scheduler.cpp
Go to the documentation of this file.
1#include "scheduler.h"
2
3#include "application.h"
5#include "esphome/core/hal.h"
7#include "esphome/core/log.h"
9#include <algorithm>
10#include <cinttypes>
11#include <cstring>
12
13namespace esphome {
14
15static const char *const TAG = "scheduler";
16
17// Memory pool configuration constants
18// Pool size of 5 matches typical usage patterns (2-4 active timers)
19// - Minimal memory overhead (~250 bytes on ESP32)
20// - Sufficient for most configs with a couple sensors/components
21// - Still prevents heap fragmentation and allocation stalls
22// - Complex setups with many timers will just allocate beyond the pool
23// See https://github.com/esphome/backlog/issues/52
24static constexpr size_t MAX_POOL_SIZE = 5;
25
26// Maximum number of logically deleted (cancelled) items before forcing cleanup.
27// Set to 5 to match the pool size - when we have as many cancelled items as our
28// pool can hold, it's time to clean up and recycle them.
29static constexpr uint32_t MAX_LOGICALLY_DELETED_ITEMS = 5;
30// max delay to start an interval sequence
31static constexpr uint32_t MAX_INTERVAL_DELAY = 5000;
32
33#if defined(ESPHOME_LOG_HAS_VERBOSE) || defined(ESPHOME_DEBUG_SCHEDULER)
34// Helper struct for formatting scheduler item names consistently in logs
35// Uses a stack buffer to avoid heap allocation
36// Uses ESPHOME_snprintf_P/ESPHOME_PSTR for ESP8266 to keep format strings in flash
37struct SchedulerNameLog {
38 char buffer[20]; // Enough for "id:4294967295" or "hash:0xFFFFFFFF" or "(null)"
39
40 // Format a scheduler item name for logging
41 // Returns pointer to formatted string (either static_name or internal buffer)
42 const char *format(Scheduler::NameType name_type, const char *static_name, uint32_t hash_or_id) {
43 using NameType = Scheduler::NameType;
44 if (name_type == NameType::STATIC_STRING) {
45 if (static_name)
46 return static_name;
47 // Copy "(null)" to buffer to keep it in flash on ESP8266
48 ESPHOME_strncpy_P(buffer, ESPHOME_PSTR("(null)"), sizeof(buffer));
49 return buffer;
50 } else if (name_type == NameType::HASHED_STRING) {
51 ESPHOME_snprintf_P(buffer, sizeof(buffer), ESPHOME_PSTR("hash:0x%08" PRIX32), hash_or_id);
52 return buffer;
53 } else if (name_type == NameType::NUMERIC_ID) {
54 ESPHOME_snprintf_P(buffer, sizeof(buffer), ESPHOME_PSTR("id:%" PRIu32), hash_or_id);
55 return buffer;
56 } else { // NUMERIC_ID_INTERNAL
57 ESPHOME_snprintf_P(buffer, sizeof(buffer), ESPHOME_PSTR("iid:%" PRIu32), hash_or_id);
58 return buffer;
59 }
60 }
61};
62#endif
63
64// Uncomment to debug scheduler
65// #define ESPHOME_DEBUG_SCHEDULER
66
67#ifdef ESPHOME_DEBUG_SCHEDULER
68// Helper to validate that a pointer looks like it's in static memory
69static void validate_static_string(const char *name) {
70 if (name == nullptr)
71 return;
72
73 // This is a heuristic check - stack and heap pointers are typically
74 // much higher in memory than static data
75 uintptr_t addr = reinterpret_cast<uintptr_t>(name);
76
77 // Create a stack variable to compare against
78 int stack_var;
79 uintptr_t stack_addr = reinterpret_cast<uintptr_t>(&stack_var);
80
81 // If the string pointer is near our stack variable, it's likely on the stack
82 // Using 8KB range as ESP32 main task stack is typically 8192 bytes
83 if (addr > (stack_addr - 0x2000) && addr < (stack_addr + 0x2000)) {
84 ESP_LOGW(TAG,
85 "WARNING: Scheduler name '%s' at %p appears to be on the stack - this is unsafe!\n"
86 " Stack reference at %p",
87 name, name, &stack_var);
88 }
89
90 // Also check if it might be on the heap by seeing if it's in a very different range
91 // This is platform-specific but generally heap is allocated far from static memory
92 static const char *static_str = "test";
93 uintptr_t static_addr = reinterpret_cast<uintptr_t>(static_str);
94
95 // If the address is very far from known static memory, it might be heap
96 if (addr > static_addr + 0x100000 || (static_addr > 0x100000 && addr < static_addr - 0x100000)) {
97 ESP_LOGW(TAG, "WARNING: Scheduler name '%s' at %p might be on heap (static ref at %p)", name, name, static_str);
98 }
99}
100#endif /* ESPHOME_DEBUG_SCHEDULER */
101
102// A note on locking: the `lock_` lock protects the `items_` and `to_add_` containers. It must be taken when writing to
103// them (i.e. when adding/removing items, but not when changing items). As items are only deleted from the loop task,
104// iterating over them from the loop task is fine; but iterating from any other context requires the lock to be held to
105// avoid the main thread modifying the list while it is being accessed.
106
107// Calculate random offset for interval timers
108// Extracted from set_timer_common_ to reduce code size - only needed for intervals, not timeouts
109uint32_t Scheduler::calculate_interval_offset_(uint32_t delay) {
110 uint32_t max_offset = std::min(delay / 2, MAX_INTERVAL_DELAY);
111 // Multiply-and-shift: uniform random in [0, max_offset) without floating point
112 return static_cast<uint32_t>((static_cast<uint64_t>(random_uint32()) * max_offset) >> 32);
113}
114
115// Check if a retry was already cancelled in items_ or to_add_
116// Extracted from set_timer_common_ to reduce code size - retry path is cold and deprecated
117// Remove before 2026.8.0 along with all retry code
118bool Scheduler::is_retry_cancelled_locked_(Component *component, NameType name_type, const char *static_name,
119 uint32_t hash_or_id) {
120 for (auto *container : {&this->items_, &this->to_add_}) {
121 for (auto *item : *container) {
122 if (item != nullptr && this->is_item_removed_locked_(item) &&
123 this->matches_item_locked_(item, component, name_type, static_name, hash_or_id, SchedulerItem::TIMEOUT,
124 /* match_retry= */ true, /* skip_removed= */ false)) {
125 return true;
126 }
127 }
128 }
129 return false;
130}
131
132// Common implementation for both timeout and interval
133// name_type determines storage type: STATIC_STRING uses static_name, others use hash_or_id
134void HOT Scheduler::set_timer_common_(Component *component, SchedulerItem::Type type, NameType name_type,
135 const char *static_name, uint32_t hash_or_id, uint32_t delay,
136 std::function<void()> &&func, bool is_retry, bool skip_cancel) {
137 if (delay == SCHEDULER_DONT_RUN) {
138 // Still need to cancel existing timer if we have a name/id
139 if (!skip_cancel) {
140 LockGuard guard{this->lock_};
141 this->cancel_item_locked_(component, name_type, static_name, hash_or_id, type, /* match_retry= */ false,
142 /* find_first= */ true);
143 }
144 return;
145 }
146
147 // An interval of 0 means "fire every tick forever," which is misuse: the
148 // item would always be due, causing Scheduler::call() to spin and starve
149 // the main loop (WDT reset in the field). Coerce to 1ms so existing code
150 // using update_interval=0ms as a pseudo-loop() continues to work at ~1kHz,
151 // and warn so authors can migrate to HighFrequencyLoopRequester which is
152 // the intended mechanism for running fast in the main loop. Zero-delay
153 // timeouts (defer) remain legitimate one-shots and are not affected.
154 if (type == SchedulerItem::INTERVAL && delay == 0) [[unlikely]] {
155 ESP_LOGE(TAG, "[%s] set_interval(0) would spin main loop - coercing to 1ms (use HighFrequencyLoopRequester)",
156 component ? LOG_STR_ARG(component->get_component_log_str()) : LOG_STR_LITERAL("?"));
157 delay = 1;
158 }
159
160 // Take lock early to protect scheduler_item_pool_ access and retry-cancelled check
161 LockGuard guard{this->lock_};
162
163 // For retries, check if there's a cancelled timeout first - before allocating an item.
164 // Skip check for anonymous retries (STATIC_STRING with nullptr) - they can't be cancelled by name
165 // Skip check for defer (delay=0) - deferred retries bypass the cancellation check
166 if (is_retry && delay != 0 && (name_type != NameType::STATIC_STRING || static_name != nullptr) &&
167 type == SchedulerItem::TIMEOUT &&
168 this->is_retry_cancelled_locked_(component, name_type, static_name, hash_or_id)) {
169#ifdef ESPHOME_DEBUG_SCHEDULER
170 SchedulerNameLog skip_name_log;
171 ESP_LOGD(TAG, "Skipping retry '%s' - found cancelled item",
172 skip_name_log.format(name_type, static_name, hash_or_id));
173#endif
174 return;
175 }
176
177 // Create and populate the scheduler item
178 SchedulerItem *item = this->get_item_from_pool_locked_();
179 item->component = component;
180 item->set_name(name_type, static_name, hash_or_id);
181 item->type = type;
182 // Use destroy + placement-new instead of move-assignment.
183 // GCC's std::function::operator=(function&&) does a full swap dance even when the
184 // target is empty. Since recycled/new items always have an empty callback, we can
185 // destroy the empty one (no-op) and move-construct directly, saving ~40 bytes of
186 // swap/destructor code on Xtensa.
187 item->callback.~function();
188 new (&item->callback) std::function<void()>(std::move(func));
189 // Reset remove flag - recycled items may have been cancelled (remove=true) in previous use
190 this->set_item_removed_(item, false);
191 item->is_retry = is_retry;
192
193 // Determine target container: defer_queue_ for deferred items, to_add_ for everything else.
194 // Using a pointer lets both paths share the cancel + push_back epilogue.
195 auto *target = &this->to_add_;
196
197#ifndef ESPHOME_THREAD_SINGLE
198 // Special handling for defer() (delay = 0, type = TIMEOUT)
199 // Single-core platforms don't need thread-safe defer handling
200 if (delay == 0 && type == SchedulerItem::TIMEOUT) {
201 // Put in defer queue for guaranteed FIFO execution
202 target = &this->defer_queue_;
203 } else
204#endif /* not ESPHOME_THREAD_SINGLE */
205 {
206 // Only non-defer items need a timestamp for scheduling
207 const uint64_t now_64 = millis_64();
208
209 // Type-specific setup
210 if (type == SchedulerItem::INTERVAL) {
211 item->interval = delay;
212 // first execution happens immediately after a random smallish offset
213 uint32_t offset = this->calculate_interval_offset_(delay);
214 item->set_next_execution(now_64 + offset);
215#ifdef ESPHOME_LOG_HAS_VERBOSE
216 SchedulerNameLog name_log;
217 ESP_LOGV(TAG, "Scheduler interval for %s is %" PRIu32 "ms, offset %" PRIu32 "ms",
218 name_log.format(name_type, static_name, hash_or_id), delay, offset);
219#endif
220 } else {
221 item->interval = 0;
222 item->set_next_execution(now_64 + delay);
223 }
224
225#ifdef ESPHOME_DEBUG_SCHEDULER
226 this->debug_log_timer_(item, name_type, static_name, hash_or_id, type, delay, now_64);
227#endif /* ESPHOME_DEBUG_SCHEDULER */
228 }
229
230 // Common epilogue: atomic cancel-and-add (unless skip_cancel is true or anonymous)
231 // Anonymous items (STATIC_STRING with nullptr) can never match anything, so skip the scan.
232 if (!skip_cancel && (name_type != NameType::STATIC_STRING || static_name != nullptr)) {
233 this->cancel_item_locked_(component, name_type, static_name, hash_or_id, type, /* match_retry= */ false,
234 /* find_first= */ true);
235 }
236 target->push_back(item);
237 if (target == &this->to_add_) {
238 this->to_add_count_increment_();
239 }
240#ifndef ESPHOME_THREAD_SINGLE
241 else {
242 this->defer_count_increment_();
243 }
244#endif
245}
246
247void HOT Scheduler::set_timeout(Component *component, const char *name, uint32_t timeout,
248 std::function<void()> &&func) {
249 this->set_timer_common_(component, SchedulerItem::TIMEOUT, NameType::STATIC_STRING, name, 0, timeout,
250 std::move(func));
251}
252
253void HOT Scheduler::set_timeout(Component *component, const std::string &name, uint32_t timeout,
254 std::function<void()> &&func) {
255 this->set_timer_common_(component, SchedulerItem::TIMEOUT, NameType::HASHED_STRING, nullptr, fnv1a_hash(name),
256 timeout, std::move(func));
257}
258void HOT Scheduler::set_timeout(Component *component, uint32_t id, uint32_t timeout, std::function<void()> &&func) {
259 this->set_timer_common_(component, SchedulerItem::TIMEOUT, NameType::NUMERIC_ID, nullptr, id, timeout,
260 std::move(func));
261}
262bool HOT Scheduler::cancel_timeout(Component *component, const std::string &name) {
263 return this->cancel_item_(component, NameType::HASHED_STRING, nullptr, fnv1a_hash(name), SchedulerItem::TIMEOUT);
264}
265bool HOT Scheduler::cancel_timeout(Component *component, const char *name) {
266 return this->cancel_item_(component, NameType::STATIC_STRING, name, 0, SchedulerItem::TIMEOUT);
267}
268bool HOT Scheduler::cancel_timeout(Component *component, uint32_t id) {
269 return this->cancel_item_(component, NameType::NUMERIC_ID, nullptr, id, SchedulerItem::TIMEOUT);
270}
271void HOT Scheduler::set_interval(Component *component, const std::string &name, uint32_t interval,
272 std::function<void()> &&func) {
273 this->set_timer_common_(component, SchedulerItem::INTERVAL, NameType::HASHED_STRING, nullptr, fnv1a_hash(name),
274 interval, std::move(func));
275}
276
277void HOT Scheduler::set_interval(Component *component, const char *name, uint32_t interval,
278 std::function<void()> &&func) {
279 this->set_timer_common_(component, SchedulerItem::INTERVAL, NameType::STATIC_STRING, name, 0, interval,
280 std::move(func));
281}
282void HOT Scheduler::set_interval(Component *component, uint32_t id, uint32_t interval, std::function<void()> &&func) {
283 this->set_timer_common_(component, SchedulerItem::INTERVAL, NameType::NUMERIC_ID, nullptr, id, interval,
284 std::move(func));
285}
286bool HOT Scheduler::cancel_interval(Component *component, const std::string &name) {
287 return this->cancel_item_(component, NameType::HASHED_STRING, nullptr, fnv1a_hash(name), SchedulerItem::INTERVAL);
288}
289bool HOT Scheduler::cancel_interval(Component *component, const char *name) {
290 return this->cancel_item_(component, NameType::STATIC_STRING, name, 0, SchedulerItem::INTERVAL);
291}
292bool HOT Scheduler::cancel_interval(Component *component, uint32_t id) {
293 return this->cancel_item_(component, NameType::NUMERIC_ID, nullptr, id, SchedulerItem::INTERVAL);
294}
295
296// Suppress deprecation warnings for RetryResult usage in the still-present (but deprecated) retry implementation.
297// Remove before 2026.8.0 along with all retry code.
298#pragma GCC diagnostic push
299#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
300
301struct RetryArgs {
302 // Ordered to minimize padding on 32-bit systems
303 std::function<RetryResult(uint8_t)> func;
304 Component *component;
305 Scheduler *scheduler;
306 // Union for name storage - only one is used based on name_type
307 union {
308 const char *static_name; // For STATIC_STRING
309 uint32_t hash_or_id; // For HASHED_STRING or NUMERIC_ID
310 } name_;
311 uint32_t current_interval;
312 float backoff_increase_factor;
313 Scheduler::NameType name_type; // Discriminator for name_ union
314 uint8_t retry_countdown;
315};
316
317void retry_handler(const std::shared_ptr<RetryArgs> &args) {
318 RetryResult const retry_result = args->func(--args->retry_countdown);
319 if (retry_result == RetryResult::DONE || args->retry_countdown <= 0)
320 return;
321 // second execution of `func` happens after `initial_wait_time`
322 // args->name_ is owned by the shared_ptr<RetryArgs>
323 // which is captured in the lambda and outlives the SchedulerItem
324 const char *static_name = (args->name_type == Scheduler::NameType::STATIC_STRING) ? args->name_.static_name : nullptr;
325 uint32_t hash_or_id = (args->name_type != Scheduler::NameType::STATIC_STRING) ? args->name_.hash_or_id : 0;
326 args->scheduler->set_timer_common_(
327 args->component, Scheduler::SchedulerItem::TIMEOUT, args->name_type, static_name, hash_or_id,
328 args->current_interval, [args]() { retry_handler(args); },
329 /* is_retry= */ true);
330 // backoff_increase_factor applied to third & later executions
331 args->current_interval *= args->backoff_increase_factor;
332}
333
334void HOT Scheduler::set_retry_common_(Component *component, NameType name_type, const char *static_name,
335 uint32_t hash_or_id, uint32_t initial_wait_time, uint8_t max_attempts,
336 std::function<RetryResult(uint8_t)> func, float backoff_increase_factor) {
337 this->cancel_retry_(component, name_type, static_name, hash_or_id);
338
339 if (initial_wait_time == SCHEDULER_DONT_RUN)
340 return;
341
342#ifdef ESPHOME_LOG_HAS_VERY_VERBOSE
343 {
344 SchedulerNameLog name_log;
345 ESP_LOGVV(TAG, "set_retry(name='%s', initial_wait_time=%" PRIu32 ", max_attempts=%u, backoff_factor=%0.1f)",
346 name_log.format(name_type, static_name, hash_or_id), initial_wait_time, max_attempts,
347 backoff_increase_factor);
348 }
349#endif
350
351 if (backoff_increase_factor < 0.0001) {
352 ESP_LOGE(TAG, "set_retry: backoff_factor %0.1f too small, using 1.0: %s", backoff_increase_factor,
353 (name_type == NameType::STATIC_STRING && static_name) ? static_name : "");
354 backoff_increase_factor = 1;
355 }
356
357 auto args = std::make_shared<RetryArgs>();
358 args->func = std::move(func);
359 args->component = component;
360 args->scheduler = this;
361 args->name_type = name_type;
362 if (name_type == NameType::STATIC_STRING) {
363 args->name_.static_name = static_name;
364 } else {
365 args->name_.hash_or_id = hash_or_id;
366 }
367 args->current_interval = initial_wait_time;
368 args->backoff_increase_factor = backoff_increase_factor;
369 args->retry_countdown = max_attempts;
370
371 // First execution of `func` immediately - use set_timer_common_ with is_retry=true
372 this->set_timer_common_(
373 component, SchedulerItem::TIMEOUT, name_type, static_name, hash_or_id, 0, [args]() { retry_handler(args); },
374 /* is_retry= */ true);
375}
376
377void HOT Scheduler::set_retry(Component *component, const char *name, uint32_t initial_wait_time, uint8_t max_attempts,
378 std::function<RetryResult(uint8_t)> func, float backoff_increase_factor) {
379 this->set_retry_common_(component, NameType::STATIC_STRING, name, 0, initial_wait_time, max_attempts, std::move(func),
380 backoff_increase_factor);
381}
382
383bool HOT Scheduler::cancel_retry_(Component *component, NameType name_type, const char *static_name,
384 uint32_t hash_or_id) {
385 return this->cancel_item_(component, name_type, static_name, hash_or_id, SchedulerItem::TIMEOUT,
386 /* match_retry= */ true);
387}
388bool HOT Scheduler::cancel_retry(Component *component, const char *name) {
389 return this->cancel_retry_(component, NameType::STATIC_STRING, name, 0);
390}
391
392void HOT Scheduler::set_retry(Component *component, const std::string &name, uint32_t initial_wait_time,
393 uint8_t max_attempts, std::function<RetryResult(uint8_t)> func,
394 float backoff_increase_factor) {
395 this->set_retry_common_(component, NameType::HASHED_STRING, nullptr, fnv1a_hash(name), initial_wait_time,
396 max_attempts, std::move(func), backoff_increase_factor);
397}
398
399bool HOT Scheduler::cancel_retry(Component *component, const std::string &name) {
400 return this->cancel_retry_(component, NameType::HASHED_STRING, nullptr, fnv1a_hash(name));
401}
402
403void HOT Scheduler::set_retry(Component *component, uint32_t id, uint32_t initial_wait_time, uint8_t max_attempts,
404 std::function<RetryResult(uint8_t)> func, float backoff_increase_factor) {
405 this->set_retry_common_(component, NameType::NUMERIC_ID, nullptr, id, initial_wait_time, max_attempts,
406 std::move(func), backoff_increase_factor);
407}
408
409bool HOT Scheduler::cancel_retry(Component *component, uint32_t id) {
410 return this->cancel_retry_(component, NameType::NUMERIC_ID, nullptr, id);
411}
412
413#pragma GCC diagnostic pop // End suppression of deprecated RetryResult warnings
414
415optional<uint32_t> HOT Scheduler::next_schedule_in(uint32_t now) {
416 // IMPORTANT: This method should only be called from the main thread (loop task).
417 // It performs cleanup and accesses items_[0] without holding a lock, which is only
418 // safe when called from the main thread. Other threads must not call this method.
419
420 // If no items, return empty optional
421 if (!this->cleanup_())
422 return {};
423
424 SchedulerItem *item = this->items_[0];
425 const auto now_64 = this->millis_64_from_(now);
426 const uint64_t next_exec = item->get_next_execution();
427 if (next_exec < now_64)
428 return 0;
429 return next_exec - now_64;
430}
431
432void Scheduler::full_cleanup_removed_items_() {
433 // We hold the lock for the entire cleanup operation because:
434 // 1. We're rebuilding the entire items_ list, so we need exclusive access throughout
435 // 2. Other threads must see either the old state or the new state, not intermediate states
436 // 3. The operation is already expensive (O(n)), so lock overhead is negligible
437 // 4. No operations inside can block or take other locks, so no deadlock risk
438 LockGuard guard{this->lock_};
439
440 // Compact in-place: move valid items forward, recycle removed ones
441 size_t write = 0;
442 for (size_t read = 0; read < this->items_.size(); ++read) {
443 if (!is_item_removed_locked_(this->items_[read])) {
444 if (write != read) {
445 this->items_[write] = this->items_[read];
446 }
447 ++write;
448 } else {
449 this->recycle_item_main_loop_(this->items_[read]);
450 }
451 }
452 this->items_.erase(this->items_.begin() + write, this->items_.end());
453 // Rebuild the heap structure since items are no longer in heap order
454 std::make_heap(this->items_.begin(), this->items_.end(), SchedulerItem::cmp);
455 this->to_remove_clear_();
456}
457
458#ifndef ESPHOME_THREAD_SINGLE
459void Scheduler::compact_defer_queue_locked_() {
460 // Rare case: new items were added during processing - compact the vector
461 // This only happens when:
462 // 1. A deferred callback calls defer() again, or
463 // 2. Another thread calls defer() while we're processing
464 //
465 // Move unprocessed items (added during this loop) to the front for next iteration
466 //
467 // SAFETY: Compacted items may include cancelled items (marked for removal via
468 // cancel_item_locked_() during execution). This is safe because should_skip_item_()
469 // checks is_item_removed_() before executing, so cancelled items will be skipped
470 // and recycled on the next loop iteration.
471 size_t remaining = this->defer_queue_.size() - this->defer_queue_front_;
472 for (size_t i = 0; i < remaining; i++) {
473 this->defer_queue_[i] = this->defer_queue_[this->defer_queue_front_ + i];
474 }
475 // Use erase() instead of resize() to avoid instantiating _M_default_append
476 // (saves ~156 bytes flash). Erasing from the end is O(1) - no shifting needed.
477 this->defer_queue_.erase(this->defer_queue_.begin() + remaining, this->defer_queue_.end());
478}
479void HOT Scheduler::process_defer_queue_slow_path_(uint32_t &now) {
480 // Process defer queue to guarantee FIFO execution order for deferred items.
481 // Previously, defer() used the heap which gave undefined order for equal timestamps,
482 // causing race conditions on multi-core systems (ESP32, BK7200).
483 // With the defer queue:
484 // - Deferred items (delay=0) go directly to defer_queue_ in set_timer_common_
485 // - Items execute in exact order they were deferred (FIFO guarantee)
486 // - No deferred items exist in to_add_, so processing order doesn't affect correctness
487 // Single-core platforms don't use this queue and fall back to the heap-based approach.
488 //
489 // Note: Items cancelled via cancel_item_locked_() are marked with remove=true but still
490 // processed here. They are skipped during execution by should_skip_item_().
491 // This is intentional - no memory leak occurs.
492 //
493 // We use an index (defer_queue_front_) to track the read position instead of calling
494 // erase() on every pop, which would be O(n). The queue is processed once per loop -
495 // any items added during processing are left for the next loop iteration.
496
497 // Merge lock acquisitions: instead of separate locks for move-out and recycle (2N+1 total),
498 // recycle each item after re-acquiring the lock for the next iteration (N+1 total).
499 // The lock is held across: recycle → loop condition → move-out, then released for execution.
500 SchedulerItem *item;
501
502 this->lock_.lock();
503 // Reset counter and snapshot queue end under lock
504 this->defer_count_clear_();
505 size_t defer_queue_end = this->defer_queue_.size();
506 if (this->defer_queue_front_ >= defer_queue_end) {
507 this->lock_.unlock();
508 return;
509 }
510 while (this->defer_queue_front_ < defer_queue_end) {
511 // Take ownership of the item, leaving nullptr in the vector slot.
512 // This is safe because:
513 // 1. The vector is only cleaned up by cleanup_defer_queue_locked_() at the end of this function
514 // 2. Any code iterating defer_queue_ MUST check for nullptr items (see mark_matching_items_removed_locked_)
515 // 3. The lock protects concurrent access, but the nullptr remains until cleanup
516 item = this->defer_queue_[this->defer_queue_front_];
517 this->defer_queue_[this->defer_queue_front_] = nullptr;
518 this->defer_queue_front_++;
519 this->lock_.unlock();
520
521 // Execute callback without holding lock to prevent deadlocks
522 // if the callback tries to call defer() again
523 if (!this->should_skip_item_(item)) {
524 now = this->execute_item_(item, now);
525 }
526
527 this->lock_.lock();
528 this->recycle_item_main_loop_(item);
529 }
530 // Clean up the queue (lock already held from last recycle or initial acquisition)
531 this->cleanup_defer_queue_locked_();
532 this->lock_.unlock();
533}
534#endif /* not ESPHOME_THREAD_SINGLE */
535
536uint32_t HOT Scheduler::call(uint32_t now) {
537#ifndef ESPHOME_THREAD_SINGLE
538 this->process_defer_queue_(now);
539#endif /* not ESPHOME_THREAD_SINGLE */
540
541 // Extend the caller's 32-bit timestamp to 64-bit for scheduler operations
542 const auto now_64 = this->millis_64_from_(now);
543 this->process_to_add();
544
545 // Track if any items were added to to_add_ during callbacks
546 bool has_added_items = false;
547
548#ifdef ESPHOME_DEBUG_SCHEDULER
549 static uint64_t last_print = 0;
550
551 if (now_64 - last_print > 2000) {
552 last_print = now_64;
553 std::vector<SchedulerItem *> old_items;
554 ESP_LOGD(TAG, "Items: count=%zu, pool=%zu, now=%" PRIu64, this->items_.size(), this->scheduler_item_pool_.size(),
555 now_64);
556 // Cleanup before debug output
557 this->cleanup_();
558 while (!this->items_.empty()) {
559 SchedulerItem *item;
560 {
561 LockGuard guard{this->lock_};
562 item = this->pop_raw_locked_();
563 }
564
565 SchedulerNameLog name_log;
566 bool is_cancelled = is_item_removed_(item);
567 ESP_LOGD(TAG, " %s '%s/%s' interval=%" PRIu32 " next_execution in %" PRIu64 "ms at %" PRIu64 "%s",
568 item->get_type_str(), LOG_STR_ARG(item->get_source()),
569 name_log.format(item->get_name_type(), item->get_name(), item->get_name_hash_or_id()), item->interval,
570 item->get_next_execution() - now_64, item->get_next_execution(), is_cancelled ? " [CANCELLED]" : "");
571
572 old_items.push_back(item);
573 }
574 ESP_LOGD(TAG, "\n");
575
576 {
577 LockGuard guard{this->lock_};
578 this->items_ = std::move(old_items);
579 // Rebuild heap after moving items back
580 std::make_heap(this->items_.begin(), this->items_.end(), SchedulerItem::cmp);
581 }
582 }
583#endif /* ESPHOME_DEBUG_SCHEDULER */
584
585 // Cleanup removed items before processing
586 // First try to clean items from the top of the heap (fast path)
587 this->cleanup_();
588
589 // If we still have too many cancelled items, do a full cleanup
590 // This only happens if cancelled items are stuck in the middle/bottom of the heap
591 if (this->to_remove_count_() >= MAX_LOGICALLY_DELETED_ITEMS) {
592 this->full_cleanup_removed_items_();
593 }
594 // IMPORTANT: This loop uses index-based access (items_[0]), NOT iterators.
595 // This is intentional — fired intervals are pushed back into items_ via
596 // push_back() + push_heap() below, which may reallocate the vector's storage.
597 // Index-based access is safe across reallocations because we re-read items_[0]
598 // at the top of each iteration. Do NOT convert this to a range-based for loop
599 // or iterator-based loop, as that would break when items are added.
600 while (!this->items_.empty()) {
601 // Don't copy-by value yet
602 SchedulerItem *item = this->items_[0];
603 if (item->get_next_execution() > now_64) {
604 // Not reached timeout yet, done for this call
605 break;
606 }
607 // Don't run on failed components
608 if (item->component != nullptr && item->component->is_failed()) {
609 LockGuard guard{this->lock_};
610 this->recycle_item_main_loop_(this->pop_raw_locked_());
611 continue;
612 }
613
614 // Check if item is marked for removal
615 // This handles two cases:
616 // 1. Item was marked for removal after cleanup_() but before we got here
617 // 2. Item is marked for removal but wasn't at the front of the heap during cleanup_()
618#ifdef ESPHOME_THREAD_MULTI_NO_ATOMICS
619 // Multi-threaded platforms without atomics: must take lock to safely read remove flag
620 {
621 LockGuard guard{this->lock_};
622 if (is_item_removed_locked_(item)) {
623 this->recycle_item_main_loop_(this->pop_raw_locked_());
624 this->to_remove_decrement_();
625 continue;
626 }
627 }
628#else
629 // Single-threaded or multi-threaded with atomics: can check without lock
630 if (is_item_removed_(item)) {
631 LockGuard guard{this->lock_};
632 this->recycle_item_main_loop_(this->pop_raw_locked_());
633 this->to_remove_decrement_();
634 continue;
635 }
636#endif
637
638#ifdef ESPHOME_DEBUG_SCHEDULER
639 {
640 SchedulerNameLog name_log;
641 ESP_LOGV(TAG, "Running %s '%s/%s' with interval=%" PRIu32 " next_execution=%" PRIu64 " (now=%" PRIu64 ")",
642 item->get_type_str(), LOG_STR_ARG(item->get_source()),
643 name_log.format(item->get_name_type(), item->get_name(), item->get_name_hash_or_id()), item->interval,
644 item->get_next_execution(), now_64);
645 }
646#endif /* ESPHOME_DEBUG_SCHEDULER */
647
648 // Warning: During callback(), a lot of stuff can happen, including:
649 // - timeouts/intervals get added, potentially invalidating vector pointers
650 // - timeouts/intervals get cancelled
651 now = this->execute_item_(item, now);
652
653 LockGuard guard{this->lock_};
654
655 // Only pop after function call, this ensures we were reachable
656 // during the function call and know if we were cancelled.
657 SchedulerItem *executed_item = this->pop_raw_locked_();
658
659 if (this->is_item_removed_locked_(executed_item)) {
660 // We were removed/cancelled in the function call, recycle and continue
661 this->to_remove_decrement_();
662 this->recycle_item_main_loop_(executed_item);
663 continue;
664 }
665
666 if (executed_item->type == SchedulerItem::INTERVAL) {
667 executed_item->set_next_execution(now_64 + executed_item->interval);
668 // Push directly back into the heap instead of routing through to_add_.
669 // This is safe because:
670 // 1. We're on the main loop and already hold the lock
671 // 2. The item was already popped from items_ via pop_raw_locked_() above
672 // 3. The while loop uses index-based access (items_[0]), not iterators,
673 // so push_back() reallocation cannot invalidate our iteration
674 // 4. push_heap() restores the heap invariant before the next iteration
675 // peeks at items_[0]
676 // This avoids the to_add_ detour and the overhead of
677 // process_to_add_slow_path_() (lock acquisition, vector iteration, clear).
678 this->items_.push_back(executed_item);
679 std::push_heap(this->items_.begin(), this->items_.end(), SchedulerItem::cmp);
680 } else {
681 // Timeout completed - recycle it
682 this->recycle_item_main_loop_(executed_item);
683 }
684
685 has_added_items |= !this->to_add_.empty();
686 }
687
688 if (has_added_items) {
689 this->process_to_add();
690 }
691
692#ifdef ESPHOME_DEBUG_SCHEDULER
693 // Verify no items were leaked during this call() cycle.
694 // All items must be in items_, to_add_, defer_queue_, or the pool.
695 // Safe to check here because:
696 // - process_defer_queue_ has already run its cleanup_defer_queue_locked_(),
697 // so defer_queue_ contains no nullptr slots inflating the count.
698 // - The while loop above has finished, so no items are held in local variables;
699 // every item has been returned to a container (items_, to_add_, or pool).
700 // Lock needed to get a consistent snapshot of all containers.
701 {
702 LockGuard guard{this->lock_};
703 this->debug_verify_no_leak_();
704 }
705#endif
706 // execute_item_() advances `now` as items fire; return it so the caller
707 // stays monotonic with last_wdt_feed_.
708 return now;
709}
710void HOT Scheduler::process_to_add_slow_path_() {
711 LockGuard guard{this->lock_};
712 for (auto *&it : this->to_add_) {
713 if (is_item_removed_locked_(it)) {
714 // Recycle cancelled items
715 this->recycle_item_main_loop_(it);
716 it = nullptr;
717 continue;
718 }
719
720 this->items_.push_back(it);
721 std::push_heap(this->items_.begin(), this->items_.end(), SchedulerItem::cmp);
722 }
723 this->to_add_.clear();
724 this->to_add_count_clear_();
725}
726bool HOT Scheduler::cleanup_slow_path_() {
727 // We must hold the lock for the entire cleanup operation because:
728 // 1. We're modifying items_ (via pop_raw_locked_) which requires exclusive access
729 // 2. We're decrementing to_remove_ which is also modified by other threads
730 // (though all modifications are already under lock)
731 // 3. Other threads read items_ when searching for items to cancel in cancel_item_locked_()
732 // 4. We need a consistent view of items_ and to_remove_ throughout the operation
733 // Without the lock, we could access items_ while another thread is reading it,
734 // leading to race conditions
735 LockGuard guard{this->lock_};
736 while (!this->items_.empty()) {
737 SchedulerItem *item = this->items_[0];
738 if (!this->is_item_removed_locked_(item))
739 break;
740 this->to_remove_decrement_();
741 this->recycle_item_main_loop_(this->pop_raw_locked_());
742 }
743 return !this->items_.empty();
744}
745Scheduler::SchedulerItem *HOT Scheduler::pop_raw_locked_() {
746 std::pop_heap(this->items_.begin(), this->items_.end(), SchedulerItem::cmp);
747
748 SchedulerItem *item = this->items_.back();
749 this->items_.pop_back();
750 return item;
751}
752
753// Helper to execute a scheduler item
754uint32_t HOT Scheduler::execute_item_(SchedulerItem *item, uint32_t now) {
755 App.set_current_component(item->component);
756 WarnIfComponentBlockingGuard guard{item->component, now};
757 item->callback();
758 uint32_t end = guard.finish();
759 // Feed the watchdog after each scheduled item (both main heap and defer
760 // queue paths go through here). A run of back-to-back callbacks cannot
761 // starve the wdt. The inline fast path is a load + sub + branch — nearly
762 // free when the 3 ms rate limit hasn't elapsed.
764 return end;
765}
766
767// Common implementation for cancel operations - handles locking
768bool HOT Scheduler::cancel_item_(Component *component, NameType name_type, const char *static_name, uint32_t hash_or_id,
769 SchedulerItem::Type type, bool match_retry) {
770 LockGuard guard{this->lock_};
771 // Public cancel path uses default find_first=false to cancel ALL matches because
772 // DelayAction parallel mode (skip_cancel=true) can create multiple items with the same key.
773 return this->cancel_item_locked_(component, name_type, static_name, hash_or_id, type, match_retry);
774}
775
776// Helper to cancel matching items - must be called with lock held.
777// When find_first=true, stops after the first match and exits across containers
778// (used by set_timer_common_ where cancel-before-add guarantees at most one match).
779// When find_first=false, cancels ALL matches across all containers (needed for
780// public cancel path where DelayAction parallel mode can create duplicates).
781// name_type determines matching: STATIC_STRING uses static_name, others use hash_or_id
782size_t Scheduler::mark_matching_items_removed_slow_locked_(std::vector<SchedulerItem *> &container,
783 Component *component, NameType name_type,
784 const char *static_name, uint32_t hash_or_id,
785 SchedulerItem::Type type, bool match_retry,
786 bool find_first) {
787 size_t count = 0;
788 for (auto *item : container) {
789 if (this->matches_item_locked_(item, component, name_type, static_name, hash_or_id, type, match_retry)) {
790 this->set_item_removed_(item, true);
791 if (find_first)
792 return 1;
793 count++;
794 }
795 }
796 return count;
797}
798
799bool HOT Scheduler::cancel_item_locked_(Component *component, NameType name_type, const char *static_name,
800 uint32_t hash_or_id, SchedulerItem::Type type, bool match_retry,
801 bool find_first) {
802 // Early return if static string name is invalid
803 if (name_type == NameType::STATIC_STRING && static_name == nullptr) {
804 return false;
805 }
806
807 size_t total_cancelled = 0;
808
809#ifndef ESPHOME_THREAD_SINGLE
810 // Mark items in defer queue as cancelled (they'll be skipped when processed)
811 if (type == SchedulerItem::TIMEOUT) {
812 total_cancelled += this->mark_matching_items_removed_locked_(this->defer_queue_, component, name_type, static_name,
813 hash_or_id, type, match_retry, find_first);
814 if (find_first && total_cancelled > 0)
815 return true;
816 }
817#endif /* not ESPHOME_THREAD_SINGLE */
818
819 // Cancel items in the main heap
820 // We only mark items for removal here - never recycle directly.
821 // The main loop may be executing an item's callback right now, and recycling
822 // would destroy the callback while it's running (use-after-free).
823 // Only the main loop in call() should recycle items after execution completes.
824 {
825 size_t heap_cancelled = this->mark_matching_items_removed_locked_(this->items_, component, name_type, static_name,
826 hash_or_id, type, match_retry, find_first);
827 total_cancelled += heap_cancelled;
828 this->to_remove_add_(heap_cancelled);
829 if (find_first && total_cancelled > 0)
830 return true;
831 }
832
833 // Cancel items in to_add_
834 total_cancelled += this->mark_matching_items_removed_locked_(this->to_add_, component, name_type, static_name,
835 hash_or_id, type, match_retry, find_first);
836
837 return total_cancelled > 0;
838}
839
840bool HOT Scheduler::SchedulerItem::cmp(SchedulerItem *a, SchedulerItem *b) {
841 // High bits are almost always equal (change only on 32-bit rollover ~49 days)
842 // Optimize for common case: check low bits first when high bits are equal
843 return (a->next_execution_high_ == b->next_execution_high_) ? (a->next_execution_low_ > b->next_execution_low_)
844 : (a->next_execution_high_ > b->next_execution_high_);
845}
846
847// Recycle a SchedulerItem back to the pool for reuse.
848// IMPORTANT: Caller must hold the scheduler lock before calling this function.
849// This protects scheduler_item_pool_ from concurrent access by other threads
850// that may be acquiring items from the pool in set_timer_common_().
851void Scheduler::recycle_item_main_loop_(SchedulerItem *item) {
852 if (item == nullptr)
853 return;
854
855 if (this->scheduler_item_pool_.size() < MAX_POOL_SIZE) {
856 // Clear callback to release captured resources
857 item->callback = nullptr;
858 this->scheduler_item_pool_.push_back(item);
859#ifdef ESPHOME_DEBUG_SCHEDULER
860 ESP_LOGD(TAG, "Recycled item to pool (pool size now: %zu)", this->scheduler_item_pool_.size());
861#endif
862 } else {
863#ifdef ESPHOME_DEBUG_SCHEDULER
864 ESP_LOGD(TAG, "Pool full (size: %zu), deleting item", this->scheduler_item_pool_.size());
865#endif
866 delete item;
867#ifdef ESPHOME_DEBUG_SCHEDULER
868 this->debug_live_items_--;
869#endif
870 }
871}
872
873#ifdef ESPHOME_DEBUG_SCHEDULER
874void Scheduler::debug_log_timer_(const SchedulerItem *item, NameType name_type, const char *static_name,
875 uint32_t hash_or_id, SchedulerItem::Type type, uint32_t delay, uint64_t now) {
876 // Validate static strings in debug mode
877 if (name_type == NameType::STATIC_STRING && static_name != nullptr) {
878 validate_static_string(static_name);
879 }
880
881 // Debug logging
882 SchedulerNameLog name_log;
883 const char *type_str = (type == SchedulerItem::TIMEOUT) ? "timeout" : "interval";
884 if (type == SchedulerItem::TIMEOUT) {
885 ESP_LOGD(TAG, "set_%s(name='%s/%s', %s=%" PRIu32 ")", type_str, LOG_STR_ARG(item->get_source()),
886 name_log.format(name_type, static_name, hash_or_id), type_str, delay);
887 } else {
888 ESP_LOGD(TAG, "set_%s(name='%s/%s', %s=%" PRIu32 ", offset=%" PRIu32 ")", type_str, LOG_STR_ARG(item->get_source()),
889 name_log.format(name_type, static_name, hash_or_id), type_str, delay,
890 static_cast<uint32_t>(item->get_next_execution() - now));
891 }
892}
893#endif /* ESPHOME_DEBUG_SCHEDULER */
894
895// Helper to get or create a scheduler item from the pool
896// IMPORTANT: Caller must hold the scheduler lock before calling this function.
897Scheduler::SchedulerItem *Scheduler::get_item_from_pool_locked_() {
898 if (!this->scheduler_item_pool_.empty()) {
899 SchedulerItem *item = this->scheduler_item_pool_.back();
900 this->scheduler_item_pool_.pop_back();
901#ifdef ESPHOME_DEBUG_SCHEDULER
902 ESP_LOGD(TAG, "Reused item from pool (pool size now: %zu)", this->scheduler_item_pool_.size());
903#endif
904 return item;
905 }
906#ifdef ESPHOME_DEBUG_SCHEDULER
907 ESP_LOGD(TAG, "Allocated new item (pool empty)");
908#endif
909 auto *item = new SchedulerItem();
910#ifdef ESPHOME_DEBUG_SCHEDULER
911 this->debug_live_items_++;
912#endif
913 return item;
914}
915
916#ifdef ESPHOME_DEBUG_SCHEDULER
917bool Scheduler::debug_verify_no_leak_() const {
918 // Invariant: every live SchedulerItem must be in exactly one container.
919 // debug_live_items_ tracks allocations minus deletions.
920 size_t accounted = this->items_.size() + this->to_add_.size() + this->scheduler_item_pool_.size();
921#ifndef ESPHOME_THREAD_SINGLE
922 accounted += this->defer_queue_.size();
923#endif
924 if (accounted != this->debug_live_items_) {
925 ESP_LOGE(TAG,
926 "SCHEDULER LEAK DETECTED: live=%" PRIu32 " but accounted=%" PRIu32 " (items=%" PRIu32 " to_add=%" PRIu32
927 " pool=%" PRIu32
928#ifndef ESPHOME_THREAD_SINGLE
929 " defer=%" PRIu32
930#endif
931 ")",
932 static_cast<uint32_t>(this->debug_live_items_), static_cast<uint32_t>(accounted),
933 static_cast<uint32_t>(this->items_.size()), static_cast<uint32_t>(this->to_add_.size()),
934 static_cast<uint32_t>(this->scheduler_item_pool_.size())
935#ifndef ESPHOME_THREAD_SINGLE
936 ,
937 static_cast<uint32_t>(this->defer_queue_.size())
938#endif
939 );
940 assert(false);
941 return false;
942 }
943 return true;
944}
945#endif
946
947} // namespace esphome
void ESPHOME_ALWAYS_INLINE feed_wdt_with_time(uint32_t time)
Feed the task watchdog, hot entry.
void set_current_component(Component *component)
const LogString * get_component_log_str() const ESPHOME_ALWAYS_INLINE
Get the integration where this component was declared as a LogString for logging.
Definition component.h:342
ESPDEPRECATED("set_retry is deprecated and will be removed in 2026.8.0. Use set_timeout or set_interval instead.", "2026.2.0") void set_retry(const std uint32_t uint8_t std::function< RetryResult(uint8_t)> float backoff_increase_factor
Definition component.h:454
const Component * component
Definition component.cpp:34
uint16_t type
static float float b
const char *const TAG
Definition spi.cpp:7
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
const char int const __FlashStringHelper * format
Definition log.h:74
void retry_handler(const std::shared_ptr< RetryArgs > &args)
const char int const __FlashStringHelper va_list args
Definition log.h:74
uint64_t HOT millis_64()
Definition core.cpp:27
uint32_t random_uint32()
Return a random 32-bit unsigned integer.
Definition helpers.cpp:12
void HOT delay(uint32_t ms)
Definition core.cpp:28
Application App
Global storage of Application pointer - only one Application can exist.
constexpr uint32_t fnv1a_hash(const char *str)
Calculate a FNV-1a hash of str.
Definition helpers.h:822
constexpr uint32_t SCHEDULER_DONT_RUN
Definition component.h:60
static void uint32_t
uint8_t end[39]
Definition sun_gtil2.cpp:17