fix: stop SpscRingBuffer.Count reporting a count the buffer never held [patch] - #92
Merged
Merged
Conversation
…d [patch] Count read tail and then head. A thread that was neither producer nor consumer could see the consumer move head past the stale tail between the two reads, and the masked difference then wrapped to a value near Capacity on an idle buffer. Count now reads head, then tail, then head again, and retries until head did not move, so the tail it uses was observed while head held that value. Fixes #81 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EdrcmpMTNHSnACRgnNGs8v
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Fixes #81
What was wrong
SpscRingBuffer<T>.Countreadtailand thenhead, and returned(tail - head) & mask. Suppose a diagnostics thread, which is neither the producer nor the consumer, readsCount. Between its two reads, the producer and consumer can both move on, soheadpasses the staletail. The negative difference then wraps to a value nearCapacity. The result is a count the buffer never held, which breaks metering and back-pressure heuristics built on it.Change
Countnow readshead, thentail, thenheadagain. It retries untilheadstays the same across both reads. Thetailit uses was therefore observed whileheadheld that value, so the difference is a count the buffer really had. Masking already bounds the result to[0, Capacity], so no extra clamp is needed. The retry only affects the thread readingCount:TryEnqueueandTryDequeueare unchanged and remain wait-free.Tests
Count_ReadFromThirdThread_NeverExceedsTrueCountis the stress test from the issue. The producer only enqueues into an empty buffer, so the true count is always 0 or 1, and a third thread pollsCountfor 2 seconds.dotnet test: 419 passed, 0 failed), andContainers.csprojbuilds with 0 warnings.🤖 Generated with Claude Code
https://claude.ai/code/session_01EdrcmpMTNHSnACRgnNGs8v
Generated by Claude Code