From 646c41dced1fa895c94233eb98d56f9fbf0872f9 Mon Sep 17 00:00:00 2001 From: zhanghongyu Date: Mon, 17 Apr 2023 22:01:32 +0800 Subject: [PATCH] tcp_monitor: start_monitor return OK when connect closed by peer If TCP_FIN is received before the user calls accept, the later accept will report an error, then the previously received data cannot be read. operation flow: tcp_server_listen -> tcp_client_connect -> tcp_client_send -> tcp_client_close -> tcp_server_accept(fix this error) -> tcp_server_recv Signed-off-by: zhanghongyu --- net/tcp/tcp_monitor.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/net/tcp/tcp_monitor.c b/net/tcp/tcp_monitor.c index ec4cbf613eb..5c620f4045f 100644 --- a/net/tcp/tcp_monitor.c +++ b/net/tcp/tcp_monitor.c @@ -271,6 +271,18 @@ int tcp_start_monitor(FAR struct socket *psock) tcp_shutdown_monitor(conn, TCP_CLOSE); + /* If the peer close the connection before we call accept, + * in order to allow user to read the readahead data, + * return OK. + */ + + if (conn->tcpstateflags == TCP_CLOSED || + conn->tcpstateflags == TCP_LAST_ACK) + { + net_unlock(); + return OK; + } + /* And return -ENOTCONN to indicate the monitor was not started * because the socket was already disconnected. */