diff --git a/esphome/components/api/api_connection.h b/esphome/components/api/api_connection.h index d47423b517..15d79a25ec 100644 --- a/esphome/components/api/api_connection.h +++ b/esphome/components/api/api_connection.h @@ -23,10 +23,7 @@ static constexpr uint32_t KEEPALIVE_TIMEOUT_MS = 60000; // TODO: Remove MAX_INITIAL_PER_BATCH_LEGACY before 2026.7.0 - all clients should support API 1.14 by then static constexpr size_t MAX_INITIAL_PER_BATCH_LEGACY = 24; // For clients < API 1.14 (includes object_id) static constexpr size_t MAX_INITIAL_PER_BATCH = 34; // For clients >= API 1.14 (no object_id) -// Maximum number of messages to process in a single batch -// This limit exists to prevent stack overflow from the MessageInfo/iovec arrays in process_batch_ -// Each MessageInfo is 6 bytes, each iovec is 8 bytes: 34 * (6 + 8) = 476 bytes -static constexpr size_t MAX_MESSAGES_PER_BATCH = 34; +// Verify MAX_MESSAGES_PER_BATCH (defined in api_frame_helper.h) can hold the initial batch static_assert(MAX_MESSAGES_PER_BATCH >= MAX_INITIAL_PER_BATCH, "MAX_MESSAGES_PER_BATCH must be >= MAX_INITIAL_PER_BATCH"); diff --git a/esphome/components/api/api_frame_helper.h b/esphome/components/api/api_frame_helper.h index 07997ac7a0..76a93d094e 100644 --- a/esphome/components/api/api_frame_helper.h +++ b/esphome/components/api/api_frame_helper.h @@ -29,6 +29,10 @@ static constexpr uint16_t MAX_MESSAGE_SIZE = 8192; // 8 KiB for ESP8266 static constexpr uint16_t MAX_MESSAGE_SIZE = 32768; // 32 KiB for ESP32 and other platforms #endif +// Maximum number of messages to batch in a single write operation +// Must be >= MAX_INITIAL_PER_BATCH in api_connection.h (enforced by static_assert there) +static constexpr size_t MAX_MESSAGES_PER_BATCH = 34; + class ProtoWriteBuffer; // Max client name length (e.g., "Home Assistant 2026.1.0.dev0" = 28 chars)