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

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

commit 14f7caddf2ebdb7b6f74082c34cd32fc98350f07
Author: zhangshuai39 <[email protected]>
AuthorDate: Thu Jan 29 11:22:17 2026 +0800

    net/local: Add a check to see if conn is in a listening state.
    
    Because the file descriptor (fd) obtained by `accept` uses the same address 
as the file descriptor (fd) obtained by `listen`, even if the file descriptor 
has exited, accessing that address again will still show that the address is 
already in use.
    
    Signed-off-by: zhangshuai39 <[email protected]>
---
 net/local/local_bind.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/net/local/local_bind.c b/net/local/local_bind.c
index a66e1b56de3..eec73907957 100644
--- a/net/local/local_bind.c
+++ b/net/local/local_bind.c
@@ -53,6 +53,7 @@ int psock_local_bind(FAR struct socket *psock,
   FAR struct local_conn_s *conn = psock->s_conn;
   FAR const struct sockaddr_un *unaddr =
     (FAR const struct sockaddr_un *)addr;
+  FAR struct local_conn_s *found;
   int index;
 #ifdef CONFIG_NET_LOCAL_DGRAM
   int ret;
@@ -77,10 +78,18 @@ int psock_local_bind(FAR struct socket *psock,
       return -EINVAL;
     }
 
-  /* Check if local address is already in use */
+  /* Check if local address is already in use.
+   * Check for LOCAL_STATE_BOUND or LOCAL_STATE_LISTENING connections.
+   * This ensures that a socket that is bound (or listening) will block
+   * new binds. Note that accepted connections are in LOCAL_STATE_CONNECTED
+   * state, so they won't block new binds, allowing server restart.
+   */
 
   local_lock();
-  if (local_findconn(conn, unaddr) != NULL)
+  found = local_findconn(conn, unaddr);
+  if (found != NULL &&
+      (found->lc_state == LOCAL_STATE_BOUND ||
+       found->lc_state == LOCAL_STATE_LISTENING))
     {
       local_unlock();
       return -EADDRINUSE;

Reply via email to