From fc2a5b52f7d9f02eb43931414767635e5cf4c8c1 Mon Sep 17 00:00:00 2001
From: jiangheng <jiangheng14@huawei.com>
Date: Tue, 11 Oct 2022 21:47:24 +0800
Subject: [PATCH] alloc socket fail clean sock

---
 src/api/sockets.c      | 3 +++
 src/include/lwipsock.h | 1 +
 2 files changed, 4 insertions(+)

diff --git a/src/api/sockets.c b/src/api/sockets.c
index 14f2b35..3552599 100644
--- a/src/api/sockets.c
+++ b/src/api/sockets.c
@@ -598,6 +598,9 @@ alloc_socket(struct netconn *newconn, int accepted)
     sockets[i].sendevent  = (NETCONNTYPE_GROUP(newconn->type) == NETCONN_TCP ? (accepted != 0) : 1);
     sockets[i].errevent   = 0;
     return i + LWIP_SOCKET_OFFSET;
+  } else {
+    lwip_close(i);
+    gazelle_clean_sock(i);
   }
 
 err:
diff --git a/src/include/lwipsock.h b/src/include/lwipsock.h
index 912d471..2ffb077 100644
--- a/src/include/lwipsock.h
+++ b/src/include/lwipsock.h
@@ -130,6 +130,7 @@ extern void add_recv_list(int32_t fd);
 extern ssize_t read_lwip_data(struct lwip_sock *sock, int32_t flags, u8_t apiflags);
 extern struct pbuf *write_lwip_data(struct lwip_sock *sock, uint16_t remain_size, uint8_t *apiflags);
 extern void gazelle_init_sock(int32_t fd);
+extern void gazelle_clean_sock(int32_t fd);
 extern void write_lwip_over(struct lwip_sock *sock, uint32_t n);
 #endif /* USE_LIBOS */
 
-- 
2.23.0

