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 0002eec5f242ee825938abe0c02ccde6c4e9d7f4
Author: Bowen Wang <[email protected]>
AuthorDate: Fri Jun 26 22:19:35 2026 +0800

    net/rpmsg: do not send the window update to an unbound peer
    
    rpmsg_socket_wakeup() checked conn->unbind and then called rpmsg_send()
    outside recvlock.  A NS_DESTROY from the peer in that window sets
    conn->unbind and resets ept.dest_addr to RPMSG_ADDR_ANY, and rpmsg_send()
    then waits in is_rpmsg_ept_ready() for the whole RPMSG_TICK_COUNT
    (15 s), blocking the reader that called recv().
    
    Reserve the tx buffer first, outside the lock since that may block on a
    free vring buffer, then check is_rpmsg_ept_ready() and conn->unbind and
    send with rpmsg_send_offchannel_nocopy() under recvlock, which
    rpmsg_socket_ns_unbind() takes as well.  If the peer is gone, release
    the buffer and return -ECONNRESET.
    
    Signed-off-by: Bowen Wang <[email protected]>
---
 net/rpmsg/rpmsg_sockif.c | 52 ++++++++++++++++++++++++++++++++++++++----------
 1 file changed, 42 insertions(+), 10 deletions(-)

diff --git a/net/rpmsg/rpmsg_sockif.c b/net/rpmsg/rpmsg_sockif.c
index 75b13c1dc79..2a369f3fd71 100644
--- a/net/rpmsg/rpmsg_sockif.c
+++ b/net/rpmsg/rpmsg_sockif.c
@@ -331,29 +331,61 @@ static void rpmsg_socket_free(FAR struct 
rpmsg_socket_conn_s *conn)
 
 static int rpmsg_socket_wakeup(FAR struct rpmsg_socket_conn_s *conn)
 {
-  struct rpmsg_socket_data_s msg;
+  FAR struct rpmsg_socket_data_s *msg;
+  uint32_t len = sizeof(*msg);
   uint32_t space;
-  int ret = 0;
+  int ret;
 
   if (!conn->ept.rdev || atomic_read(&conn->unbind))
     {
-      return ret;
+      return 0;
     }
 
   nxmutex_lock(&conn->recvlock);
   space = conn->recvpos - conn->lastpos;
+  nxmutex_unlock(&conn->recvlock);
+
+  if (space <= circbuf_size(&conn->recvbuf) / 2)
+    {
+      return 0;
+    }
 
-  if (space > circbuf_size(&conn->recvbuf) / 2)
+  /* Reserve the tx buffer without holding recvlock, since it may block
+   * waiting for a free vring buffer.
+   */
+
+  msg = rpmsg_get_tx_payload_buffer(&conn->ept, &len, true);
+  if (msg == NULL)
     {
-      conn->lastpos = conn->recvpos;
-      msg.cmd = RPMSG_SOCKET_CMD_DATA;
-      msg.pos = conn->recvpos;
-      msg.len = 0;
-      ret = 1;
+      return -ENOMEM;
     }
 
+  DEBUGASSERT(len >= sizeof(*msg));
+  nxmutex_lock(&conn->recvlock);
+
+  /* The peer may have destroyed its endpoint meanwhile.  rpmsg_send()
+   * would then wait the whole RPMSG_TICK_COUNT for dest_addr to become
+   * valid again, so check and send under recvlock, which
+   * rpmsg_socket_ns_unbind() takes too.
+   */
+
+  if (!is_rpmsg_ept_ready(&conn->ept) || atomic_read(&conn->unbind))
+    {
+      nxmutex_unlock(&conn->recvlock);
+      rpmsg_release_tx_buffer(&conn->ept, msg);
+      return -ECONNRESET;
+    }
+
+  conn->lastpos = conn->recvpos;
+  msg->cmd = RPMSG_SOCKET_CMD_DATA;
+  msg->pos = conn->recvpos;
+  msg->len = 0;
+  ret = rpmsg_send_offchannel_nocopy(&conn->ept, conn->ept.addr,
+                                     conn->ept.dest_addr, msg,
+                                     sizeof(*msg));
+
   nxmutex_unlock(&conn->recvlock);
-  return ret ? rpmsg_send(&conn->ept, &msg, sizeof(msg)) : 0;
+  return ret;
 }
 
 static inline uint32_t

Reply via email to