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 a9007ebe4a624e3629e17bdca3eec785276ba8d8
Author: Bowen Wang <[email protected]>
AuthorDate: Wed Nov 25 01:34:35 2026 +0800

    drivers/misc/rpmsgdev_server: keep a blocking read waiting on -EAGAIN
    
    After POLLIN the read work does one non-blocking file_read() and replies
    with its result. When another reader took the data first, or the
    wakeup was spurious, that result is -EAGAIN, and the client's blocking
    read() fails with it. Before the server held blocking reads, the client
    retried on -EAGAIN.
    
    Local reader on the server, with a delay before the work's file_read()
    to lose the race (sim):
    
      server> cat /dev/p126fifo1 &
      proxy>  cat /dev/server-fifo1 &
      server> echo e1one > /dev/p126fifo1
      e1one                                  <- server's local cat
      nsh: cat: read failed: 11              <- proxy
    
    On -EAGAIN poll the device again instead of replying. A running read
    work can now arm the poll, so rpmsgdev_read_cancel() stops the work
    from doing that and cancels it before tearing the poll down.
    
    Signed-off-by: Bowen Wang <[email protected]>
---
 drivers/misc/rpmsgdev_server.c | 30 +++++++++++++++++++++++++++---
 1 file changed, 27 insertions(+), 3 deletions(-)

diff --git a/drivers/misc/rpmsgdev_server.c b/drivers/misc/rpmsgdev_server.c
index 9e463573596..040d014ede6 100644
--- a/drivers/misc/rpmsgdev_server.c
+++ b/drivers/misc/rpmsgdev_server.c
@@ -55,6 +55,7 @@ struct rpmsgdev_read_pend_s
   struct pollfd_s        fd;
   struct rpmsgdev_read_s rsp;
   struct work_s          work;
+  bool                   cancel;
 };
 
 struct rpmsgdev_device_s
@@ -207,6 +208,13 @@ out:
 
 static void rpmsgdev_read_cancel(FAR struct rpmsgdev_device_s *dev)
 {
+  /* A running read work may poll the device again: stop it first, then
+   * tear down the poll, then drop a work the poll queued meanwhile.
+   */
+
+  dev->readpend.cancel = true;
+  work_cancel_sync_wq(g_rpmsgdev_wqueue, &dev->readpend.work);
+
   if (dev->readpend.fd.priv != NULL)
     {
       file_poll(&dev->file, &dev->readpend.fd, false);
@@ -273,6 +281,12 @@ static int rpmsgdev_read_nonblock_handler(FAR struct 
rpmsg_endpoint *ept,
     }
 
   ret = file_read(filep, rsp->buf, space);
+  if (ret == -EAGAIN && msg->header.command == RPMSGDEV_READ)
+    {
+      rpmsg_release_tx_buffer(ept, rsp);
+      return ret;
+    }
+
   rsp->header.result = ret;
   if (ret < 0)
     {
@@ -301,10 +315,19 @@ static void rpmsgdev_read_work(FAR void *arg)
     {
       rpmsg_send(&server->ept, rsp, sizeof(*rsp) - 1);
     }
-  else
+  else if (rpmsgdev_read_nonblock_handler(&server->ept, rsp, sizeof(*rsp),
+                                          0, NULL) == -EAGAIN &&
+           !pend->cancel)
     {
-      rpmsgdev_read_nonblock_handler(&server->ept, rsp,
-                                     sizeof(*rsp), 0, NULL);
+      /* Another reader took the data: wait for the next POLLIN */
+
+      pend->fd.revents = 0;
+      pend->fd.cb      = rpmsgdev_read_cb;
+      rsp->header.result = file_poll(&dev->file, &pend->fd, true);
+      if (rsp->header.result < 0)
+        {
+          rpmsg_send(&server->ept, rsp, sizeof(*rsp) - 1);
+        }
     }
 }
 
@@ -353,6 +376,7 @@ static int rpmsgdev_read_handler(FAR struct rpmsg_endpoint 
*ept,
   pend->fd.revents = 0;
   pend->fd.cb      = rpmsgdev_read_cb;
   pend->fd.arg     = server;
+  pend->cancel     = false;
 
   memcpy(rsp, msg, sizeof(*msg));
 

Reply via email to