This is an automated email from the ASF dual-hosted git repository. GUIDINGLI pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git
commit 406254cf2f307f167d45345efffbbe436399dfbb Author: Marco Casaroli <[email protected]> AuthorDate: Thu Nov 26 14:32:16 2026 +0100 netutils/webclient: Wait for the connection after EINTR from connect(). A signal that interrupted the blocking connect() of webclient_perform() made it call connect() again, and it took EISCONN as success. POSIX: after EINTR "the connection request shall not be aborted, and the connection shall be established asynchronously". A second connect() while the handshake goes on gives EALREADY (Linux), so webclient failed; and a connection that never completed was taken as connected. Now webclient waits for the end of the connection with poll(POLLOUT), for the timeout of the context, and takes the result from SO_ERROR. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]> --- netutils/webclient/webclient.c | 78 +++++++++++++++++++++++++++++++++--------- 1 file changed, 62 insertions(+), 16 deletions(-) diff --git a/netutils/webclient/webclient.c b/netutils/webclient/webclient.c index 8d0eec4a2..b5abc61f0 100644 --- a/netutils/webclient/webclient.c +++ b/netutils/webclient/webclient.c @@ -52,6 +52,7 @@ #include <assert.h> #include <fcntl.h> +#include <poll.h> #include <sys/socket.h> #include <sys/time.h> #include <stdint.h> @@ -1073,6 +1074,50 @@ static int wget_gethostip(FAR char *hostname, FAR struct in_addr *dest) #endif } +/**************************************************************************** + * Name: wget_waitconnect + * + * Description: + * Wait for the end of a connection that a signal interrupted in + * connect(): the connection goes on (POSIX). + * + * Input Parameters: + * sockfd - The socket + * timeout - The timeout in seconds, 0 for none (as SO_SNDTIMEO) + * + * Returned Value: + * Zero (OK) when the socket is connected, else a negated errno value. + * + ****************************************************************************/ + +static int wget_waitconnect(int sockfd, unsigned int timeout) +{ + struct pollfd pfd; + socklen_t len = sizeof(int); + int err; + int ret; + + pfd.fd = sockfd; + pfd.events = POLLOUT; + do + { + ret = poll(&pfd, 1, timeout > 0 ? timeout * 1000 : -1); + } + while (ret < 0 && errno == EINTR); + + if (ret <= 0) + { + return ret < 0 ? -errno : -ETIMEDOUT; + } + + if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &err, &len) < 0) + { + return -errno; + } + + return -err; +} + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -1619,25 +1664,26 @@ int webclient_perform(FAR struct webclient_context *ctx) * arbitrary local port that is not in use. */ - while (true) + ret = connect(conn->sockfd, server_address, + server_address_len); + if (ret == -1) { - ret = connect(conn->sockfd, server_address, - server_address_len); - if (ret == -1) - { - ret = -errno; - DEBUGASSERT(ret < 0); - if (ret == -EINTR) - { - continue; - } + ret = -errno; + DEBUGASSERT(ret < 0); - if (ret == -EISCONN) - { - ret = 0; - } + /* After a signal the connection goes on: wait for its + * end. A second connect() would give EALREADY. + */ + + if (ret == -EINTR) + { + ret = wget_waitconnect(conn->sockfd, + ctx->timeout_sec); + } + else if (ret == -EISCONN) + { + ret = 0; } - break; } }
