Merge branch 'api-handshake-timeout' into integration

This commit is contained in:
J. Nick Koston
2026-02-18 10:05:07 -06:00
2 changed files with 16 additions and 2 deletions

View File

@@ -60,6 +60,11 @@ static constexpr uint8_t MAX_MESSAGES_PER_LOOP = 5;
static constexpr uint8_t MAX_PING_RETRIES = 60;
static constexpr uint16_t PING_RETRY_INTERVAL = 1000;
static constexpr uint32_t KEEPALIVE_DISCONNECT_TIMEOUT = (KEEPALIVE_TIMEOUT_MS * 5) / 2;
// Timeout for completing the handshake (Noise transport + HelloRequest).
// A stalled handshake from a buggy client or network glitch holds a connection
// slot, which can prevent legitimate clients from reconnecting. Also hardens
// against the less likely case of intentional connection slot exhaustion.
static constexpr uint32_t HANDSHAKE_TIMEOUT_MS = 15000;
static constexpr auto ESPHOME_VERSION_REF = StringRef::from_lit(ESPHOME_VERSION);
@@ -223,6 +228,15 @@ void APIConnection::loop() {
this->process_active_iterator_();
}
// Disconnect clients that haven't completed the handshake in time.
// Stale half-open connections from buggy clients or network issues can
// accumulate and block legitimate clients from reconnecting.
if (!this->is_authenticated() && now - this->last_traffic_ > HANDSHAKE_TIMEOUT_MS) {
this->on_fatal_error();
this->log_client_(ESPHOME_LOG_LEVEL_WARN, LOG_STR("handshake timeout; disconnecting"));
return;
}
if (this->flags_.sent_ping) {
// Disconnect if not responded within 2.5*keepalive
if (now - this->last_traffic_ > KEEPALIVE_DISCONNECT_TIMEOUT) {

View File

@@ -1,4 +1,4 @@
cryptography==45.0.1
cryptography==46.0.5
voluptuous==0.16.0
PyYAML==6.0.3
paho-mqtt==1.6.1
@@ -18,7 +18,7 @@ puremagic==1.30
ruamel.yaml==0.19.1 # dashboard_import
ruamel.yaml.clib==0.2.15 # dashboard_import
esphome-glyphsets==0.2.0
pillow==11.3.0
pillow==12.1.1
resvg-py==0.2.6
freetype-py==2.5.1
jinja2==3.1.6