This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 87862b710c4f21c2dc89231f9d2024efd6c94c84
Author: Marco Casaroli <[email protected]>
AuthorDate: Thu Nov 26 08:34:57 2026 +0100

    net/tcp: Return EALREADY or EISCONN for a second connect().
    
    A second connect() while the handshake of the first one was still in
    progress (a non-blocking socket in SYN_SENT) reached tcp_connect(),
    which returned EISCONN.  POSIX and Linux return EALREADY.  Now
    inet_connect() returns EALREADY for a TCP connection in SYN_SENT, and
    psock_connect() keeps _SF_CONNECTING for it: the first connect() is
    still in progress, and poll() must still report its end.
    
    A connect() of a listening socket started a connection from the
    listener: the connection of a listener is in TCP_ALLOCATED, so
    tcp_connect() accepted it.  Now inet_connect() returns EISCONN for a
    listening socket, as Linux does.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 net/inet/inet_sockif.c | 12 ++++++++++--
 net/socket/connect.c   |  5 ++++-
 2 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/net/inet/inet_sockif.c b/net/inet/inet_sockif.c
index fff983d5533..4e9d08082d9 100644
--- a/net/inet/inet_sockif.c
+++ b/net/inet/inet_sockif.c
@@ -1302,13 +1302,21 @@ static int inet_connect(FAR struct socket *psock,
         {
           FAR struct tcp_conn_s *conn = psock->s_conn;
 
-          /* Verify that the socket is not already connected */
+          /* Verify that the socket is not already connected and does not
+           * listen.  A handshake that is still in progress gives EALREADY.
+           */
 
-          if (_SS_ISCONNECTED(conn->sconn.s_flags))
+          if (_SS_ISCONNECTED(conn->sconn.s_flags) ||
+              _SS_ISLISTENING(conn->sconn.s_flags))
             {
               return -EISCONN;
             }
 
+          if ((conn->tcpstateflags & TCP_STATE_MASK) == TCP_SYN_SENT)
+            {
+              return -EALREADY;
+            }
+
 #if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6)
           if (conn->domain != addr->sa_family)
             {
diff --git a/net/socket/connect.c b/net/socket/connect.c
index f2c00927451..3465801f86d 100644
--- a/net/socket/connect.c
+++ b/net/socket/connect.c
@@ -158,7 +158,10 @@ int psock_connect(FAR struct socket *psock, FAR const 
struct sockaddr *addr,
       conn->s_flags |= _SF_CONNECTED;
     }
 
-  if (ret != -EINPROGRESS && (ret < 0 || !connstate))
+  /* EALREADY: an earlier connect() is still in progress */
+
+  if (ret != -EINPROGRESS && ret != -EALREADY &&
+      (ret < 0 || !connstate))
     {
       conn->s_flags &= ~_SF_CONNECTING;
     }

Reply via email to