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

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

commit 32a31d39bd3be6c31ee936ec5cbeffcc985df31c
Author: Bowen Wang <[email protected]>
AuthorDate: Wed Jul 15 23:35:48 2026 +0800

    include/nuttx/ring: fix cnt leak on zero-count overwrite
    
    ring_mp_overwrite_start() bumps prod_head.cnt unconditionally once its
    reserve CAS succeeds, even when count is 0.  Its commit counterpart
    ring_mp_finish() returns early when buf->cnt == 0 and never bumps
    prod_tail.cnt.  A zero-count overwrite therefore adds a prod_head.cnt
    increment with no matching prod_tail.cnt increment, leaving a permanent
    head.cnt != tail.cnt gap.
    
    The RTS tail sync in ring_mp_finish() only advances prod_tail.idx when
    tail.cnt catches up to head.cnt.  Once the gap is permanent that
    condition can never hold again, so prod_tail.idx freezes.  With a frozen
    tail, ring_mp_overwrite_start() computes committed == 0, every subsequent
    overwrite finds discard > committed, and all producers spin forever in
    the eviction retry loop.
    
    Guard against count == 0 before the reserve loop and return an empty buf,
    mirroring the n == 0 break already present in ring_mp_start().  The copy
    wrappers stay correct because ring_buf_init() already zeroes buf on a
    zero count.
    
    Signed-off-by: Bowen Wang <[email protected]>
---
 include/nuttx/ring.h | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/include/nuttx/ring.h b/include/nuttx/ring.h
index 8adc8564c89..12835cc011e 100644
--- a/include/nuttx/ring.h
+++ b/include/nuttx/ring.h
@@ -1631,6 +1631,18 @@ ring_mp_overwrite_start(FAR struct ring_s *r, uint32_t 
count,
       count = r->capacity;
     }
 
+  if (count == 0)
+    {
+      /* Nothing to reserve.  Bumping prod_head.cnt here without a
+       * matching prod_tail.cnt bump (ring_mp_finish() returns early on
+       * buf->cnt == 0) would leave a permanent cnt mismatch that
+       * deadlocks the RTS tail sync.  Return an empty buf instead.
+       */
+
+      ring_buf_init(buf, r, 0, 0, elem_size);
+      return all_discard;
+    }
+
   old_prod = atomic_read(&r->prod_head);
   for (; ; )
     {

Reply via email to