This is an automated email from the ASF dual-hosted git repository.

GUIDINGLI pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 14c5d488ff86caff85f54125555a6d4857ebb4b3
Author: zhangshuai39 <[email protected]>
AuthorDate: Thu Nov 12 13:47:39 2026 +0800

    fs/socket: release the listening socket when accept() is canceled
    
    accept4() holds a reference to the listening socket while it waits in
    psock_accept().  If the thread is canceled asynchronously there, it
    is torn down without reaching file_put(), so the listening socket is
    never released: closing its descriptor leaves the address bound and
    a new bind() to it fails with EADDRINUSE.
    
    Push a cleanup handler that drops the reference once the socket has
    been looked up, and pop it on every return path after that.
    
    Signed-off-by: zhangshuai39 <[email protected]>
    Signed-off-by: jingfei <[email protected]>
---
 fs/socket/accept.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/fs/socket/accept.c b/fs/socket/accept.c
index 7b84bdfca2f..cea6f13f434 100644
--- a/fs/socket/accept.c
+++ b/fs/socket/accept.c
@@ -38,6 +38,7 @@
 #include <nuttx/cancelpt.h>
 #include <nuttx/net/net.h>
 #include <nuttx/fs/fs.h>
+#include <nuttx/tls.h>
 #include <nuttx/mm/kmap.h>
 #include <arch/irq.h>
 
@@ -93,6 +94,15 @@ static int map_user_mem(FAR void *uaddr, size_t size, void 
**p_kaddr)
 }
 #endif
 
+/****************************************************************************
+ * Name: accept4_cleanup
+ ****************************************************************************/
+
+static void accept4_cleanup(FAR void *arg)
+{
+  file_put(arg);
+}
+
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
@@ -229,6 +239,10 @@ int accept4(int sockfd, FAR struct sockaddr *addr, FAR 
socklen_t *addrlen,
       goto errout_with_kmap;
     }
 
+  /* Drop the reference to the listening socket if canceled in accept */
+
+  tls_cleanup_push(tls_get_info(), accept4_cleanup, filep);
+
   newsock = fs_heap_zalloc(sizeof(*newsock));
   if (newsock == NULL)
     {
@@ -264,6 +278,7 @@ int accept4(int sockfd, FAR struct sockaddr *addr, FAR 
socklen_t *addrlen,
       goto errout_with_psock;
     }
 
+  tls_cleanup_pop(tls_get_info(), 0);
   file_put(filep);
 
 #ifdef CONFIG_MM_KMAP
@@ -281,6 +296,7 @@ errout_with_alloc:
   fs_heap_free(newsock);
 
 errout_with_filep:
+  tls_cleanup_pop(tls_get_info(), 0);
   file_put(filep);
 
 errout_with_kmap:

Reply via email to