From a71030c4de2548acdbb60306d77e260a134d853e Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 6 Jul 2025 10:40:19 -0500 Subject: [PATCH] fix race --- esphome/core/scheduler.cpp | 12 ++++++------ esphome/core/scheduler.h | 5 ++++- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/esphome/core/scheduler.cpp b/esphome/core/scheduler.cpp index 5c01b4f3f4..525525dbc3 100644 --- a/esphome/core/scheduler.cpp +++ b/esphome/core/scheduler.cpp @@ -65,13 +65,13 @@ void HOT Scheduler::set_timer_common_(Component *component, SchedulerItem::Type const char *name_cstr = is_static_string ? static_cast(name_ptr) : static_cast(name_ptr)->c_str(); - // Cancel existing timer if name is not empty - if (name_cstr != nullptr && name_cstr[0] != '\0') { - this->cancel_item_(component, name_cstr, type); - } - - if (delay == SCHEDULER_DONT_RUN) + if (delay == SCHEDULER_DONT_RUN) { + // Cancel existing timer if name is not empty + if (name_cstr != nullptr && name_cstr[0] != '\0') { + this->cancel_item_(component, name_cstr, type); + } return; + } const auto now = this->millis_(); diff --git a/esphome/core/scheduler.h b/esphome/core/scheduler.h index a64968932e..b3c69068b5 100644 --- a/esphome/core/scheduler.h +++ b/esphome/core/scheduler.h @@ -2,6 +2,7 @@ #include #include +#include #include "esphome/core/component.h" #include "esphome/core/helpers.h" @@ -135,10 +136,12 @@ class Scheduler { void set_timer_common_(Component *component, SchedulerItem::Type type, bool is_static_string, const void *name_ptr, uint32_t delay, std::function func); + // Helper to cancel items by name - must be called with lock held + bool cancel_item_locked_(Component *component, const char *name, SchedulerItem::Type type); + uint64_t millis_(); void cleanup_(); void pop_raw_(); - void push_(std::unique_ptr item); // Common implementation for cancel operations bool cancel_item_common_(Component *component, bool is_static_string, const void *name_ptr, SchedulerItem::Type type);