[api] Fix ESP8266 noise API handshake deadlock and prompt socket cleanup

Two fixes for ESP8266 with noise encryption:

1. Cache socket ready() before the handshake loop. On ESP8266 LWIP raw
   TCP, ready() returns the live state (false once rx buffer is consumed),
   unlike ESP32 where it is cached until the next main loop. Re-checking
   each iteration blocked handshake writes that must follow reads,
   deadlocking the handshake.

2. Process client removal immediately after loop() instead of deferring
   to the next server loop iteration. This closes the socket promptly
   to free LWIP PCB resources and prevent retransmit crashes on ESP8266.
This commit is contained in:
J. Nick Koston
2026-02-12 17:54:32 -06:00
parent 136d17366f
commit 375fc1db84
2 changed files with 12 additions and 6 deletions

View File

@@ -148,12 +148,16 @@ void APIServer::loop() {
while (client_index < this->clients_.size()) {
auto &client = this->clients_[client_index];
// Common case: process active client
if (!client->flags_.remove) {
client->loop();
}
// Handle disconnection promptly - close socket to free LWIP PCB
// resources and prevent retransmit crashes on ESP8266.
if (client->flags_.remove) {
// Rare case: handle disconnection (don't increment - swapped element needs processing)
this->remove_client_(client_index);
} else {
// Common case: process active client
client->loop();
client_index++;
}
}