From 0cda83d29c17ba4cff7d9a9987fb3a5205412a31 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 4 Jul 2025 13:46:39 -0500 Subject: [PATCH 1/2] Update scheduler.cpp Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- esphome/core/scheduler.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/esphome/core/scheduler.cpp b/esphome/core/scheduler.cpp index bd39447c11..9f9fb75290 100644 --- a/esphome/core/scheduler.cpp +++ b/esphome/core/scheduler.cpp @@ -234,10 +234,11 @@ void HOT Scheduler::call() { // The outer check is done without a lock for performance. If the queue // appears non-empty, we lock and process an item. We don't need to check // empty() again inside the lock because only this thread can remove items. - this->lock_.lock(); - auto item = std::move(this->defer_queue_.front()); - this->defer_queue_.pop_front(); - this->lock_.unlock(); + { + LockGuard lock(this->lock_); + auto item = std::move(this->defer_queue_.front()); + this->defer_queue_.pop_front(); + } // Execute callback without holding lock to prevent deadlocks // if the callback tries to call defer() again From debef6fde42704c1ef78553d2311f0192bbdf7d5 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 4 Jul 2025 13:54:07 -0500 Subject: [PATCH 2/2] address review comments --- esphome/core/scheduler.cpp | 10 ++++++---- esphome/core/scheduler.h | 1 + 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/esphome/core/scheduler.cpp b/esphome/core/scheduler.cpp index bd39447c11..515f6fd355 100644 --- a/esphome/core/scheduler.cpp +++ b/esphome/core/scheduler.cpp @@ -234,10 +234,12 @@ void HOT Scheduler::call() { // The outer check is done without a lock for performance. If the queue // appears non-empty, we lock and process an item. We don't need to check // empty() again inside the lock because only this thread can remove items. - this->lock_.lock(); - auto item = std::move(this->defer_queue_.front()); - this->defer_queue_.pop_front(); - this->lock_.unlock(); + std::unique_ptr item; + { + LockGuard lock(this->lock_); + item = std::move(this->defer_queue_.front()); + this->defer_queue_.pop_front(); + } // Execute callback without holding lock to prevent deadlocks // if the callback tries to call defer() again diff --git a/esphome/core/scheduler.h b/esphome/core/scheduler.h index 060ec34da9..bf5e63cccf 100644 --- a/esphome/core/scheduler.h +++ b/esphome/core/scheduler.h @@ -143,6 +143,7 @@ class Scheduler { // Common implementation for cancel operations bool cancel_item_common_(Component *component, bool is_static_string, const void *name_ptr, SchedulerItem::Type type); + private: bool cancel_item_(Component *component, const std::string &name, SchedulerItem::Type type); bool cancel_item_(Component *component, const char *name, SchedulerItem::Type type);