From bb48077a81dfeb3ba4ff457f3ba0bd2f84d21caf Mon Sep 17 00:00:00 2001 From: Liu Zhi Fu Date: Sun, 16 Sep 2018 18:28:45 +0800 Subject: [PATCH] lwip: fix the bug that failed to create socket Fix the bug that failed to create nonblocking TCP or UDP socket after several times socket create/close --- components/lwip/api/api_msg.c | 45 ++++++++++++++++------------------- components/lwip/api/sockets.c | 9 +++---- 2 files changed, 23 insertions(+), 31 deletions(-) diff --git a/components/lwip/api/api_msg.c b/components/lwip/api/api_msg.c index 531bca235..d6fbff30e 100644 --- a/components/lwip/api/api_msg.c +++ b/components/lwip/api/api_msg.c @@ -945,38 +945,33 @@ lwip_netconn_do_close_internal(struct netconn *conn WRITE_DELAYED_PARAM SIG_CLO #endif /* LWIP_SO_LINGER */ } else { if (err == ERR_MEM) { - /* Closing failed because of memory shortage */ - if (netconn_is_nonblocking(conn)) { - /* Nonblocking close failed */ - close_finished = 1; - err = ERR_WOULDBLOCK; - } else { - /* Blocking close, check the timeout */ + /* Closing failed because of memory shortage, try again later. Even for + nonblocking netconns, we have to wait since no standard socket application + is prepared for close failing because of resource shortage. + Check the timeout: this is kind of an lwip addition to the standard sockets: + we wait for some time when failing to allocate a segment for the FIN */ #if LWIP_SO_SNDTIMEO || LWIP_SO_LINGER - s32_t close_timeout = LWIP_TCP_CLOSE_TIMEOUT_MS_DEFAULT; - /* this is kind of an lwip addition to the standard sockets: we wait - for some time when failing to allocate a segment for the FIN */ + s32_t close_timeout = LWIP_TCP_CLOSE_TIMEOUT_MS_DEFAULT; #if LWIP_SO_SNDTIMEO - if (conn->send_timeout > 0) { - close_timeout = conn->send_timeout; - } + if (conn->send_timeout > 0) { + close_timeout = conn->send_timeout; + } #endif /* LWIP_SO_SNDTIMEO */ #if LWIP_SO_LINGER - if (conn->linger >= 0) { - /* use linger timeout (seconds) */ - close_timeout = conn->linger * 1000U; - } + if (conn->linger >= 0) { + /* use linger timeout (seconds) */ + close_timeout = conn->linger * 1000U; + } #endif - if ((s32_t)(sys_now() - conn->current_msg->msg.sd.time_started) >= close_timeout) { + if ((s32_t)(sys_now() - conn->current_msg->msg.sd.time_started) >= close_timeout) { #else /* LWIP_SO_SNDTIMEO || LWIP_SO_LINGER */ - if (conn->current_msg->msg.sd.polls_left == 0) { + if (conn->current_msg->msg.sd.polls_left == 0) { #endif /* LWIP_SO_SNDTIMEO || LWIP_SO_LINGER */ - close_finished = 1; - if (close) { - /* in this case, we want to RST the connection */ - tcp_abort(tpcb); - err = ERR_OK; - } + close_finished = 1; + if (close) { + /* in this case, we want to RST the connection */ + tcp_abort(tpcb); + err = ERR_OK; } } } else { diff --git a/components/lwip/api/sockets.c b/components/lwip/api/sockets.c index 184b1080b..badde6613 100644 --- a/components/lwip/api/sockets.c +++ b/components/lwip/api/sockets.c @@ -266,11 +266,11 @@ do{\ }\ }while(0) -#define LWIP_SET_CLOSE_FLAG(_flag) \ +#define LWIP_SET_CLOSE_FLAG() \ do{\ LWIP_SOCK_LOCK(__sock);\ LWIP_DEBUGF(ESP_THREAD_SAFE_DEBUG, ("mark sock closing\n"));\ - __sock->state = (_flag);\ + __sock->state = LWIP_SOCK_CLOSING;\ LWIP_SOCK_UNLOCK(__sock);\ }while(0) @@ -3312,11 +3312,8 @@ int lwip_close_r(int s) { LWIP_API_LOCK(); - LWIP_SET_CLOSE_FLAG(LWIP_SOCK_CLOSING); + LWIP_SET_CLOSE_FLAG(); __ret = lwip_close(s); - if (EWOULDBLOCK == __sock->err) { - LWIP_SET_CLOSE_FLAG(LWIP_SOCK_OPEN); - } LWIP_API_UNLOCK(); }