ESPHome 2026.3.0
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 - float math + random_float()
109// only needed for intervals, not timeouts
110uint32_t Scheduler::calculate_interval_offset_(uint32_t delay) {
111 return static_cast<uint32_t>(std::min(delay / 2, MAX_INTERVAL_DELAY) * random_float());
112}
113
114// Check if a retry was already cancelled in items_ or to_add_
115// Extracted from set_timer_common_ to reduce code size - retry path is cold and deprecated
116// Remove before 2026.8.0 along with all retry code
117bool Scheduler::is_retry_cancelled_locked_(Component *component, NameType name_type, const char *static_name,
118 uint32_t hash_or_id) {
119 for (auto *container : {&this->items_, &this->to_add_}) {
120 for (auto *item : *container) {
121 if (item != nullptr && this->is_item_removed_locked_(item) &&
122 this->matches_item_locked_(item, component, name_type, static_name, hash_or_id, SchedulerItem::TIMEOUT,
123 /* match_retry= */ true, /* skip_removed= */ false)) {
124 return true;
125 }
126 }
127 }
128 return false;
129}
130
131// Common implementation for both timeout and interval
132// name_type determines storage type: STATIC_STRING uses static_name, others use hash_or_id
133void HOT Scheduler::set_timer_common_(Component *component, SchedulerItem::Type type, NameType name_type,
134 const char *static_name, uint32_t hash_or_id, uint32_t delay,
135 std::function<void()> &&func, bool is_retry, bool skip_cancel) {
136 if (delay == SCHEDULER_DONT_RUN) {
137 // Still need to cancel existing timer if we have a name/id
138 if (!skip_cancel) {
139 LockGuard guard{this->lock_};
140 this->cancel_item_locked_(component, name_type, static_name, hash_or_id, type);
141 }
142 return;
143 }
144
145 // Take lock early to protect scheduler_item_pool_ access and retry-cancelled check
146 LockGuard guard{this->lock_};
147
148 // For retries, check if there's a cancelled timeout first - before allocating an item.
149 // Skip check for anonymous retries (STATIC_STRING with nullptr) - they can't be cancelled by name
150 // Skip check for defer (delay=0) - deferred retries bypass the cancellation check
151 if (is_retry && delay != 0 && (name_type != NameType::STATIC_STRING || static_name != nullptr) &&
152 type == SchedulerItem::TIMEOUT &&
153 this->is_retry_cancelled_locked_(component, name_type, static_name, hash_or_id)) {
154#ifdef ESPHOME_DEBUG_SCHEDULER
155 SchedulerNameLog skip_name_log;
156 ESP_LOGD(TAG, "Skipping retry '%s' - found cancelled item",
157 skip_name_log.format(name_type, static_name, hash_or_id));
158#endif
159 return;
160 }
161
162 // Create and populate the scheduler item
163 SchedulerItem *item = this->get_item_from_pool_locked_();
164 item->component = component;
165 item->set_name(name_type, static_name, hash_or_id);
166 item->type = type;
167 item->callback = std::move(func);
168 // Reset remove flag - recycled items may have been cancelled (remove=true) in previous use
169 this->set_item_removed_(item, false);
170 item->is_retry = is_retry;
171
172 // Determine target container: defer_queue_ for deferred items, to_add_ for everything else.
173 // Using a pointer lets both paths share the cancel + push_back epilogue.
174 auto *target = &this->to_add_;
175
176#ifndef ESPHOME_THREAD_SINGLE
177 // Special handling for defer() (delay = 0, type = TIMEOUT)
178 // Single-core platforms don't need thread-safe defer handling
179 if (delay == 0 && type == SchedulerItem::TIMEOUT) {
180 // Put in defer queue for guaranteed FIFO execution
181 target = &this->defer_queue_;
182 } else
183#endif /* not ESPHOME_THREAD_SINGLE */
184 {
185 // Only non-defer items need a timestamp for scheduling
186 const uint64_t now_64 = millis_64();
187
188 // Type-specific setup
189 if (type == SchedulerItem::INTERVAL) {
190 item->interval = delay;
191 // first execution happens immediately after a random smallish offset
192 uint32_t offset = this->calculate_interval_offset_(delay);
193 item->set_next_execution(now_64 + offset);
194#ifdef ESPHOME_LOG_HAS_VERBOSE
195 SchedulerNameLog name_log;
196 ESP_LOGV(TAG, "Scheduler interval for %s is %" PRIu32 "ms, offset %" PRIu32 "ms",
197 name_log.format(name_type, static_name, hash_or_id), delay, offset);
198#endif
199 } else {
200 item->interval = 0;
201 item->set_next_execution(now_64 + delay);
202 }
203
204#ifdef ESPHOME_DEBUG_SCHEDULER
205 this->debug_log_timer_(item, name_type, static_name, hash_or_id, type, delay, now_64);
206#endif /* ESPHOME_DEBUG_SCHEDULER */
207 }
208
209 // Common epilogue: atomic cancel-and-add (unless skip_cancel is true)
210 if (!skip_cancel) {
211 this->cancel_item_locked_(component, name_type, static_name, hash_or_id, type);
212 }
213 target->push_back(item);
214 if (target == &this->to_add_) {
215 this->to_add_count_increment_();
216 }
217#ifndef ESPHOME_THREAD_SINGLE
218 else {
219 this->defer_count_increment_();
220 }
221#endif
222}
223
224void HOT Scheduler::set_timeout(Component *component, const char *name, uint32_t timeout,
225 std::function<void()> &&func) {
226 this->set_timer_common_(component, SchedulerItem::TIMEOUT, NameType::STATIC_STRING, name, 0, timeout,
227 std::move(func));
228}
229
230void HOT Scheduler::set_timeout(Component *component, const std::string &name, uint32_t timeout,
231 std::function<void()> &&func) {
232 this->set_timer_common_(component, SchedulerItem::TIMEOUT, NameType::HASHED_STRING, nullptr, fnv1a_hash(name),
233 timeout, std::move(func));
234}
235void HOT Scheduler::set_timeout(Component *component, uint32_t id, uint32_t timeout, std::function<void()> &&func) {
236 this->set_timer_common_(component, SchedulerItem::TIMEOUT, NameType::NUMERIC_ID, nullptr, id, timeout,
237 std::move(func));
238}
239bool HOT Scheduler::cancel_timeout(Component *component, const std::string &name) {
240 return this->cancel_item_(component, NameType::HASHED_STRING, nullptr, fnv1a_hash(name), SchedulerItem::TIMEOUT);
241}
242bool HOT Scheduler::cancel_timeout(Component *component, const char *name) {
243 return this->cancel_item_(component, NameType::STATIC_STRING, name, 0, SchedulerItem::TIMEOUT);
244}
245bool HOT Scheduler::cancel_timeout(Component *component, uint32_t id) {
246 return this->cancel_item_(component, NameType::NUMERIC_ID, nullptr, id, SchedulerItem::TIMEOUT);
247}
248void HOT Scheduler::set_interval(Component *component, const std::string &name, uint32_t interval,
249 std::function<void()> &&func) {
250 this->set_timer_common_(component, SchedulerItem::INTERVAL, NameType::HASHED_STRING, nullptr, fnv1a_hash(name),
251 interval, std::move(func));
252}
253
254void HOT Scheduler::set_interval(Component *component, const char *name, uint32_t interval,
255 std::function<void()> &&func) {
256 this->set_timer_common_(component, SchedulerItem::INTERVAL, NameType::STATIC_STRING, name, 0, interval,
257 std::move(func));
258}
259void HOT Scheduler::set_interval(Component *component, uint32_t id, uint32_t interval, std::function<void()> &&func) {
260 this->set_timer_common_(component, SchedulerItem::INTERVAL, NameType::NUMERIC_ID, nullptr, id, interval,
261 std::move(func));
262}
263bool HOT Scheduler::cancel_interval(Component *component, const std::string &name) {
264 return this->cancel_item_(component, NameType::HASHED_STRING, nullptr, fnv1a_hash(name), SchedulerItem::INTERVAL);
265}
266bool HOT Scheduler::cancel_interval(Component *component, const char *name) {
267 return this->cancel_item_(component, NameType::STATIC_STRING, name, 0, SchedulerItem::INTERVAL);
268}
269bool HOT Scheduler::cancel_interval(Component *component, uint32_t id) {
270 return this->cancel_item_(component, NameType::NUMERIC_ID, nullptr, id, SchedulerItem::INTERVAL);
271}
272
273// Suppress deprecation warnings for RetryResult usage in the still-present (but deprecated) retry implementation.
274// Remove before 2026.8.0 along with all retry code.
275#pragma GCC diagnostic push
276#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
277
278struct RetryArgs {
279 // Ordered to minimize padding on 32-bit systems
280 std::function<RetryResult(uint8_t)> func;
281 Component *component;
282 Scheduler *scheduler;
283 // Union for name storage - only one is used based on name_type
284 union {
285 const char *static_name; // For STATIC_STRING
286 uint32_t hash_or_id; // For HASHED_STRING or NUMERIC_ID
287 } name_;
288 uint32_t current_interval;
289 float backoff_increase_factor;
290 Scheduler::NameType name_type; // Discriminator for name_ union
291 uint8_t retry_countdown;
292};
293
294void retry_handler(const std::shared_ptr<RetryArgs> &args) {
295 RetryResult const retry_result = args->func(--args->retry_countdown);
296 if (retry_result == RetryResult::DONE || args->retry_countdown <= 0)
297 return;
298 // second execution of `func` happens after `initial_wait_time`
299 // args->name_ is owned by the shared_ptr<RetryArgs>
300 // which is captured in the lambda and outlives the SchedulerItem
301 const char *static_name = (args->name_type == Scheduler::NameType::STATIC_STRING) ? args->name_.static_name : nullptr;
302 uint32_t hash_or_id = (args->name_type != Scheduler::NameType::STATIC_STRING) ? args->name_.hash_or_id : 0;
303 args->scheduler->set_timer_common_(
304 args->component, Scheduler::SchedulerItem::TIMEOUT, args->name_type, static_name, hash_or_id,
305 args->current_interval, [args]() { retry_handler(args); },
306 /* is_retry= */ true);
307 // backoff_increase_factor applied to third & later executions
308 args->current_interval *= args->backoff_increase_factor;
309}
310
311void HOT Scheduler::set_retry_common_(Component *component, NameType name_type, const char *static_name,
312 uint32_t hash_or_id, uint32_t initial_wait_time, uint8_t max_attempts,
313 std::function<RetryResult(uint8_t)> func, float backoff_increase_factor) {
314 this->cancel_retry_(component, name_type, static_name, hash_or_id);
315
316 if (initial_wait_time == SCHEDULER_DONT_RUN)
317 return;
318
319#ifdef ESPHOME_LOG_HAS_VERY_VERBOSE
320 {
321 SchedulerNameLog name_log;
322 ESP_LOGVV(TAG, "set_retry(name='%s', initial_wait_time=%" PRIu32 ", max_attempts=%u, backoff_factor=%0.1f)",
323 name_log.format(name_type, static_name, hash_or_id), initial_wait_time, max_attempts,
324 backoff_increase_factor);
325 }
326#endif
327
328 if (backoff_increase_factor < 0.0001) {
329 ESP_LOGE(TAG, "set_retry: backoff_factor %0.1f too small, using 1.0: %s", backoff_increase_factor,
330 (name_type == NameType::STATIC_STRING && static_name) ? static_name : "");
331 backoff_increase_factor = 1;
332 }
333
334 auto args = std::make_shared<RetryArgs>();
335 args->func = std::move(func);
336 args->component = component;
337 args->scheduler = this;
338 args->name_type = name_type;
339 if (name_type == NameType::STATIC_STRING) {
340 args->name_.static_name = static_name;
341 } else {
342 args->name_.hash_or_id = hash_or_id;
343 }
344 args->current_interval = initial_wait_time;
345 args->backoff_increase_factor = backoff_increase_factor;
346 args->retry_countdown = max_attempts;
347
348 // First execution of `func` immediately - use set_timer_common_ with is_retry=true
349 this->set_timer_common_(
350 component, SchedulerItem::TIMEOUT, name_type, static_name, hash_or_id, 0, [args]() { retry_handler(args); },
351 /* is_retry= */ true);
352}
353
354void HOT Scheduler::set_retry(Component *component, const char *name, uint32_t initial_wait_time, uint8_t max_attempts,
355 std::function<RetryResult(uint8_t)> func, float backoff_increase_factor) {
356 this->set_retry_common_(component, NameType::STATIC_STRING, name, 0, initial_wait_time, max_attempts, std::move(func),
357 backoff_increase_factor);
358}
359
360bool HOT Scheduler::cancel_retry_(Component *component, NameType name_type, const char *static_name,
361 uint32_t hash_or_id) {
362 return this->cancel_item_(component, name_type, static_name, hash_or_id, SchedulerItem::TIMEOUT,
363 /* match_retry= */ true);
364}
365bool HOT Scheduler::cancel_retry(Component *component, const char *name) {
366 return this->cancel_retry_(component, NameType::STATIC_STRING, name, 0);
367}
368
369void HOT Scheduler::set_retry(Component *component, const std::string &name, uint32_t initial_wait_time,
370 uint8_t max_attempts, std::function<RetryResult(uint8_t)> func,
371 float backoff_increase_factor) {
372 this->set_retry_common_(component, NameType::HASHED_STRING, nullptr, fnv1a_hash(name), initial_wait_time,
373 max_attempts, std::move(func), backoff_increase_factor);
374}
375
376bool HOT Scheduler::cancel_retry(Component *component, const std::string &name) {
377 return this->cancel_retry_(component, NameType::HASHED_STRING, nullptr, fnv1a_hash(name));
378}
379
380void HOT Scheduler::set_retry(Component *component, uint32_t id, uint32_t initial_wait_time, uint8_t max_attempts,
381 std::function<RetryResult(uint8_t)> func, float backoff_increase_factor) {
382 this->set_retry_common_(component, NameType::NUMERIC_ID, nullptr, id, initial_wait_time, max_attempts,
383 std::move(func), backoff_increase_factor);
384}
385
386bool HOT Scheduler::cancel_retry(Component *component, uint32_t id) {
387 return this->cancel_retry_(component, NameType::NUMERIC_ID, nullptr, id);
388}
389
390#pragma GCC diagnostic pop // End suppression of deprecated RetryResult warnings
391
392optional<uint32_t> HOT Scheduler::next_schedule_in(uint32_t now) {
393 // IMPORTANT: This method should only be called from the main thread (loop task).
394 // It performs cleanup and accesses items_[0] without holding a lock, which is only
395 // safe when called from the main thread. Other threads must not call this method.
396
397 // If no items, return empty optional
398 if (!this->cleanup_())
399 return {};
400
401 SchedulerItem *item = this->items_[0];
402 const auto now_64 = this->millis_64_from_(now);
403 const uint64_t next_exec = item->get_next_execution();
404 if (next_exec < now_64)
405 return 0;
406 return next_exec - now_64;
407}
408
409void Scheduler::full_cleanup_removed_items_() {
410 // We hold the lock for the entire cleanup operation because:
411 // 1. We're rebuilding the entire items_ list, so we need exclusive access throughout
412 // 2. Other threads must see either the old state or the new state, not intermediate states
413 // 3. The operation is already expensive (O(n)), so lock overhead is negligible
414 // 4. No operations inside can block or take other locks, so no deadlock risk
415 LockGuard guard{this->lock_};
416
417 // Compact in-place: move valid items forward, recycle removed ones
418 size_t write = 0;
419 for (size_t read = 0; read < this->items_.size(); ++read) {
420 if (!is_item_removed_locked_(this->items_[read])) {
421 if (write != read) {
422 this->items_[write] = this->items_[read];
423 }
424 ++write;
425 } else {
426 this->recycle_item_main_loop_(this->items_[read]);
427 }
428 }
429 this->items_.erase(this->items_.begin() + write, this->items_.end());
430 // Rebuild the heap structure since items are no longer in heap order
431 std::make_heap(this->items_.begin(), this->items_.end(), SchedulerItem::cmp);
432 this->to_remove_clear_();
433}
434
435#ifndef ESPHOME_THREAD_SINGLE
436void Scheduler::compact_defer_queue_locked_() {
437 // Rare case: new items were added during processing - compact the vector
438 // This only happens when:
439 // 1. A deferred callback calls defer() again, or
440 // 2. Another thread calls defer() while we're processing
441 //
442 // Move unprocessed items (added during this loop) to the front for next iteration
443 //
444 // SAFETY: Compacted items may include cancelled items (marked for removal via
445 // cancel_item_locked_() during execution). This is safe because should_skip_item_()
446 // checks is_item_removed_() before executing, so cancelled items will be skipped
447 // and recycled on the next loop iteration.
448 size_t remaining = this->defer_queue_.size() - this->defer_queue_front_;
449 for (size_t i = 0; i < remaining; i++) {
450 this->defer_queue_[i] = this->defer_queue_[this->defer_queue_front_ + i];
451 }
452 // Use erase() instead of resize() to avoid instantiating _M_default_append
453 // (saves ~156 bytes flash). Erasing from the end is O(1) - no shifting needed.
454 this->defer_queue_.erase(this->defer_queue_.begin() + remaining, this->defer_queue_.end());
455}
456#endif /* not ESPHOME_THREAD_SINGLE */
457
458void HOT Scheduler::call(uint32_t now) {
459#ifndef ESPHOME_THREAD_SINGLE
460 this->process_defer_queue_(now);
461#endif /* not ESPHOME_THREAD_SINGLE */
462
463 // Extend the caller's 32-bit timestamp to 64-bit for scheduler operations
464 const auto now_64 = this->millis_64_from_(now);
465 this->process_to_add();
466
467 // Track if any items were added to to_add_ during this call (intervals or from callbacks)
468 bool has_added_items = false;
469
470#ifdef ESPHOME_DEBUG_SCHEDULER
471 static uint64_t last_print = 0;
472
473 if (now_64 - last_print > 2000) {
474 last_print = now_64;
475 std::vector<SchedulerItem *> old_items;
476 ESP_LOGD(TAG, "Items: count=%zu, pool=%zu, now=%" PRIu64, this->items_.size(), this->scheduler_item_pool_.size(),
477 now_64);
478 // Cleanup before debug output
479 this->cleanup_();
480 while (!this->items_.empty()) {
481 SchedulerItem *item;
482 {
483 LockGuard guard{this->lock_};
484 item = this->pop_raw_locked_();
485 }
486
487 SchedulerNameLog name_log;
488 bool is_cancelled = is_item_removed_(item);
489 ESP_LOGD(TAG, " %s '%s/%s' interval=%" PRIu32 " next_execution in %" PRIu64 "ms at %" PRIu64 "%s",
490 item->get_type_str(), LOG_STR_ARG(item->get_source()),
491 name_log.format(item->get_name_type(), item->get_name(), item->get_name_hash_or_id()), item->interval,
492 item->get_next_execution() - now_64, item->get_next_execution(), is_cancelled ? " [CANCELLED]" : "");
493
494 old_items.push_back(item);
495 }
496 ESP_LOGD(TAG, "\n");
497
498 {
499 LockGuard guard{this->lock_};
500 this->items_ = std::move(old_items);
501 // Rebuild heap after moving items back
502 std::make_heap(this->items_.begin(), this->items_.end(), SchedulerItem::cmp);
503 }
504 }
505#endif /* ESPHOME_DEBUG_SCHEDULER */
506
507 // Cleanup removed items before processing
508 // First try to clean items from the top of the heap (fast path)
509 this->cleanup_();
510
511 // If we still have too many cancelled items, do a full cleanup
512 // This only happens if cancelled items are stuck in the middle/bottom of the heap
513 if (this->to_remove_count_() >= MAX_LOGICALLY_DELETED_ITEMS) {
514 this->full_cleanup_removed_items_();
515 }
516 while (!this->items_.empty()) {
517 // Don't copy-by value yet
518 SchedulerItem *item = this->items_[0];
519 if (item->get_next_execution() > now_64) {
520 // Not reached timeout yet, done for this call
521 break;
522 }
523 // Don't run on failed components
524 if (item->component != nullptr && item->component->is_failed()) {
525 LockGuard guard{this->lock_};
526 this->recycle_item_main_loop_(this->pop_raw_locked_());
527 continue;
528 }
529
530 // Check if item is marked for removal
531 // This handles two cases:
532 // 1. Item was marked for removal after cleanup_() but before we got here
533 // 2. Item is marked for removal but wasn't at the front of the heap during cleanup_()
534#ifdef ESPHOME_THREAD_MULTI_NO_ATOMICS
535 // Multi-threaded platforms without atomics: must take lock to safely read remove flag
536 {
537 LockGuard guard{this->lock_};
538 if (is_item_removed_locked_(item)) {
539 this->recycle_item_main_loop_(this->pop_raw_locked_());
540 this->to_remove_decrement_();
541 continue;
542 }
543 }
544#else
545 // Single-threaded or multi-threaded with atomics: can check without lock
546 if (is_item_removed_(item)) {
547 LockGuard guard{this->lock_};
548 this->recycle_item_main_loop_(this->pop_raw_locked_());
549 this->to_remove_decrement_();
550 continue;
551 }
552#endif
553
554#ifdef ESPHOME_DEBUG_SCHEDULER
555 {
556 SchedulerNameLog name_log;
557 ESP_LOGV(TAG, "Running %s '%s/%s' with interval=%" PRIu32 " next_execution=%" PRIu64 " (now=%" PRIu64 ")",
558 item->get_type_str(), LOG_STR_ARG(item->get_source()),
559 name_log.format(item->get_name_type(), item->get_name(), item->get_name_hash_or_id()), item->interval,
560 item->get_next_execution(), now_64);
561 }
562#endif /* ESPHOME_DEBUG_SCHEDULER */
563
564 // Warning: During callback(), a lot of stuff can happen, including:
565 // - timeouts/intervals get added, potentially invalidating vector pointers
566 // - timeouts/intervals get cancelled
567 now = this->execute_item_(item, now);
568
569 LockGuard guard{this->lock_};
570
571 // Only pop after function call, this ensures we were reachable
572 // during the function call and know if we were cancelled.
573 SchedulerItem *executed_item = this->pop_raw_locked_();
574
575 if (this->is_item_removed_locked_(executed_item)) {
576 // We were removed/cancelled in the function call, recycle and continue
577 this->to_remove_decrement_();
578 this->recycle_item_main_loop_(executed_item);
579 continue;
580 }
581
582 if (executed_item->type == SchedulerItem::INTERVAL) {
583 executed_item->set_next_execution(now_64 + executed_item->interval);
584 // Add new item directly to to_add_
585 // since we have the lock held
586 this->to_add_.push_back(executed_item);
587 this->to_add_count_increment_();
588 } else {
589 // Timeout completed - recycle it
590 this->recycle_item_main_loop_(executed_item);
591 }
592
593 has_added_items |= !this->to_add_.empty();
594 }
595
596 if (has_added_items) {
597 this->process_to_add();
598 }
599
600#ifdef ESPHOME_DEBUG_SCHEDULER
601 // Verify no items were leaked during this call() cycle.
602 // All items must be in items_, to_add_, defer_queue_, or the pool.
603 // Safe to check here because:
604 // - process_defer_queue_ has already run its cleanup_defer_queue_locked_(),
605 // so defer_queue_ contains no nullptr slots inflating the count.
606 // - The while loop above has finished, so no items are held in local variables;
607 // every item has been returned to a container (items_, to_add_, or pool).
608 // Lock needed to get a consistent snapshot of all containers.
609 {
610 LockGuard guard{this->lock_};
611 this->debug_verify_no_leak_();
612 }
613#endif
614}
615void HOT Scheduler::process_to_add() {
616 // Fast path: skip lock acquisition when nothing to add.
617 // Worst case is a one-loop-iteration delay before newly added items are processed.
618 if (this->to_add_empty_())
619 return;
620 LockGuard guard{this->lock_};
621 for (auto *&it : this->to_add_) {
622 if (is_item_removed_locked_(it)) {
623 // Recycle cancelled items
624 this->recycle_item_main_loop_(it);
625 it = nullptr;
626 continue;
627 }
628
629 this->items_.push_back(it);
630 std::push_heap(this->items_.begin(), this->items_.end(), SchedulerItem::cmp);
631 }
632 this->to_add_.clear();
633 this->to_add_count_clear_();
634}
635bool HOT Scheduler::cleanup_() {
636 // Fast path: if nothing to remove, just check if items exist.
637 // Uses atomic load on platforms with atomics, falls back to always taking the lock otherwise.
638 // Worst case is a one-loop-iteration delay in cleanup.
639 if (this->to_remove_empty_())
640 return !this->items_.empty();
641
642 // We must hold the lock for the entire cleanup operation because:
643 // 1. We're modifying items_ (via pop_raw_locked_) which requires exclusive access
644 // 2. We're decrementing to_remove_ which is also modified by other threads
645 // (though all modifications are already under lock)
646 // 3. Other threads read items_ when searching for items to cancel in cancel_item_locked_()
647 // 4. We need a consistent view of items_ and to_remove_ throughout the operation
648 // Without the lock, we could access items_ while another thread is reading it,
649 // leading to race conditions
650 LockGuard guard{this->lock_};
651 while (!this->items_.empty()) {
652 SchedulerItem *item = this->items_[0];
653 if (!this->is_item_removed_locked_(item))
654 break;
655 this->to_remove_decrement_();
656 this->recycle_item_main_loop_(this->pop_raw_locked_());
657 }
658 return !this->items_.empty();
659}
660Scheduler::SchedulerItem *HOT Scheduler::pop_raw_locked_() {
661 std::pop_heap(this->items_.begin(), this->items_.end(), SchedulerItem::cmp);
662
663 SchedulerItem *item = this->items_.back();
664 this->items_.pop_back();
665 return item;
666}
667
668// Helper to execute a scheduler item
669uint32_t HOT Scheduler::execute_item_(SchedulerItem *item, uint32_t now) {
670 App.set_current_component(item->component);
671 WarnIfComponentBlockingGuard guard{item->component, now};
672 item->callback();
673 return guard.finish();
674}
675
676// Common implementation for cancel operations - handles locking
677bool HOT Scheduler::cancel_item_(Component *component, NameType name_type, const char *static_name, uint32_t hash_or_id,
678 SchedulerItem::Type type, bool match_retry) {
679 LockGuard guard{this->lock_};
680 return this->cancel_item_locked_(component, name_type, static_name, hash_or_id, type, match_retry);
681}
682
683// Helper to cancel items - must be called with lock held
684// name_type determines matching: STATIC_STRING uses static_name, others use hash_or_id
685bool HOT Scheduler::cancel_item_locked_(Component *component, NameType name_type, const char *static_name,
686 uint32_t hash_or_id, SchedulerItem::Type type, bool match_retry) {
687 // Early return if static string name is invalid
688 if (name_type == NameType::STATIC_STRING && static_name == nullptr) {
689 return false;
690 }
691
692 size_t total_cancelled = 0;
693
694#ifndef ESPHOME_THREAD_SINGLE
695 // Mark items in defer queue as cancelled (they'll be skipped when processed)
696 if (type == SchedulerItem::TIMEOUT) {
697 total_cancelled += this->mark_matching_items_removed_locked_(this->defer_queue_, component, name_type, static_name,
698 hash_or_id, type, match_retry);
699 }
700#endif /* not ESPHOME_THREAD_SINGLE */
701
702 // Cancel items in the main heap
703 // We only mark items for removal here - never recycle directly.
704 // The main loop may be executing an item's callback right now, and recycling
705 // would destroy the callback while it's running (use-after-free).
706 // Only the main loop in call() should recycle items after execution completes.
707 if (!this->items_.empty()) {
708 size_t heap_cancelled = this->mark_matching_items_removed_locked_(this->items_, component, name_type, static_name,
709 hash_or_id, type, match_retry);
710 total_cancelled += heap_cancelled;
711 this->to_remove_add_(heap_cancelled);
712 }
713
714 // Cancel items in to_add_
715 total_cancelled += this->mark_matching_items_removed_locked_(this->to_add_, component, name_type, static_name,
716 hash_or_id, type, match_retry);
717
718 return total_cancelled > 0;
719}
720
721bool HOT Scheduler::SchedulerItem::cmp(SchedulerItem *a, SchedulerItem *b) {
722 // High bits are almost always equal (change only on 32-bit rollover ~49 days)
723 // Optimize for common case: check low bits first when high bits are equal
724 return (a->next_execution_high_ == b->next_execution_high_) ? (a->next_execution_low_ > b->next_execution_low_)
725 : (a->next_execution_high_ > b->next_execution_high_);
726}
727
728// Recycle a SchedulerItem back to the pool for reuse.
729// IMPORTANT: Caller must hold the scheduler lock before calling this function.
730// This protects scheduler_item_pool_ from concurrent access by other threads
731// that may be acquiring items from the pool in set_timer_common_().
732void Scheduler::recycle_item_main_loop_(SchedulerItem *item) {
733 if (item == nullptr)
734 return;
735
736 if (this->scheduler_item_pool_.size() < MAX_POOL_SIZE) {
737 // Clear callback to release captured resources
738 item->callback = nullptr;
739 this->scheduler_item_pool_.push_back(item);
740#ifdef ESPHOME_DEBUG_SCHEDULER
741 ESP_LOGD(TAG, "Recycled item to pool (pool size now: %zu)", this->scheduler_item_pool_.size());
742#endif
743 } else {
744#ifdef ESPHOME_DEBUG_SCHEDULER
745 ESP_LOGD(TAG, "Pool full (size: %zu), deleting item", this->scheduler_item_pool_.size());
746#endif
747 delete item;
748#ifdef ESPHOME_DEBUG_SCHEDULER
749 this->debug_live_items_--;
750#endif
751 }
752}
753
754#ifdef ESPHOME_DEBUG_SCHEDULER
755void Scheduler::debug_log_timer_(const SchedulerItem *item, NameType name_type, const char *static_name,
756 uint32_t hash_or_id, SchedulerItem::Type type, uint32_t delay, uint64_t now) {
757 // Validate static strings in debug mode
758 if (name_type == NameType::STATIC_STRING && static_name != nullptr) {
759 validate_static_string(static_name);
760 }
761
762 // Debug logging
763 SchedulerNameLog name_log;
764 const char *type_str = (type == SchedulerItem::TIMEOUT) ? "timeout" : "interval";
765 if (type == SchedulerItem::TIMEOUT) {
766 ESP_LOGD(TAG, "set_%s(name='%s/%s', %s=%" PRIu32 ")", type_str, LOG_STR_ARG(item->get_source()),
767 name_log.format(name_type, static_name, hash_or_id), type_str, delay);
768 } else {
769 ESP_LOGD(TAG, "set_%s(name='%s/%s', %s=%" PRIu32 ", offset=%" PRIu32 ")", type_str, LOG_STR_ARG(item->get_source()),
770 name_log.format(name_type, static_name, hash_or_id), type_str, delay,
771 static_cast<uint32_t>(item->get_next_execution() - now));
772 }
773}
774#endif /* ESPHOME_DEBUG_SCHEDULER */
775
776// Helper to get or create a scheduler item from the pool
777// IMPORTANT: Caller must hold the scheduler lock before calling this function.
778Scheduler::SchedulerItem *Scheduler::get_item_from_pool_locked_() {
779 if (!this->scheduler_item_pool_.empty()) {
780 SchedulerItem *item = this->scheduler_item_pool_.back();
781 this->scheduler_item_pool_.pop_back();
782#ifdef ESPHOME_DEBUG_SCHEDULER
783 ESP_LOGD(TAG, "Reused item from pool (pool size now: %zu)", this->scheduler_item_pool_.size());
784#endif
785 return item;
786 }
787#ifdef ESPHOME_DEBUG_SCHEDULER
788 ESP_LOGD(TAG, "Allocated new item (pool empty)");
789#endif
790 auto *item = new SchedulerItem();
791#ifdef ESPHOME_DEBUG_SCHEDULER
792 this->debug_live_items_++;
793#endif
794 return item;
795}
796
797#ifdef ESPHOME_DEBUG_SCHEDULER
798bool Scheduler::debug_verify_no_leak_() const {
799 // Invariant: every live SchedulerItem must be in exactly one container.
800 // debug_live_items_ tracks allocations minus deletions.
801 size_t accounted = this->items_.size() + this->to_add_.size() + this->scheduler_item_pool_.size();
802#ifndef ESPHOME_THREAD_SINGLE
803 accounted += this->defer_queue_.size();
804#endif
805 if (accounted != this->debug_live_items_) {
806 ESP_LOGE(TAG,
807 "SCHEDULER LEAK DETECTED: live=%" PRIu32 " but accounted=%" PRIu32 " (items=%" PRIu32 " to_add=%" PRIu32
808 " pool=%" PRIu32
809#ifndef ESPHOME_THREAD_SINGLE
810 " defer=%" PRIu32
811#endif
812 ")",
813 static_cast<uint32_t>(this->debug_live_items_), static_cast<uint32_t>(accounted),
814 static_cast<uint32_t>(this->items_.size()), static_cast<uint32_t>(this->to_add_.size()),
815 static_cast<uint32_t>(this->scheduler_item_pool_.size())
816#ifndef ESPHOME_THREAD_SINGLE
817 ,
818 static_cast<uint32_t>(this->defer_queue_.size())
819#endif
820 );
821 assert(false);
822 return false;
823 }
824 return true;
825}
826#endif
827
828} // namespace esphome
void set_current_component(Component *component)
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:395
const Component * component
Definition component.cpp:37
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
float random_float()
Return a random float between 0 and 1.
Definition helpers.cpp:159
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
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:674
constexpr uint32_t SCHEDULER_DONT_RUN
Definition component.h:49
static void uint32_t