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 1d0030b377f5b4c6a38911ffb54358935743eec0
Author: Marco Casaroli <[email protected]>
AuthorDate: Thu Nov 26 08:35:28 2026 +0100

    net/tcp: Keep the handshake of a connect() that a signal interrupts.
    
    A blocking connect() that a signal interrupted returned EINTR, but it
    also tore the connection down without a monitor: the connection stayed
    in SYN_SENT, the socket never became connected, and the next connect()
    returned EISCONN.
    
    POSIX: "If connect() is interrupted by a signal that is caught while
    blocked waiting to establish a connection, connect() shall fail and set
    errno to [EINTR], but the connection request shall not be aborted, and
    the connection shall be established asynchronously."  Linux does this.
    
    Now psock_tcp_connect() handles EINTR as the SO_SNDTIMEO timeout: the
    connection goes on with its monitor, and connect() returns EINTR.
    psock_connect() keeps _SF_CONNECTING for EINTR, and tcp_pollsetup()
    handles a connecting socket as a non-blocking connection: poll() gives
    POLLOUT when the handshake ends, and a second connect() gives EALREADY
    while it goes on.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 net/socket/connect.c  |  6 ++++--
 net/tcp/tcp_connect.c | 12 +++++++-----
 net/tcp/tcp_netpoll.c |  7 +++++--
 3 files changed, 16 insertions(+), 9 deletions(-)

diff --git a/net/socket/connect.c b/net/socket/connect.c
index 3465801f86d..911beaedb86 100644
--- a/net/socket/connect.c
+++ b/net/socket/connect.c
@@ -158,9 +158,11 @@ int psock_connect(FAR struct socket *psock, FAR const 
struct sockaddr *addr,
       conn->s_flags |= _SF_CONNECTED;
     }
 
-  /* EALREADY: an earlier connect() is still in progress */
+  /* EALREADY: an earlier connect() is still in progress.  EINTR: the
+   * connection goes on after the signal (POSIX).
+   */
 
-  if (ret != -EINPROGRESS && ret != -EALREADY &&
+  if (ret != -EINPROGRESS && ret != -EALREADY && ret != -EINTR &&
       (ret < 0 || !connstate))
     {
       conn->s_flags &= ~_SF_CONNECTING;
diff --git a/net/tcp/tcp_connect.c b/net/tcp/tcp_connect.c
index b5e283c4d4a..dcecf5a45d1 100644
--- a/net/tcp/tcp_connect.c
+++ b/net/tcp/tcp_connect.c
@@ -405,7 +405,8 @@ int psock_tcp_connect(FAR struct socket *psock,
 
               /* The event handler may have set the result before the wait
                * ended.  SO_SNDTIMEO ends the wait, not the connection: the
-               * connection goes on as for a non-blocking socket.
+               * connection goes on as for a non-blocking socket.  A signal
+               * also ends only the wait (POSIX): connect() gives EINTR.
                */
 
               if (ret >= 0 || state.tc_result != -EAGAIN)
@@ -418,17 +419,18 @@ int psock_tcp_connect(FAR struct socket *psock,
                 }
 
               /* Make sure that no further events are processed.  With
-               * EINPROGRESS the connection goes on, as after a success.
+               * EINPROGRESS or EINTR the connection goes on, as after a
+               * success.
                */
 
-              psock_teardown_callbacks(&state,
-                                       ret != -EINPROGRESS ? ret : OK);
+              psock_teardown_callbacks(&state, ret != -EINPROGRESS &&
+                                               ret != -EINTR ? ret : OK);
             }
         }
 
       /* Check if the socket was successfully connected. */
 
-      if (ret >= 0 || ret == -EINPROGRESS)
+      if (ret >= 0 || ret == -EINPROGRESS || ret == -EINTR)
         {
           int ret2;
 
diff --git a/net/tcp/tcp_netpoll.c b/net/tcp/tcp_netpoll.c
index e142045bd1b..ec277cdbd9c 100644
--- a/net/tcp/tcp_netpoll.c
+++ b/net/tcp/tcp_netpoll.c
@@ -247,11 +247,14 @@ int tcp_pollsetup(FAR struct socket *psock, FAR struct 
pollfd_s *fds)
 
   conn_dev_lock(&conn->sconn, conn->dev);
 
-  /* Non-blocking connection ? */
+  /* Non-blocking connection, or a blocking connect() that a signal
+   * ended while the handshake goes on?
+   */
 
   nonblock_conn = ((conn->tcpstateflags == TCP_ALLOCATED ||
                     conn->tcpstateflags == TCP_SYN_SENT) &&
-                   _SS_ISNONBLOCK(conn->sconn.s_flags));
+                   (_SS_ISNONBLOCK(conn->sconn.s_flags) ||
+                    _SS_ISCONNECTING(conn->sconn.s_flags)));
 
   /* Find a container to hold the poll information */
 

Reply via email to