Skip to content

Commit e53a32d

Browse files
committed
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 <marco.casaroli@gmail.com>
1 parent 7fba6cc commit e53a32d

2 files changed

Lines changed: 18 additions & 12 deletions

File tree

‎net/tcp/tcp_connect.c‎

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ int psock_tcp_connect(FAR struct socket *psock,
298298
FAR struct tcp_conn_s *conn;
299299
struct tcp_connect_s state;
300300
struct tcp_callback_s info;
301+
unsigned int timeout;
301302
int ret = OK;
302303

303304
/* Interrupts must be disabled through all of the following because
@@ -388,11 +389,12 @@ int psock_tcp_connect(FAR struct socket *psock,
388389
/* Wait for either the connect to complete or for an
389390
* error/timeout to occur.
390391
* NOTES: conn_dev_sem_timedwait will also terminate if a
391-
* signal is received.
392+
* signal is received or the send timeout expires.
392393
*/
393394

394395
tls_cleanup_push(tls_get_info(), tcp_callback_cleanup, &info);
395-
ret = conn_dev_sem_timedwait(&state.tc_sem, true, UINT_MAX,
396+
timeout = _SO_TIMEOUT(conn->sconn.s_sndtimeo);
397+
ret = conn_dev_sem_timedwait(&state.tc_sem, true, timeout,
396398
&conn->sconn, conn->dev);
397399

398400
tls_cleanup_pop(tls_get_info(), 0);
@@ -401,22 +403,26 @@ int psock_tcp_connect(FAR struct socket *psock,
401403

402404
nxsem_destroy(&state.tc_sem);
403405

404-
/* If conn_dev_sem_timedwait failed, negated errno was
405-
* returned.
406+
/* The event handler may have set the result before the wait
407+
* ended. SO_SNDTIMEO ends the wait, not the connection: the
408+
* connection goes on as for a non-blocking socket.
406409
*/
407410

408-
if (ret >= 0)
411+
if (ret >= 0 || state.tc_result != -EAGAIN)
409412
{
410-
/* If the wait succeeded, then get the new error
411-
* value from the state structure
412-
*/
413-
414413
ret = state.tc_result;
415414
}
415+
else if (ret == -ETIMEDOUT)
416+
{
417+
ret = -EINPROGRESS;
418+
}
416419

417-
/* Make sure that no further events are processed */
420+
/* Make sure that no further events are processed. With
421+
* EINPROGRESS the connection goes on, as after a success.
422+
*/
418423

419-
psock_teardown_callbacks(&state, ret);
424+
psock_teardown_callbacks(&state,
425+
ret != -EINPROGRESS ? ret : OK);
420426
}
421427
}
422428

‎net/tcp/tcp_monitor.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ int tcp_start_monitor(FAR struct socket *psock)
260260
/* Non-blocking connection ? */
261261

262262
nonblock_conn = (conn->tcpstateflags == TCP_SYN_SENT &&
263-
_SS_ISNONBLOCK(conn->sconn.s_flags));
263+
_SS_ISCONNECTING(conn->sconn.s_flags));
264264

265265
/* Check if the connection has already been closed before any callbacks
266266
* have been registered. (Maybe the connection is lost before accept has

0 commit comments

Comments
 (0)