casaroli commented on code in PR #22300:
URL: https://github.com/apache/nuttx/pull/22300#discussion_r4696536155


##########
sched/signal/sig_timedwait.c:
##########
@@ -192,80 +192,78 @@ int nxsig_timedwait(FAR const sigset_t *set, FAR struct 
siginfo *info,
       leave_critical_section(flags);
 
       ret = nxsig_clockwait(CLOCK_REALTIME, 0, timeout, NULL);
-      if (ret < 0)
+      if (ret >= 0)
         {
-          sigemptyset(&rtcb->sigwaitmask);
-          rtcb->sigunbinfo = NULL;
-          return ret;
-        }
+          flags = enter_critical_section();
 
-      flags = enter_critical_section();
+          /* We are running again, clear the sigwaitmask */
 
-      /* We are running again, clear the sigwaitmask */
-
-      sigemptyset(&rtcb->sigwaitmask);
-
-      /* When we awaken, the cause will be in the TCB.  Get the signal number
-       * or timeout) that awakened us.
-       */
+          sigemptyset(&rtcb->sigwaitmask);
 
-      if (GOOD_SIGNO(unbinfo.si_signo))
-        {
-          /* We were awakened by a signal... but is it one of the signals
-           * that we were waiting for?
+          /* When we awaken, the cause will be in the TCB.
+           * Get the signal number or timeout that awakened us.
            */
 
-          if (nxsig_ismember(set, unbinfo.si_signo) == 1)
+          if (GOOD_SIGNO(unbinfo.si_signo))
             {
-              /* Yes.. the return value is the number of the signal that
-               * awakened us.
+              /* We were awakened by a signal... but is it one of the
+               * signals that we were waiting for?
                */
 
-              ret = unbinfo.si_signo;
-            }
-          else
-            {
-              /* No... then report the EINTR error */
+              if (nxsig_ismember(set, unbinfo.si_signo) == 1)
+                {
+                  /* Yes.. the return value is the number of the signal
+                   * that awakened us.
+                   */
 
-              ret = -EINTR;
-            }
-        }
-      else
-        {
-          /* Otherwise, we must have been awakened by the timeout or,
-           * perhaps, the wait was cancelled.
-           */
+                  ret = unbinfo.si_signo;
+                }
+              else
+                {
+                  /* No... then report the EINTR error */
 
-#ifdef CONFIG_CANCELLATION_POINTS
-          if (unbinfo.si_signo == SIG_CANCEL_TIMEOUT)
-            {
-              /* The wait was canceled */
-
-              ret = -unbinfo.si_errno;
-              DEBUGASSERT(ret < 0);
+                  ret = -EINTR;
+                }
             }
           else
-#endif
             {
-              /* We were awakened by a timeout.  Set EAGAIN and return an
-               * error.
+              /* Otherwise, we must have been awakened by the timeout or,
+               * perhaps, the wait was cancelled.
                */
 
-              DEBUGASSERT(unbinfo.si_signo == SIG_WAIT_TIMEOUT);
-              ret = -EAGAIN;
+#ifdef CONFIG_CANCELLATION_POINTS
+              if (unbinfo.si_signo == SIG_CANCEL_TIMEOUT)
+                {
+                  /* The wait was canceled */
+
+                  ret = -unbinfo.si_errno;
+                  DEBUGASSERT(ret < 0);
+                }
+              else
+#endif
+                {
+                  /* We were awakened by a timeout.  Set EAGAIN and return
+                   * an error.
+                   */
+
+                  DEBUGASSERT(unbinfo.si_signo == SIG_WAIT_TIMEOUT);
+                  ret = -EAGAIN;
+                }
             }
-        }
 
-      rtcb->sigunbinfo = NULL;
+          rtcb->sigunbinfo = NULL;
 
-      if (info != NULL)
-        {
-          memcpy(info, &unbinfo, sizeof(struct siginfo));
+          if (info != NULL)
+            {
+              memcpy(info, &unbinfo, sizeof(struct siginfo));
+            }
+
+          leave_critical_section(flags);
+          return ret;
         }
     }
 
-  leave_critical_section(flags);
-
+  rtcb->sigunbinfo = NULL;

Review Comment:
   This removes the `leave_critical_section(flags)` that ended the function. 
When a waited-for signal is already pending, the loop at line 137 sets `ret` to 
the signal number, the `if (ret < 0)` block is skipped, and the function 
returns here with the critical section of line 120 still held: interrupts stay 
disabled and `irqcount` stays raised for the caller.
   
   Fix: call `leave_critical_section(flags)` before `return ret` on the path 
where `ret >= 0` came from the pending signal, for example `else { 
leave_critical_section(flags); }` after the `if (ret < 0)` block.



##########
sched/signal/sig_timedwait.c:
##########
@@ -192,80 +192,78 @@ int nxsig_timedwait(FAR const sigset_t *set, FAR struct 
siginfo *info,
       leave_critical_section(flags);
 
       ret = nxsig_clockwait(CLOCK_REALTIME, 0, timeout, NULL);
-      if (ret < 0)
+      if (ret >= 0)

Review Comment:
   When `nxsig_clockwait()` fails (zero timeout: `-EAGAIN`, bad timeout: 
`-EINVAL`, cancellation: `-ECANCELED`), `rtcb->sigwaitmask` is no longer 
cleared; master cleared it before the early return. The stale mask stays in the 
TCB, and `nxsig_tcbdispatch()` then lets an ignored signal from that set wake a 
later `nanosleep()` of the task with `EINTR`.
   
   Fix: add an `else` branch to `if (ret >= 0)` that calls 
`sigemptyset(&rtcb->sigwaitmask)`, as master does.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to