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


The following commit(s) were added to refs/heads/master by this push:
     new 3c263d24a94 mm/iob: Undo the waiter count of an IOB wait that ends 
without an IOB.
3c263d24a94 is described below

commit 3c263d24a940bd497538e288887415b1392bc9d6
Author: Marco Casaroli <[email protected]>
AuthorDate: Wed Nov 18 00:10:28 2026 +0100

    mm/iob: Undo the waiter count of an IOB wait that ends without an IOB.
    
    iob_allocwait() counts a waiter (g_throttle_wait++ for a throttled
    allocation, g_iob_count-- for the others) before it waits on the
    semaphore.  When the wait ended without an IOB (the timeout of
    iob_timedalloc(), for example a send() with SO_SNDTIMEO), the count
    stayed.  iob_free() then committed the next
    freed IOB to the committed list for a waiter that was gone and posted
    the semaphore: that IOB was out of the free count until another
    waiter came.  Ten timed-out sends on a full TCP connection took 11 of
    the 48 free IOBs.  With more such waits the committed list can take
    almost the whole pool: four senders with SO_SNDTIMEO on connections
    whose readers did not read left 68 of 72 free IOBs there after all
    sockets closed.  The free count then stays below CONFIG_IOB_THROTTLE,
    and tcp_datahandler() drops all received data that no recv() waits for.
    
    Now a failed wait undoes its count under g_iob_lock, unless
    iob_free() already counted the waiter out; then the IOB committed to
    it is taken (its post comes at once).
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 mm/iob/iob_alloc.c | 40 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)

diff --git a/mm/iob/iob_alloc.c b/mm/iob/iob_alloc.c
index e06f5518805..0073eee7254 100644
--- a/mm/iob/iob_alloc.c
+++ b/mm/iob/iob_alloc.c
@@ -207,6 +207,46 @@ static FAR struct iob_s *iob_allocwait(bool throttled, 
unsigned int timeout)
                                    iob_allocwait_gettimeout(start, timeout));
         }
 
+      if (ret < 0)
+        {
+          /* The wait ended without an IOB (for example a timeout).
+           * Unless iob_free() already counted this waiter out (it
+           * committed an IOB to it and posts the semaphore), undo the
+           * count: else the next freed IOB goes to the committed list for
+           * a waiter that is gone, and it is lost to the free count.
+           */
+
+          flags = spin_lock_irqsave(&g_iob_lock);
+#if CONFIG_IOB_THROTTLE > 0
+          if (throttled ? g_throttle_wait > 0 : g_iob_count < 0)
+#else
+          if (g_iob_count < 0)
+#endif
+            {
+#if CONFIG_IOB_THROTTLE > 0
+              if (throttled)
+                {
+                  g_throttle_wait--;
+                }
+              else
+#endif
+                {
+                  g_iob_count++;
+                }
+
+              spin_unlock_irqrestore(&g_iob_lock, flags);
+              return NULL;
+            }
+
+          /* An IOB was committed to this waiter: its post comes at once.
+           * (If this second wait fails too, the IOB stays committed and
+           * the next waiter takes it at once: it is not lost.)
+           */
+
+          spin_unlock_irqrestore(&g_iob_lock, flags);
+          ret = nxsem_wait_uninterruptible(sem);
+        }
+
       if (ret >= 0)
         {
           /* When we wake up from wait successfully, an I/O buffer was

Reply via email to