From ffb2fe5f10f3d4d162757cf808d98efeb4dd4ef7 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 4 Apr 2026 10:44:45 -1000 Subject: [PATCH] Stop setting USE_SOCKET_SELECT_SUPPORT on fast-select platforms USE_SOCKET_SELECT_SUPPORT now exclusively means 'host select() fallback'. ESP32/LibreTiny use USE_LWIP_FAST_SELECT instead. This eliminates all 'USE_SOCKET_SELECT_SUPPORT && !USE_LWIP_FAST_SELECT' guards. --- esphome/components/socket/__init__.py | 4 +-- esphome/components/socket/socket.cpp | 2 +- esphome/core/application.cpp | 10 +++---- esphome/core/application.h | 41 +++++++++------------------ esphome/core/defines.h | 2 -- 5 files changed, 22 insertions(+), 37 deletions(-) diff --git a/esphome/components/socket/__init__.py b/esphome/components/socket/__init__.py index cbe61cb872..ab4f3269a8 100644 --- a/esphome/components/socket/__init__.py +++ b/esphome/components/socket/__init__.py @@ -161,16 +161,16 @@ async def to_code(config): cg.add_define("USE_SOCKET_IMPL_LWIP_TCP") elif impl == IMPLEMENTATION_LWIP_SOCKETS: cg.add_define("USE_SOCKET_IMPL_LWIP_SOCKETS") - cg.add_define("USE_SOCKET_SELECT_SUPPORT") elif impl == IMPLEMENTATION_BSD_SOCKETS: cg.add_define("USE_SOCKET_IMPL_BSD_SOCKETS") - cg.add_define("USE_SOCKET_SELECT_SUPPORT") # ESP32 and LibreTiny both have LwIP >= 2.1.3 with lwip_socket_dbg_get_socket() # and FreeRTOS task notifications — enable fast select to bypass lwip_select(). # Only when not using lwip_tcp, which does not provide select() support. if (CORE.is_esp32 or CORE.is_libretiny) and impl != IMPLEMENTATION_LWIP_TCP: cg.add_build_flag("-DUSE_LWIP_FAST_SELECT") elif impl != IMPLEMENTATION_LWIP_TCP: + # Host platform: uses select() syscall for socket monitoring + cg.add_define("USE_SOCKET_SELECT_SUPPORT") # Platforms with select() but without fast select (host) need a UDP # loopback socket for wake_loop_threadsafe(). consume_sockets(1, "socket.wake_loop_threadsafe", SocketType.UDP)({}) diff --git a/esphome/components/socket/socket.cpp b/esphome/components/socket/socket.cpp index bfb6ae8e13..4b8a32ed1d 100644 --- a/esphome/components/socket/socket.cpp +++ b/esphome/components/socket/socket.cpp @@ -8,7 +8,7 @@ namespace esphome::socket { -#if defined(USE_SOCKET_SELECT_SUPPORT) && !defined(USE_LWIP_FAST_SELECT) +#ifdef USE_SOCKET_SELECT_SUPPORT // Shared ready() implementation for fd-based socket implementations (BSD and LWIP sockets). // Checks if the Application's select() loop has marked this fd as ready. bool socket_ready_fd(int fd, bool loop_monitored) { return !loop_monitored || App.is_socket_ready_(fd); } diff --git a/esphome/core/application.cpp b/esphome/core/application.cpp index 26486aa3a1..3df82e25a5 100644 --- a/esphome/core/application.cpp +++ b/esphome/core/application.cpp @@ -128,13 +128,13 @@ void Application::setup() { clear_setup_priority_overrides(); #endif -#if defined(USE_SOCKET_SELECT_SUPPORT) && defined(USE_LWIP_FAST_SELECT) +#ifdef USE_LWIP_FAST_SELECT // Initialize fast select: saves main loop task handle for xTaskNotifyGive wake. // The fast path (rcvevent reads + ulTaskNotifyTake) is used unconditionally // when USE_LWIP_FAST_SELECT is enabled (ESP32 and LibreTiny). esphome_lwip_fast_select_init(); #endif -#if defined(USE_SOCKET_SELECT_SUPPORT) && !defined(USE_LWIP_FAST_SELECT) +#ifdef USE_SOCKET_SELECT_SUPPORT // Set up wake socket for waking main loop from tasks (platforms without fast select only) this->setup_wake_loop_threadsafe_(); #endif @@ -547,7 +547,7 @@ void Application::unregister_socket_fd(int fd) { #endif // Only the select() fallback path remains in the .cpp — all other paths are inlined in application.h -#if defined(USE_SOCKET_SELECT_SUPPORT) && !defined(USE_LWIP_FAST_SELECT) +#ifdef USE_SOCKET_SELECT_SUPPORT void Application::yield_with_select_(uint32_t delay_ms) { // Fallback select() path (host platform and any future platforms without fast select). if (!this->socket_fds_.empty()) [[likely]] { @@ -597,7 +597,7 @@ void Application::yield_with_select_(uint32_t delay_ms) { // No sockets registered or select() failed - use regular delay delay(delay_ms); } -#endif // defined(USE_SOCKET_SELECT_SUPPORT) && !defined(USE_LWIP_FAST_SELECT) +#endif // USE_SOCKET_SELECT_SUPPORT // App storage — asm label shares the linker symbol with "extern Application App". // char[] is trivially destructible, so no __cxa_atexit or destructor chain is emitted. @@ -620,7 +620,7 @@ alignas(Application) char app_storage[sizeof(Application)] asm( // Host platform wake_loop_threadsafe() and setup — needs wake_socket_fd_ // ESP32/LibreTiny/ESP8266/RP2040 implementations are in wake.cpp -#if defined(USE_SOCKET_SELECT_SUPPORT) && !defined(USE_LWIP_FAST_SELECT) +#ifdef USE_SOCKET_SELECT_SUPPORT void Application::setup_wake_loop_threadsafe_() { // Create UDP socket for wake notifications diff --git a/esphome/core/application.h b/esphome/core/application.h index cc1aba77ca..a5bacde997 100644 --- a/esphome/core/application.h +++ b/esphome/core/application.h @@ -25,20 +25,9 @@ #endif #ifdef USE_SOCKET_SELECT_SUPPORT -#ifdef USE_LWIP_FAST_SELECT -#include "esphome/core/lwip_fast_select.h" -#ifdef USE_ESP32 -#include -#include -#else -#include -#include -#endif -#else #include #include #endif -#endif // USE_SOCKET_SELECT_SUPPORT #ifdef USE_RUNTIME_STATS #include "esphome/components/runtime_stats/runtime_stats.h" #endif @@ -565,7 +554,7 @@ class Application { protected: friend Component; -#if defined(USE_SOCKET_SELECT_SUPPORT) && !defined(USE_LWIP_FAST_SELECT) +#ifdef USE_SOCKET_SELECT_SUPPORT friend bool socket::socket_ready_fd(int fd, bool loop_monitored); #endif #ifdef USE_RUNTIME_STATS @@ -573,11 +562,11 @@ class Application { #endif friend void ::setup(); friend void ::original_setup(); -#if defined(USE_SOCKET_SELECT_SUPPORT) && !defined(USE_LWIP_FAST_SELECT) +#ifdef USE_SOCKET_SELECT_SUPPORT friend void wake_loop_threadsafe(); // Host platform accesses wake_socket_fd_ #endif -#if defined(USE_SOCKET_SELECT_SUPPORT) && !defined(USE_LWIP_FAST_SELECT) +#ifdef USE_SOCKET_SELECT_SUPPORT bool is_socket_ready_(int fd) const { return FD_ISSET(fd, &this->read_fds_); } #endif @@ -622,14 +611,14 @@ class Application { void feed_wdt_arch_(); /// Perform a delay while also monitoring socket file descriptors for readiness -#if defined(USE_SOCKET_SELECT_SUPPORT) && !defined(USE_LWIP_FAST_SELECT) +#ifdef USE_SOCKET_SELECT_SUPPORT // select() fallback path is too complex to inline (host platform) void yield_with_select_(uint32_t delay_ms); #else inline void ESPHOME_ALWAYS_INLINE yield_with_select_(uint32_t delay_ms); #endif -#if defined(USE_SOCKET_SELECT_SUPPORT) && !defined(USE_LWIP_FAST_SELECT) +#ifdef USE_SOCKET_SELECT_SUPPORT void setup_wake_loop_threadsafe_(); // Create wake notification socket inline void drain_wake_notifications_(); // Read pending wake notifications in main loop (hot path - inlined) #endif @@ -663,9 +652,7 @@ class Application { std::vector socket_fds_; // Vector of all monitored socket file descriptors #endif #ifdef USE_SOCKET_SELECT_SUPPORT -#if !defined(USE_LWIP_FAST_SELECT) int wake_socket_fd_{-1}; // Shared wake notification socket for waking main loop from tasks -#endif #endif // StringRef members (8 bytes each: pointer + size) @@ -676,7 +663,7 @@ class Application { uint32_t last_loop_{0}; uint32_t loop_component_start_time_{0}; -#if defined(USE_SOCKET_SELECT_SUPPORT) && !defined(USE_LWIP_FAST_SELECT) +#ifdef USE_SOCKET_SELECT_SUPPORT int max_fd_{-1}; // Highest file descriptor number for select() #endif @@ -692,11 +679,11 @@ class Application { bool in_loop_{false}; volatile bool has_pending_enable_loop_requests_{false}; -#if defined(USE_SOCKET_SELECT_SUPPORT) && !defined(USE_LWIP_FAST_SELECT) +#ifdef USE_SOCKET_SELECT_SUPPORT bool socket_fds_changed_{false}; // Flag to rebuild base_read_fds_ when socket_fds_ changes #endif -#if defined(USE_SOCKET_SELECT_SUPPORT) && !defined(USE_LWIP_FAST_SELECT) +#ifdef USE_SOCKET_SELECT_SUPPORT // Variable-sized members (not needed with fast select — is_socket_ready_ reads rcvevent directly) fd_set read_fds_{}; // Working fd_set: populated by select() fd_set base_read_fds_{}; // Cached fd_set rebuilt only when socket_fds_ changes @@ -789,7 +776,7 @@ class Application { /// Global storage of Application pointer - only one Application can exist. extern Application App; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) -#if defined(USE_SOCKET_SELECT_SUPPORT) && !defined(USE_LWIP_FAST_SELECT) +#ifdef USE_SOCKET_SELECT_SUPPORT // Inline implementations for hot-path functions // drain_wake_notifications_() is called on every loop iteration @@ -811,10 +798,10 @@ inline void Application::drain_wake_notifications_() { } } } -#endif // defined(USE_SOCKET_SELECT_SUPPORT) && !defined(USE_LWIP_FAST_SELECT) +#endif // USE_SOCKET_SELECT_SUPPORT inline void ESPHOME_ALWAYS_INLINE Application::before_loop_tasks_(uint32_t loop_start_time) { -#if defined(USE_SOCKET_SELECT_SUPPORT) && !defined(USE_LWIP_FAST_SELECT) +#ifdef USE_SOCKET_SELECT_SUPPORT // Drain wake notifications first to clear socket for next wake this->drain_wake_notifications_(); #endif @@ -905,9 +892,9 @@ inline void ESPHOME_ALWAYS_INLINE Application::loop() { } // Inline yield_with_select_ for all paths except the select() fallback -#if !defined(USE_SOCKET_SELECT_SUPPORT) || defined(USE_LWIP_FAST_SELECT) +#ifndef USE_SOCKET_SELECT_SUPPORT inline void ESPHOME_ALWAYS_INLINE Application::yield_with_select_(uint32_t delay_ms) { -#if defined(USE_SOCKET_SELECT_SUPPORT) && defined(USE_LWIP_FAST_SELECT) +#ifdef USE_LWIP_FAST_SELECT // Fast path (ESP32/LibreTiny): reads rcvevent directly from cached lwip_sock pointers. // Safe because this runs on the main loop which owns socket lifetime (create, read, close). if (delay_ms == 0) [[unlikely]] { @@ -934,6 +921,6 @@ inline void ESPHOME_ALWAYS_INLINE Application::yield_with_select_(uint32_t delay esphome::wakeable_delay(delay_ms); #endif } -#endif // !defined(USE_SOCKET_SELECT_SUPPORT) || defined(USE_LWIP_FAST_SELECT) +#endif // !USE_SOCKET_SELECT_SUPPORT } // namespace esphome diff --git a/esphome/core/defines.h b/esphome/core/defines.h index 0bd9418b9d..576865ced8 100644 --- a/esphome/core/defines.h +++ b/esphome/core/defines.h @@ -251,7 +251,6 @@ #define USE_SENDSPIN #define USE_SENDSPIN_PORT 8928 // NOLINT #define USE_SOCKET_IMPL_BSD_SOCKETS -#define USE_SOCKET_SELECT_SUPPORT #define USE_LWIP_FAST_SELECT #define USE_SPEAKER @@ -377,7 +376,6 @@ #ifdef USE_LIBRETINY #define USE_CAPTIVE_PORTAL #define USE_SOCKET_IMPL_LWIP_SOCKETS -#define USE_SOCKET_SELECT_SUPPORT #define USE_LWIP_FAST_SELECT #define USE_WEBSERVER #define USE_WEBSERVER_AUTH