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


##########
sched/signal/sig_timedwait.c:
##########
@@ -194,77 +194,82 @@ int nxsig_timedwait(FAR const sigset_t *set, FAR struct 
siginfo *info,
       ret = nxsig_clockwait(CLOCK_REALTIME, 0, timeout, NULL);
       if (ret < 0)
         {
+          /* No wait happened: the mask was never consumed.  Clear it, or
+           * an ignored signal of the set wakes a later nanosleep().
+           */
+
           sigemptyset(&rtcb->sigwaitmask);
           rtcb->sigunbinfo = NULL;
-          return ret;
         }
+      else
+        {
+          flags = enter_critical_section();
 
-      flags = enter_critical_section();
-
-      /* We are running again, clear the sigwaitmask */
-
-      sigemptyset(&rtcb->sigwaitmask);
+          /* We are running again, clear the 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 */
-
-              ret = -EINTR;
-            }
-        }
-      else
-        {
-          /* Otherwise, we must have been awakened by the timeout or,
-           * perhaps, the wait was cancelled.
-           */
+              if (nxsig_ismember(set, unbinfo.si_signo) == 1)
+                {
+                  /* Yes.. the return value is the number of the signal
+                   * that awakened us.
+                   */
 
-#ifdef CONFIG_CANCELLATION_POINTS
-          if (unbinfo.si_signo == SIG_CANCEL_TIMEOUT)
-            {
-              /* The wait was canceled */
+                  ret = unbinfo.si_signo;
+                }
+              else
+                {
+                  /* No... then report the EINTR error */
 
-              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);
+      leave_critical_section(flags);

Review Comment:
   `leave_critical_section(flags)` is now inside the `if (ret < 0)` block of 
line 176, not at the end of the function. Two paths are wrong:
   
   - A waited-for signal is already pending: the loop at line 137 sets `ret`, 
the block is skipped, and the function returns with the critical section of 
line 120 still held.
   - `nxsig_clockwait()` fails (zero timeout, bad timeout, cancellation): line 
192 already left the critical section, and line 271 leaves it a second time. 
With `CONFIG_IRQCOUNT` (all SMP builds) this trips `DEBUGASSERT(rtcb->irqcount 
> 0)` in `leave_critical_section()`.
   
   On sim:smp with a small test: `sigtimedwait()` with a zero timeout stops 
with `Assertion failed : at file: irq/irq_csection.c:393`, and after 
`sigwaitinfo()` with the signal already pending the task has `irqcount` 1. Both 
are fine on the merge base.
   
   Fix: move this `leave_critical_section(flags)` into the `else` branch of 
line 204 (after the copy to `info`), and add `else { 
leave_critical_section(flags); }` after the `if (ret < 0)` block for the 
pending path.



-- 
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