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

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

commit b7ab3ff3175697afed3468b70be40b64cc85e1df
Author: Marco Casaroli <[email protected]>
AuthorDate: Sun Oct 18 23:34:48 2026 +0200

    sched/signal: Do not wake an uninterruptible wait in a system call.
    
    An unmasked signal wakes a task that waits on a semaphore with -EINTR.
    An uninterruptible wait then waits again.  During a system call, the
    signal handler runs only when the call returns.  Thus the wake does
    nothing useful: it costs two context switches and a retry for each
    signal.
    
    Do not wake the task when it is in a system call (TCB_FLAG_SYSCALL) and
    waits on a semaphore with SEM_TYPE_UNINTERRUPTIBLE.  The signal stays
    pending, and its handler runs when the system call returns.  In a flat
    build and in a kernel thread, TCB_FLAG_SYSCALL is not set.  There, the
    signal still wakes the wait, so that the handler runs at once and the
    wait then continues.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 sched/signal/sig_dispatch.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/sched/signal/sig_dispatch.c b/sched/signal/sig_dispatch.c
index a2f8fee1d7a..cc3fe67af3d 100644
--- a/sched/signal/sig_dispatch.c
+++ b/sched/signal/sig_dispatch.c
@@ -716,7 +716,18 @@ int nxsig_tcbdispatch(FAR struct tcb_s *stcb, siginfo_t 
*info,
 
       if (stcb->task_state == TSTATE_WAIT_SEM && !ignored)
         {
-          nxsem_wait_irq(stcb, EINTR);
+          FAR sem_t *sem = stcb->waitobj;
+
+          /* Not for an uninterruptible semaphore in a system call: the
+           * handler runs only when the call returns, and the wait would
+           * block again.
+           */
+
+          if ((stcb->flags & TCB_FLAG_SYSCALL) == 0 ||
+              (sem->flags & SEM_TYPE_UNINTERRUPTIBLE) == 0)
+            {
+              nxsem_wait_irq(stcb, EINTR);
+            }
         }
 
 #if !defined(CONFIG_DISABLE_MQUEUE) || !defined(CONFIG_DISABLE_MQUEUE_SYSV)

Reply via email to