Fix TCP crash

This commit is contained in:
2025-06-10 20:19:06 -04:00
parent 66445a9698
commit 96a83027ca

View File

@@ -123,6 +123,7 @@ TCPServer::~TCPServer() {
auto that = static_cast<TCPServer *>(args); auto that = static_cast<TCPServer *>(args);
while (true) { while (true) {
printf("top of loop\n");
fd_set readfds; fd_set readfds;
FD_ZERO(&readfds); FD_ZERO(&readfds);
int max_fd = -1; int max_fd = -1;
@@ -139,6 +140,7 @@ TCPServer::~TCPServer() {
if (ret > 0) { if (ret > 0) {
xSemaphoreTake(that->m_mutex, portMAX_DELAY); xSemaphoreTake(that->m_mutex, portMAX_DELAY);
std::vector<int> to_remove;
for (int sock : that->m_clients) { for (int sock : that->m_clients) {
if (FD_ISSET(sock, &readfds)) { if (FD_ISSET(sock, &readfds)) {
// Handle socket // Handle socket
@@ -150,7 +152,7 @@ TCPServer::~TCPServer() {
} else if (0 == len) { } else if (0 == len) {
printf("Connection closed\n"); printf("Connection closed\n");
close(sock); close(sock);
that->m_clients.erase(sock); to_remove.emplace_back(sock);
} else { } else {
// todo: send to rx queue // todo: send to rx queue
buffer[len] = 0; // temp: Null-terminate whatever is received and treat it like a string buffer[len] = 0; // temp: Null-terminate whatever is received and treat it like a string
@@ -158,6 +160,11 @@ TCPServer::~TCPServer() {
} }
} }
} }
for (const auto r : to_remove) {
that->m_clients.erase(r);
}
xSemaphoreGive(that->m_mutex); xSemaphoreGive(that->m_mutex);
} }
} }