Track the actual item count in BlockingCollection - #133637
Open
aw0lid wants to merge 1 commit into
Open
Conversation
|
Azure Pipelines: Successfully started running 3 pipeline(s). 13 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-collections |
This was referenced Sep 11, 2026
Open
aw0lid
marked this pull request as ready for review
September 11, 2026 09:54
|
Azure Pipelines: Successfully started running 3 pipeline(s). 13 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
gfoidl
reviewed
Sep 11, 2026
| { | ||
| current = Volatile.Read(ref _actualCount); | ||
| } | ||
| while (Interlocked.CompareExchange(ref _actualCount, current + 1, current) != current); |
Member
There was a problem hiding this comment.
Interlocked.Add can't be used due to build for older targets?
| int current; | ||
| do | ||
| { | ||
| current = Volatile.Read(ref _actualCount); |
Member
There was a problem hiding this comment.
You can drop the volatile read, because below the interlocked operation provides the barrier.
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.
Description
BlockingCollection.IsCompletedcurrently uses_occupiedNodes.CurrentCountto determine whether the collection is empty.However,
_occupiedNodesis also used to synchronize consumer operations. This can causeIsCompletedto temporarily reporttrueeven though the collection still contains an item.For example:
BlockingCollectioncontains a single item.CompleteAdding()is called, soIsCompletedis initiallyfalse.Takeoperation and successfully waits on_occupiedNodes._occupiedNodes.CurrentCountbecomes0, causingIsCompletedto returntrueeven though the item has not yet been removed from the collection._occupiedNodes.CurrentCountis restored to1.IsCompletedthen returns tofalse.As a result,
IsCompletedcan transition fromfalsetotrueand back tofalseeven though the item was never removed from the collection.Fix
Introduce a separate
_actualCountfield to track the number of items successfully added to and removed from the collection._actualCountis initialized from the existing collection count and updated atomically after each successful add or remove operation, specifically inTryAddWithNoTimeValidationandTryTakeWithNoTimeValidation.CountandIsCompletednow use_actualCountinstead of_occupiedNodes.CurrentCount.This separates the synchronization state of
_occupiedNodesfrom the actual item count and prevents consumer cancellation from changing the state reported byIsCompleted.Tests
Added a regression test that verifies that
IsCompletedremainsfalsewhen:CompleteAdding()has been called,The test then removes the remaining item and verifies that
IsCompletedbecomestrue.Closes #109217