From 7c422381ca208816d035f9e2323911bff737d2e8 Mon Sep 17 00:00:00 2001 From: Zhe Weng Date: Wed, 9 Aug 2023 17:02:53 +0800 Subject: [PATCH] net/tcp: Set SO_ERROR for poll error in setup Normally, `SO_ERROR` for disconn events will be set by `tcp_poll_eventhandler`, but when the socket is closed before poll, we should also set the `SO_ERROR`. On Linux, `tcp_poll` returns `EPOLLERR` event when `sk->sk_err` has value but doesn't let `poll` fail (doesn't set `errno`). https://github.com/torvalds/linux/blob/v6.5/net/ipv4/tcp.c#L594-L596 Note: `sk->sk_err` can be get by socket option `SO_ERROR` on Linux, so `POLLERR` will always be together with `SO_ERROR`. Common libs like curl may try to read `SO_ERROR` on `POLLERR`. Signed-off-by: Zhe Weng --- net/tcp/tcp_netpoll.c | 1 + 1 file changed, 1 insertion(+) diff --git a/net/tcp/tcp_netpoll.c b/net/tcp/tcp_netpoll.c index 796c0a8b0eb53..f4eabc45d9ef6 100644 --- a/net/tcp/tcp_netpoll.c +++ b/net/tcp/tcp_netpoll.c @@ -344,6 +344,7 @@ int tcp_pollsetup(FAR struct socket *psock, FAR struct pollfd *fds) * exceptional event. */ + _SO_CONN_SETERRNO(conn, ENOTCONN); eventset |= POLLERR | POLLHUP; } else if (_SS_ISCONNECTED(conn->sconn.s_flags) &&