This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 733d66bed360e0bc04c82fdb9b02ad422cb4bdd6 Author: Marco Casaroli <[email protected]> AuthorDate: Sat Nov 21 14:33:02 2026 +0100 net/tcp: End a blocking connect() at the send timeout. psock_tcp_connect() waits for the handshake without a time limit. A blocking connect() to a peer that drops the SYNs waits until the SYN retransmissions give up, about 70 s, whatever SO_SNDTIMEO says. Linux ends the wait at the send timeout with EINPROGRESS, as if the socket were non-blocking (socket(7), SO_SNDTIMEO), and the connection goes on. Do the same: wait at most SO_SNDTIMEO. When it expires, connect() gives EINPROGRESS and the connection monitor takes the connection over, as for a non-blocking connect(): poll() reports POLLOUT when the handshake completes, and SO_ERROR gives the error of a connection that fails. tcp_start_monitor() takes a connection in TCP_SYN_SENT as one in progress when the socket is connecting, not only when it is non-blocking. A result that the event handler sets just before the wait ends (with the timeout or a signal) is now used, as the handler already tore its callback down. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]> --- net/tcp/tcp_connect.c | 28 +++++++++++++++++----------- net/tcp/tcp_monitor.c | 2 +- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/net/tcp/tcp_connect.c b/net/tcp/tcp_connect.c index 9520d8d4557..b5e283c4d4a 100644 --- a/net/tcp/tcp_connect.c +++ b/net/tcp/tcp_connect.c @@ -298,6 +298,7 @@ int psock_tcp_connect(FAR struct socket *psock, FAR struct tcp_conn_s *conn; struct tcp_connect_s state; struct tcp_callback_s info; + unsigned int timeout; int ret = OK; /* Interrupts must be disabled through all of the following because @@ -388,11 +389,12 @@ int psock_tcp_connect(FAR struct socket *psock, /* Wait for either the connect to complete or for an * error/timeout to occur. * NOTES: conn_dev_sem_timedwait will also terminate if a - * signal is received. + * signal is received or the send timeout expires. */ tls_cleanup_push(tls_get_info(), tcp_callback_cleanup, &info); - ret = conn_dev_sem_timedwait(&state.tc_sem, true, UINT_MAX, + timeout = _SO_TIMEOUT(conn->sconn.s_sndtimeo); + ret = conn_dev_sem_timedwait(&state.tc_sem, true, timeout, &conn->sconn, conn->dev); tls_cleanup_pop(tls_get_info(), 0); @@ -401,22 +403,26 @@ int psock_tcp_connect(FAR struct socket *psock, nxsem_destroy(&state.tc_sem); - /* If conn_dev_sem_timedwait failed, negated errno was - * returned. + /* 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. */ - if (ret >= 0) + if (ret >= 0 || state.tc_result != -EAGAIN) { - /* If the wait succeeded, then get the new error - * value from the state structure - */ - ret = state.tc_result; } + else if (ret == -ETIMEDOUT) + { + ret = -EINPROGRESS; + } - /* Make sure that no further events are processed */ + /* Make sure that no further events are processed. With + * EINPROGRESS the connection goes on, as after a success. + */ - psock_teardown_callbacks(&state, ret); + psock_teardown_callbacks(&state, + ret != -EINPROGRESS ? ret : OK); } } diff --git a/net/tcp/tcp_monitor.c b/net/tcp/tcp_monitor.c index 68c1055dcc4..039462cf353 100644 --- a/net/tcp/tcp_monitor.c +++ b/net/tcp/tcp_monitor.c @@ -268,7 +268,7 @@ int tcp_start_monitor(FAR struct socket *psock) /* Non-blocking connection ? */ nonblock_conn = (conn->tcpstateflags == TCP_SYN_SENT && - _SS_ISNONBLOCK(conn->sconn.s_flags)); + _SS_ISCONNECTING(conn->sconn.s_flags)); /* The peer can close its half of the connection (FIN: TCP_CLOSE_WAIT) * before the monitor is started: a server that accepts and closes at
