From a0681bce6bf7a367e59f2be4654178dde5673127 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 10 Mar 2026 15:09:29 -1000 Subject: [PATCH] [socket] Combine shift and tcp_arg update into single loop in accept() --- esphome/components/socket/lwip_raw_tcp_impl.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/esphome/components/socket/lwip_raw_tcp_impl.cpp b/esphome/components/socket/lwip_raw_tcp_impl.cpp index 77b8c1aadba..c09ccd59f90 100644 --- a/esphome/components/socket/lwip_raw_tcp_impl.cpp +++ b/esphome/components/socket/lwip_raw_tcp_impl.cpp @@ -753,18 +753,16 @@ std::unique_ptr LWIPRawListenImpl::accept(struct sockaddr *addr, so // The error callback nulled their pcb pointers; clean up buffered data and discard. while (this->accepted_socket_count_ > 0) { QueuedPcb entry = this->accepted_pcbs_[0]; - // Shift remaining entries forward and update tcp_arg pointers (slots shifted by one). + // Shift remaining entries forward, updating tcp_arg pointers as we go. // Safe because we hold LWIP_LOCK, so err/recv callbacks can't fire during the update. for (uint8_t i = 1; i < this->accepted_socket_count_; i++) { this->accepted_pcbs_[i - 1] = this->accepted_pcbs_[i]; + if (this->accepted_pcbs_[i - 1].pcb != nullptr) { + tcp_arg(this->accepted_pcbs_[i - 1].pcb, &this->accepted_pcbs_[i - 1]); + } } this->accepted_pcbs_[this->accepted_socket_count_ - 1] = {}; this->accepted_socket_count_--; - for (uint8_t i = 0; i < this->accepted_socket_count_; i++) { - if (this->accepted_pcbs_[i].pcb != nullptr) { - tcp_arg(this->accepted_pcbs_[i].pcb, &this->accepted_pcbs_[i]); - } - } if (entry.pcb == nullptr) { // PCB was freed by lwip (RST/timeout) while queued — discard and try next if (entry.rx_buf != nullptr) {