From 5db0c49b8c43009c60ef7c9d1b79938be64489b2 Mon Sep 17 00:00:00 2001 From: "nilesh.kale" Date: Mon, 7 Oct 2024 14:24:43 +0530 Subject: [PATCH] fix(tcp_tranport): Fix handling of select() return value When both readset/writeset and errset are set for a single socket, the HTTP client incorrectly handled the condition, causing premature termination. Added a check to ensure readset/writeset is prioritized before errset. Closes https://github.com/espressif/esp-idf/issues/14673 --- components/tcp_transport/transport_ssl.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/components/tcp_transport/transport_ssl.c b/components/tcp_transport/transport_ssl.c index a11f160fbc..0a3adbac3b 100644 --- a/components/tcp_transport/transport_ssl.c +++ b/components/tcp_transport/transport_ssl.c @@ -174,7 +174,10 @@ static int base_poll_read(esp_transport_handle_t t, int timeout_ms) return remain; } ret = select(ssl->sockfd + 1, &readset, NULL, &errset, esp_transport_utils_ms_to_timeval(timeout_ms, &timeout)); - if (ret > 0 && FD_ISSET(ssl->sockfd, &errset)) { + // The select() function monitors the socket for readiness to read or write, and checks for errors. + // If both an error (errset) and readiness (readset/writeset) are detected simultaneously, + // this code ensures that the pending read data must be handled before we start processing error. + if (ret == 1 && FD_ISSET(ssl->sockfd, &errset)) { int sock_errno = 0; uint32_t optlen = sizeof(sock_errno); getsockopt(ssl->sockfd, SOL_SOCKET, SO_ERROR, &sock_errno, &optlen);