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 457531ef741df387dc696cc7e898a57b1295a57b
Author: Marco Casaroli <[email protected]>
AuthorDate: Tue Nov 17 11:42:19 2026 +0100

    fs/vfs/fs_epoll: Keep the mask of epoll_pwait() until its signals run.
    
    A program blocks a signal and unblocks it only in epoll_pwait(), so that
    it cannot lose the signal between a test and the wait.  In a system call,
    a signal is dispatched at the end of the call.  epoll_pwait() restored the
    old mask before that, so the handler did not run and the signal stayed
    pending.  The call gave EINTR and the program saw no signal.  In a flat
    build, a signal that was pending before the call ran its handler, but the
    call did not stop: it waited until the timeout and returned 0.
    
    Do as ppoll() and pselect() do: set the mask with nxsig_waitmask_set(),
    give EINTR at once if the mask unblocks a pending signal, and keep the
    temporary mask until the end of the system call.  The new
    nxsig_waitmask_restore() does the last step with nxsig_restoremask().  The
    critical section from the test to the wait closes the window for a
    signal, as in those calls.
    
    Set the mask once, before the retry loop.  A second set after a wakeup
    with no event would save the temporary mask as the mask to restore.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 fs/vfs/fs_epoll.c                      | 32 ++++++++++++++++++++++++--------
 include/nuttx/signal.h                 | 18 ++++++++++++++++++
 sched/signal/sig_unmaskpendingsignal.c | 10 ++++++++++
 3 files changed, 52 insertions(+), 8 deletions(-)

diff --git a/fs/vfs/fs_epoll.c b/fs/vfs/fs_epoll.c
index 2b234248177..9801e90ee5f 100644
--- a/fs/vfs/fs_epoll.c
+++ b/fs/vfs/fs_epoll.c
@@ -998,6 +998,7 @@ int epoll_pwait(int epfd, FAR struct epoll_event *evs,
   FAR epoll_head_t *eph;
 #ifndef CONFIG_DISABLE_ALL_SIGNALS
   sigset_t oldsigmask;
+  irqstate_t flags;
 #endif
   int ret;
 
@@ -1019,6 +1020,20 @@ int epoll_pwait(int epfd, FAR struct epoll_event *evs,
       goto out;
     }
 
+  /* Set the mask for the whole wait.  Without signals, there is no mask to
+   * set.  A pending signal that the mask unblocks stops the call at once.
+   * The critical section closes the window from this test to the wait.
+   */
+
+#ifndef CONFIG_DISABLE_ALL_SIGNALS
+  flags = enter_critical_section();
+  if (nxsig_waitmask_set(sigmask, &oldsigmask))
+    {
+      ret = -EINTR;
+      goto err;
+    }
+#endif
+
 retry:
   ret = epoll_setup(eph);
   if (ret < 0)
@@ -1026,11 +1041,7 @@ retry:
       goto err;
     }
 
-  /* Wait the poll ready.  Without signals, there is no mask to set. */
-
-#ifndef CONFIG_DISABLE_ALL_SIGNALS
-  nxsig_procmask(SIG_SETMASK, sigmask, &oldsigmask);
-#endif
+  /* Wait the poll ready */
 
   if (timeout == 0)
     {
@@ -1045,9 +1056,6 @@ retry:
       ret = nxsem_wait(&eph->sem);
     }
 
-#ifndef CONFIG_DISABLE_ALL_SIGNALS
-  nxsig_procmask(SIG_SETMASK, &oldsigmask, NULL);
-#endif
   if (ret < 0 && ret != -ETIMEDOUT)
     {
       goto err;
@@ -1064,11 +1072,19 @@ retry:
       ret = num;
     }
 
+#ifndef CONFIG_DISABLE_ALL_SIGNALS
+  nxsig_waitmask_restore(&oldsigmask);
+  leave_critical_section(flags);
+#endif
   epoll_filep_close(filep);
 
   return ret;
 
 err:
+#ifndef CONFIG_DISABLE_ALL_SIGNALS
+  nxsig_waitmask_restore(&oldsigmask);
+  leave_critical_section(flags);
+#endif
   epoll_filep_close(filep);
   set_errno(-ret);
 out:
diff --git a/include/nuttx/signal.h b/include/nuttx/signal.h
index 055338abd43..43a143a1e32 100644
--- a/include/nuttx/signal.h
+++ b/include/nuttx/signal.h
@@ -336,6 +336,24 @@ sigset_t nxsig_pendingset(FAR struct tcb_s *stcb);
 
 bool nxsig_waitmask_set(FAR const sigset_t *set, FAR sigset_t *saved);
 
+/****************************************************************************
+ * Name: nxsig_waitmask_restore
+ *
+ * Description:
+ *   Restore the signal mask that nxsig_waitmask_set() saved.  In a system
+ *   call, the temporary mask stays until the end of the system call, so
+ *   that the handlers of the signals that it unblocks run.
+ *
+ * Input Parameters:
+ *   saved - The signal mask from nxsig_waitmask_set()
+ *
+ * Returned Value:
+ *   None
+ *
+ ****************************************************************************/
+
+void nxsig_waitmask_restore(FAR const sigset_t *saved);
+
 /****************************************************************************
  * Name: nxsig_procmask
  *
diff --git a/sched/signal/sig_unmaskpendingsignal.c 
b/sched/signal/sig_unmaskpendingsignal.c
index 6b1507c3380..dd941a69d42 100644
--- a/sched/signal/sig_unmaskpendingsignal.c
+++ b/sched/signal/sig_unmaskpendingsignal.c
@@ -211,3 +211,13 @@ bool nxsig_waitmask_set(FAR const sigset_t *set, FAR 
sigset_t *saved)
   sigemptyset(&rtcb->sigwaitmask);
   return nxsig_unmask_pendingsignal();
 }
+
+/****************************************************************************
+ * Name: nxsig_waitmask_restore
+ ****************************************************************************/
+
+void nxsig_waitmask_restore(FAR const sigset_t *saved)
+{
+  nxsig_restoremask(this_task(), saved);
+  nxsig_unmask_pendingsignal();
+}

Reply via email to