From 9a9e7194f5bda27ae535f87185e98b7944e77db7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juraj=20Andr=C3=A1ssy?= Date: Wed, 17 Apr 2024 14:05:07 +0200 Subject: [PATCH 1/2] fix: NetworkClient - close the connection in stop() method for all copies referring it --- libraries/Network/src/NetworkClient.cpp | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/libraries/Network/src/NetworkClient.cpp b/libraries/Network/src/NetworkClient.cpp index fb7f2515e9e..e2590a2f43b 100644 --- a/libraries/Network/src/NetworkClient.cpp +++ b/libraries/Network/src/NetworkClient.cpp @@ -162,7 +162,14 @@ class NetworkClientSocketHandle { NetworkClientSocketHandle(int fd) : sockfd(fd) {} ~NetworkClientSocketHandle() { - close(sockfd); + close(); + } + + void close() { + if (sockfd >= 0) { + ::close(sockfd); + sockfd = -1; + } } int fd() { @@ -178,10 +185,12 @@ NetworkClient::NetworkClient(int fd) : _connected(true), _timeout(WIFI_CLIENT_DE } NetworkClient::~NetworkClient() { - stop(); } void NetworkClient::stop() { + if (clientSocketHandle) { + clientSocketHandle->close(); + } clientSocketHandle = NULL; _rxBuffer = NULL; _connected = false; @@ -473,7 +482,7 @@ int NetworkClient::read(uint8_t *buf, size_t size) { int NetworkClient::peek() { int res = -1; - if (_rxBuffer) { + if (fd() >= 0 && _rxBuffer) { res = _rxBuffer->peek(); if (_rxBuffer->failed()) { log_e("fail on fd %d, errno: %d, \"%s\"", fd(), errno, strerror(errno)); @@ -484,7 +493,7 @@ int NetworkClient::peek() { } int NetworkClient::available() { - if (!_rxBuffer) { + if (fd() < 0 || !_rxBuffer) { return 0; } int res = _rxBuffer->available(); @@ -502,6 +511,9 @@ void NetworkClient::clear() { } uint8_t NetworkClient::connected() { + if (fd() == -1 && _connected) { + stop(); + } if (_connected) { uint8_t dummy; int res = recv(fd(), &dummy, 0, MSG_DONTWAIT); From efa7630873f6d208c3614e38f625bf83e6afb06e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci-lite[bot]" <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Date: Fri, 10 May 2024 13:55:07 +0000 Subject: [PATCH 2/2] ci(pre-commit): Apply automatic fixes --- libraries/Network/src/NetworkClient.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/libraries/Network/src/NetworkClient.cpp b/libraries/Network/src/NetworkClient.cpp index e2590a2f43b..7e59d2aa3f3 100644 --- a/libraries/Network/src/NetworkClient.cpp +++ b/libraries/Network/src/NetworkClient.cpp @@ -184,12 +184,11 @@ NetworkClient::NetworkClient(int fd) : _connected(true), _timeout(WIFI_CLIENT_DE _rxBuffer.reset(new NetworkClientRxBuffer(fd)); } -NetworkClient::~NetworkClient() { -} +NetworkClient::~NetworkClient() {} void NetworkClient::stop() { if (clientSocketHandle) { - clientSocketHandle->close(); + clientSocketHandle->close(); } clientSocketHandle = NULL; _rxBuffer = NULL;